답안 #970913

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
970913 2024-04-27T13:57:11 Z vjudge1 Fireworks (APIO16_fireworks) C++14
26 / 100
87 ms 196996 KB
#include<bits/stdc++.h>

using namespace std;

#define int long long

const int N=5000+10, inf=1e18;
int n, m, f[N][N], d[N];
vector<int> vals;
vector<pair<int, int>> g[N];

void pre_dfs(int u){
   for (auto &e:g[u]){
      d[e.first]=d[u]+e.second;
      pre_dfs(e.first);
   }
}

void dfs(int u){
   if (g[u].empty()){
      f[u][lower_bound(vals.begin(), vals.end(), d[u])-vals.begin()]=0;
      return;
   }
   for (int i=0; i<N; ++i) f[u][i]=0;
   for (auto &e:g[u]){
      int v=e.first, w=e.second;
      dfs(v);
      deque<int> dq;
      for (int i=1, j=1; i<(int)vals.size(); ++i){
         while (dq.size() && dq.front()<i) dq.pop_front();
         while (j<(int)vals.size() && vals[j]-vals[i]<=w){
            while (dq.size() && f[v][dq.back()]+vals[dq.back()]>=f[v][j]+vals[j]) dq.pop_back();
            dq.push_back(j);
            ++j;
         }
         f[v][i]=min(f[v][i], f[v][dq.front()]+vals[dq.front()]-vals[i]);
      }
      for (int i=2; i<(int)vals.size(); ++i) f[v][i]=min(f[v][i], f[v][i-1]+vals[i]-vals[i-1]);
      for (int i=1; i<(int)vals.size(); ++i) f[u][i]=min(inf, f[u][i]+f[v][i]);
   }
}

int32_t main(){
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);
   cin >> n >> m;
   if (n==1){
      vector<int> v;
      for (int i=2; i<=n+m; ++i){
         int p, c; cin >> p >> c;
         v.push_back(c);
      }
      sort(v.begin(), v.end());
      int val=v[m/2], ans=0;
      for (auto &i:v) ans+=abs(i-val);
      cout << ans << '\n';
      return 0;
   }
   for (int i=2; i<=m+n; ++i){
      int p, c; cin >> p >> c;
      g[p].emplace_back(i, c);
   }
   for (int i=0; i<N; ++i) for (int j=0; j<N; ++j) f[i][j]=inf;
   pre_dfs(1);
   vals.push_back(-1);
   for (int i=1; i<=n+m; ++i) vals.push_back(d[i]);
   sort(vals.begin(), vals.end()); vals.resize(unique(vals.begin(), vals.end())-vals.begin());
   dfs(1);
   cout << *min_element(f[1]+1, f[1]+(int)vals.size()) << '\n';
   return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 196948 KB Output is correct
2 Correct 45 ms 196892 KB Output is correct
3 Correct 44 ms 196952 KB Output is correct
4 Correct 45 ms 196888 KB Output is correct
5 Correct 44 ms 196948 KB Output is correct
6 Correct 44 ms 196948 KB Output is correct
7 Correct 44 ms 196816 KB Output is correct
8 Correct 44 ms 196944 KB Output is correct
9 Correct 44 ms 196948 KB Output is correct
10 Correct 44 ms 196892 KB Output is correct
11 Correct 45 ms 196932 KB Output is correct
12 Correct 44 ms 196944 KB Output is correct
13 Correct 44 ms 196944 KB Output is correct
14 Correct 46 ms 196944 KB Output is correct
15 Correct 44 ms 196948 KB Output is correct
16 Correct 44 ms 196944 KB Output is correct
17 Correct 52 ms 196944 KB Output is correct
18 Correct 44 ms 196948 KB Output is correct
19 Correct 55 ms 196868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 87 ms 196948 KB Output is correct
12 Correct 45 ms 196892 KB Output is correct
13 Correct 44 ms 196952 KB Output is correct
14 Correct 45 ms 196888 KB Output is correct
15 Correct 44 ms 196948 KB Output is correct
16 Correct 44 ms 196948 KB Output is correct
17 Correct 44 ms 196816 KB Output is correct
18 Correct 44 ms 196944 KB Output is correct
19 Correct 44 ms 196948 KB Output is correct
20 Correct 44 ms 196892 KB Output is correct
21 Correct 45 ms 196932 KB Output is correct
22 Correct 44 ms 196944 KB Output is correct
23 Correct 44 ms 196944 KB Output is correct
24 Correct 46 ms 196944 KB Output is correct
25 Correct 44 ms 196948 KB Output is correct
26 Correct 44 ms 196944 KB Output is correct
27 Correct 52 ms 196944 KB Output is correct
28 Correct 44 ms 196948 KB Output is correct
29 Correct 55 ms 196868 KB Output is correct
30 Incorrect 49 ms 196996 KB Output isn't correct
31 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 87 ms 196948 KB Output is correct
12 Correct 45 ms 196892 KB Output is correct
13 Correct 44 ms 196952 KB Output is correct
14 Correct 45 ms 196888 KB Output is correct
15 Correct 44 ms 196948 KB Output is correct
16 Correct 44 ms 196948 KB Output is correct
17 Correct 44 ms 196816 KB Output is correct
18 Correct 44 ms 196944 KB Output is correct
19 Correct 44 ms 196948 KB Output is correct
20 Correct 44 ms 196892 KB Output is correct
21 Correct 45 ms 196932 KB Output is correct
22 Correct 44 ms 196944 KB Output is correct
23 Correct 44 ms 196944 KB Output is correct
24 Correct 46 ms 196944 KB Output is correct
25 Correct 44 ms 196948 KB Output is correct
26 Correct 44 ms 196944 KB Output is correct
27 Correct 52 ms 196944 KB Output is correct
28 Correct 44 ms 196948 KB Output is correct
29 Correct 55 ms 196868 KB Output is correct
30 Incorrect 49 ms 196996 KB Output isn't correct
31 Halted 0 ms 0 KB -