Guest User

Untitled

a guest
Aug 31st, 2014
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<algorithm>
  4. #include<set>
  5. #include<map>
  6. #include<queue>
  7. #include<stack>
  8. #include<deque>
  9. #include<vector>
  10. #include<string>
  11. using namespace std;
  12.  
  13. #define INF 1000000000
  14. #define lint long long
  15. #define pb push_back
  16. #define mp make_pair
  17. #define MOD 1000000007
  18.  
  19. pair <int,pair<int,int> > a[100005];
  20. lint p[100005],mx[100005],d[100005];
  21. int n;
  22.  
  23. int check(lint g)
  24. {
  25.     int i,j=n,k;
  26.     lint gsum=0,esum,enc,enn;
  27.     for(i=n;i>0;--i)
  28.     {
  29.         gsum+=a[i].second.first;
  30.         while(1)
  31.         {
  32.             if(gsum-a[j].second.first<g)break;
  33.             gsum-=a[j].second.first;
  34.             --j;
  35.         }
  36.         if(gsum<g)continue;
  37.         enc=p[j]-p[i-1]+(a[i].first-a[i-1].first);
  38.         enn=mx[j]-p[j];
  39.         if(enn+enc>=0)return 1;
  40.     }
  41.     return 0;
  42. }
  43. int main()
  44. {
  45.     freopen("divide.in","r",stdin);
  46.     freopen("divide.out","w",stdout);
  47.     int i,j;
  48.     lint l=0,r=1;
  49.     scanf("%d",&n);
  50.     p[0]=0;
  51.     for(i=1;i<=n;++i)
  52.     {
  53.         scanf("%d %d %d",&a[i].first,&a[i].second.first,&a[i].second.second);
  54.         d[i]=d[i-1]+a[i].first;
  55.         p[i]=p[i-1]+a[i].second.second+a[i-1].first-a[i].first;
  56.         r+=a[i].second.first;
  57.     }
  58.     p[n+1]=INF*1ll*INF;
  59.     mx[n]=p[n];
  60.     for(i=n-1;i>0;--i)
  61.     {
  62.         mx[i]=max(p[i],mx[i+1]);
  63.     }
  64.     while(r-l>1)
  65.     {
  66.         lint mid=(r+l)/2;
  67.         if(check(mid))
  68.             l=mid;
  69.         else r=mid;
  70.     }
  71.     if(check(r))cout<<r;
  72.     else cout<<l;
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment