답안 #1019652

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1019652 2024-07-11T06:35:10 Z huutuan Shortcut (IOI16_shortcut) C++14
0 / 100
1 ms 604 KB
#include "shortcut.h"

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N=8000, inf=1e18;
int n, m, c, ll[N];
vector<pair<int, int>> g[N];
int tl, tr;
pair<int, int> f[N];

pair<int, int> dfs(int u, int p){
   pair<int, int> ans;
   int mx1=0, mx2=0;
   for (auto &e:g[u]){
      int v=e.first, w=e.second;
      if (v==p || (tl<=v && v<=tr)) continue;
      auto ff=dfs(v, u);
      ans.first=max(ans.first, ff.first);
      ans.second=max(ans.second, ff.second+w);
      if (mx1<ff.second+w) mx2=mx1, mx1=ff.second+w;
      else mx2=max(mx2, ff.second+w);
   }
   ans.first=max({ans.first, ans.second, mx1+mx2});
   return f[u]=ans;
}

int pf[N];

int calculate(int u, int v){
   tl=u, tr=v;
   pf[u]=0;
   for (int i=u; i<v; ++i) pf[i+1]=pf[i]+ll[i];
   int sum=pf[v]+c, ans=0;
   for (int i=u; i<=v; ++i){
      dfs(i, -1);
      ans=max(ans, f[i].first);
   }
   deque<int> dq;
   int mx=-inf;
   for (int i=u; i<=v; ++i){
      while (dq.size() && (pf[i]-pf[dq.front()])*2>sum){
         mx=max(mx, f[dq.front()].second+pf[dq.front()]);
         dq.pop_front();
      }
      if (dq.size()) ans=max(ans, f[i].second+f[dq.front()].second+pf[i]-pf[dq.front()]);
      if (mx!=-inf) ans=max(ans, f[i].second+sum-pf[i]+mx);
      while (dq.size() && f[dq.back()].second-pf[dq.back()]<f[i].second-pf[i]) dq.pop_back();
      dq.push_back(i);
   }
   return ans;
}

long long find_shortcut(int32_t _n, vector<int32_t> _l, vector<int32_t> _d, int32_t _c)
{
   n=_n, c=_c;
   for (int i=0; i<n-1; ++i) g[i].emplace_back(i+1, _l[i]), g[i+1].emplace_back(i, _l[i]), ll[i]=_l[i];
   m=n;
   for (int i=0; i<n; ++i) if (_d[i]){
      g[i].emplace_back(m, _d[i]);
      g[m].emplace_back(i, _d[i]);
      ++m;
   }
   int ans=inf;
   for (int i=0, j=0; i<n; ++i){
      int val=calculate(i, j), val2;
      while (j+1<n && (val2=calculate(i, j+1))<=val){
         ++j;
         val=val2;
      }
      ans=min(ans, val);
   }
   return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB n = 4, 80 is a correct answer
2 Incorrect 0 ms 604 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB n = 4, 80 is a correct answer
2 Incorrect 0 ms 604 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB n = 4, 80 is a correct answer
2 Incorrect 0 ms 604 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB n = 4, 80 is a correct answer
2 Incorrect 0 ms 604 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB n = 4, 80 is a correct answer
2 Incorrect 0 ms 604 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB n = 4, 80 is a correct answer
2 Incorrect 0 ms 604 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB n = 4, 80 is a correct answer
2 Incorrect 0 ms 604 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB n = 4, 80 is a correct answer
2 Incorrect 0 ms 604 KB n = 9, incorrect answer: jury 110 vs contestant 120
3 Halted 0 ms 0 KB -