FHVirus

TIOJ 1347 BurnChicken

Jul 7th, 2021 (edited)
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. // Knapsack DP is harder than FFT.
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll;
  5. #define ff first
  6. #define ss second
  7. #define pb emplace_back
  8. #define AI(x) begin(x),end(x)
  9. template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;}
  10. template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;}
  11. #ifdef OWO
  12. #define debug(args...) SDF(#args, args)
  13. #define OIU(args...) ostream& operator<<(ostream&O,args)
  14. #define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
  15. LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
  16. template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
  17. template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
  18. #else
  19. #pragma GCC optimize("Ofast")
  20. #define debug(...) ((void)0)
  21. #endif
  22.  
  23. #include<unistd.h>
  24. inline char RC(){static char buf[65536],*p=buf,*q=buf;return p==q&&(q=(p=buf)+read(0,buf,65536))==buf?-1:*p++;}
  25. inline int R(){static char c;int a;while((c=RC())<'0');a=c^'0';while((c=RC())>='0')a*=10,a+=c^'0';return a;}
  26. inline void W(ll n){char OB[20],OP=0,buf[20],p;if(n==0)OB[OP++]='0';p=0;while(n)buf[p++]='0'+(n%10),n/=10;for(--p;p>=0;--p)OB[OP++]=buf[p];write(1,OB,OP);}
  27.  
  28. const int kN = 1000001;
  29. const ll inf = 1e18;
  30. const __int128 INF = 1e18;
  31. typedef tuple<int, int, int> tii;
  32.  
  33. constexpr ll lpow(ll x, ll e){
  34.     if(x == 0) return 0;
  35.     if(x < 0) x = -x;
  36.     ll r = 1;
  37.     while(e){
  38.         if(e & 1)
  39.             r = min((__int128) r * x, INF);
  40.         x = (ll) min((__int128) x * x, INF);
  41.         e >>= 1;
  42.     }
  43.     return r;
  44. }
  45. constexpr double dpow(double x, ll e){
  46.     if(x == 0) return 0;
  47.     if(x < 0) x = -x;
  48.     double r = 1;
  49.     while(e){
  50.         if(e & 1) r = r * x;
  51.         x = x * x;
  52.         e >>= 1;
  53.     }
  54.     return r;
  55. }
  56.  
  57. int n, k, p;
  58. int a[kN], l[kN];
  59. ll b[kN], dp[kN];
  60. pii sc[kN];
  61.  
  62. double dost(int j, int i){
  63.     return dp[j] + dpow(b[i] - b[j] - l[i] - k, p);
  64. }
  65. int inter(int a, int b, int i){
  66.     int l = i, r = n + 1, m;
  67.     while(l < r){
  68.         m = (l + r) / 2;
  69.         if(dost(b, m) <= dost(a, m)) r = m;
  70.         else l = m + 1;
  71.     }
  72.     return l;
  73. }
  74.  
  75. signed main(){
  76.     n = R(), k = R(), p = R();
  77.     for(int i = 1; i <= n; ++i) a[i] = R();
  78.     for(int i = 1; i < n; ++i) l[i] = R();
  79.     for(int i = 1; i <= n; ++i) b[i] = b[i-1] + a[i] + l[i];
  80.  
  81.     int he, ta; he = ta = 0;
  82.     for(int i = 1; i <= n; ++i){
  83.         while(he < ta and inter(sc[ta-1].ff, i-1, i) <= sc[ta-1].ss) --ta;
  84.  
  85.         if(he == ta) sc[ta++] = pii(i-1, 1);
  86.         else {
  87.             int p = inter(sc[ta-1].ff, i-1, i);
  88.             if(p <= n) sc[ta++] = pii(i-1, p);
  89.         }
  90.  
  91.         while(ta - he > 1 and sc[he+1].ss <= i) ++he;
  92.  
  93.         auto &[j, jl] = sc[he];
  94.         dp[i] = dp[j] + lpow(b[i] - b[j] - l[i] - k, p);
  95.     }
  96.     W(dp[n]);
  97.     return 0;
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment