intmain(){ LL N, C; cin>>N>>C; vector<pair<LL,LL>> event; for(int i=0; i<N;i++){ LL a, b, c; cin>>a>>b>>c; event.push_back({a-1, c}); event.push_back({b, -c}); }
sort(event.begin(), event.end()); LL ans = 0, fee = 0, t = 0; for(auto [x, y]: event){ if(x!=t){ ans += min(C, fee) * (x-t); t = x; } fee += y; } cout<<ans<<endl; return0; }
E - Peddler Stock selling on a DAG Solution: Build a reverse graph. For each vertex i, calculate dp[i]: the cheapest purchase price reachable from i (only vertex prior current vertex in DAG, therefore reverse edges). Sell at vertex i and update res = A[i] - dp[i];
intmain(){ int n, m; cin>>n>>m; vector<int> A(n+1); for(int i=1;i<=n;i++) scanf("%d", &A[i]); vector<int>g[n+1]; for(int i=0;i<m;i++){ int a, b; scanf("%d%d", &a, &b ); // reverse edges g[b].push_back(a); }
// lowest price reachable from i, sell at point i vector<LL> dp(n+1, INF); LL res = -INF; for(int i=1;i<=n;i++){ for(int x:g[i]){ dp[i] = min(dp[i], dp[x]); } if(dp[i] < INF) res = max(res, (LL)A[i] - dp[i]); dp[i] = min(dp[i], (LL)A[i]); } cout<<res<<endl; return0; }
F - +1-1x2 Return the minimum steps from X to Y, by applying:
X = X + 1
X = X - 1
X = X * 2 Solution: A BFS problem. The range of data is very big. Start search from Y instead. If current y is smaller than X, then can only apply operation 1. Otherwise, y can do %2 (if odd) or y = y + 1, or y = y - 1; Two pruning strategies:
if step is greater than best ans, break;
if current y is smaller than X, calculate abs(X - y) and continue;
#include<iostream> #include<vector> #include<queue> #include<cstring> #include<algorithm> #include<string> #include<unordered_set> usingnamespacestd; #define x first #define y second typedeflonglong LL; typedef pair<LL, LL> PLL;
intmain(){ LL x, y; cin>>x>>y; if(x>=y) cout<<x-y<<endl; else{ unordered_set<LL>s; LL ans = y - x; queue<PLL>q; q.push({y, 0}); s.insert(y); while(q.size()){ auto t = q.front(); q.pop(); if(t.y >= ans) break; ans = min(ans, t.y+ abs(t.x - x)); if (t.x <= x) continue; // not break; can no longer apply //2. if(t.x %2 ==0){ if(!s.count(t.x/2)) { s.insert(t.x/2); q.push({t.x/2, t.y + 1}); } } else { if(!s.count(t.x + 1)){ s.insert(t.x + 1); q.push({t.x + 1, t.y + 1}); } if(!s.count(t.x - 1)){ s.insert(t.x - 1); q.push({t.x - 1, t.y + 1}); } }