ARC100D
解æ³
4 ã¤ã®åºéã«åããã®ã§ï¼ä»åã㯠3ã¤ã§ï¼ããã \(i,j,k\ (i < j < k)\) ã¨ããï¼ 3ã¤ã®ä»åãã®ãã¡ï¼çãä¸ \(j\) ã«ãããã®ãåºå®ããã¨ï¼æ®ãã® 2ã¤ã対称ã«ãªãããã§æ¥½ããï¼ \(j\) ãåºå®ããã¨ãã®ãã¹ã㪠\(i,k\) ã \(i_{j}, k_{j}\) ã¨ããï¼
\(j\) ã«ããå·¦å³ 2ã¤ã«å解ããã¨ãï¼\(i\) ã®ä½ç½®ã¯
- \(P = sum_{[0,i)} A \)ã¨\(Q = sum_{[i,j)} A\) ãåããããã«ãªãã¨ããï¼
- \(A\) ã®å
ã¯ãã¹ã¦ \(0\)以ä¸ãªã®ã§ï¼\(i_{j}\) ã¯å調å¢å ï¼
å¾è
ããï¼\(i_{j}\) ã¯å°ºåãæ³ã§æ±ããããï¼ åè
ã«æ³¨æããªããå°ºåãæ³ãå®è£
ããï¼ \(P\) 㨠\(Q\) ãåãä½ã«ãªãã«ã¯ï¼\(P\) 㨠\(Q\) ã®å¤§å°é¢ä¿ãå
¥ãæ¿ããé«ã
2ã¤ã \(i_{j}\) ã®åè£ã¨ãªãï¼
\(k_{j}\) ã«ã¤ãã¦ãåæ§ï¼ å \(j\) ã«å¯¾ãã¦ï¼\(i_{j}\) 㨠\(k_{j}\) ãé«ã
2éããã¤ããã§ï¼é«ã
2x2éã試ãã°è¯ãï¼
使ã£ã¦ããè¨å·ï¼ãã¯ãç "https://ecsmtlir.hatenablog.com/entry/2022/12/23/131925"
int main() {
 ll n;
 cin >> n;
 vll a(n); rep(i,n) { cin >> a[i]; }
 vll sum(n+1); rep(i,n) sum[i+1] = sum[i] + a[i];
 using I = ll; // index or partition of vector
 // a,b,i: partitions of n
 // i in [a,b]
 auto f = [&](I a, I b, I& i) -> vector<pll> {
  while(i <= b && sum[i] - sum[a] < sum[b] - sum[i]){
   i++;
  }
  i--;
  srep(j,i,i+2){
   if(in(j, a+1, b)) {
    ll ma = max(sum[j]-sum[a], sum[b] - sum[j]);
    ll mi = min(sum[j]-sum[a], sum[b] - sum[j]);
    ret.emplace_back(ma, mi);
   }
  }
  return ret;
 };
Â
 const ll inf = 2e14 + 100;
 ll ans = inf;
 ll l = 0, r = 0;
 rep(i,n+1){
  chmax(r,i); chmin(r,n);
  chmin(l,i); chmax(l,0LL);
  for(auto [ma_l, mi_l]: f(0,i,l)){
   for(auto [ma_r, mi_r]: f(i,n,r)){
    ll ma = max(ma_l, ma_r);
    ll mi = min(mi_l, mi_r);
    chmin(ans, ma - mi);
   }
  }
  Â
 }
 cout << ans << endl;
 return 0;
}