Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 10005;
- const int Mask=(1<<14)-1;
- int n,m,k,x,h[N],g[N],H[N],v[N],d[N],t[N],c[Mask+1],top,bottom;
- int cuts;
- bool crit(int,int);
- void readAndInit(),taie();
- int main()
- {
- readAndInit();
- while(1)
- taie();
- return 0;
- }
- /// criteriu de sortare
- bool crit(int i,int j)
- {
- return H[i]<H[j];
- }
- /// citire si initializare
- void readAndInit()
- {
- cin>>n>>m>>k>>x;
- for(int i=1;i<=n;i++)
- {
- cin>>h[i]>>g[i];
- H[i]=h[i]+m*g[i];
- v[i]=i;
- }
- for(int i=1;i<=m;i++)
- t[i]=k;
- cuts=m*k;
- t[m+1]=1;
- sort(v+1,v+n+1,crit);
- c[0]=v[n--];
- top=bottom=0;
- }
- void taie()
- {
- int co=c[bottom];bottom=(bottom+1)&Mask;
- if(h[co]<x)
- {
- int days=(x-h[co])/g[co];
- if((x-h[co])%g[co]) days++;
- if(d[co]+days>m)
- {
- cout<<H[co]<<'\n';
- exit(0);
- }
- d[co]+=days;
- h[co]+=days*g[co];
- }
- while(!t[d[co]]){d[co]++;h[co]+=g[co];}
- if(d[co]==m+1)
- {
- cout<<H[co]<<'\n';
- exit(0);
- }
- H[co]-=x;
- h[co]-=x;
- t[d[co]]--;
- cuts--;
- while(n>0&&H[v[n]]>=H[co])
- {
- top=(top+1)&Mask;
- c[top]=v[n];
- n--;
- }
- top=(top+1)&Mask;
- c[top]=co;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement