Advertisement
a53

waterfront

a53
Jan 4th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int N = 10005;
  5. const int Mask=(1<<14)-1;
  6. int n,m,k,x,h[N],g[N],H[N],v[N],d[N],t[N],c[Mask+1],top,bottom;
  7. int cuts;
  8. bool crit(int,int);
  9. void readAndInit(),taie();
  10. int main()
  11. {
  12. readAndInit();
  13. while(1)
  14. taie();
  15. return 0;
  16. }
  17. /// criteriu de sortare
  18. bool crit(int i,int j)
  19. {
  20. return H[i]<H[j];
  21. }
  22. /// citire si initializare
  23.  
  24. void readAndInit()
  25. {
  26. cin>>n>>m>>k>>x;
  27. for(int i=1;i<=n;i++)
  28. {
  29. cin>>h[i]>>g[i];
  30. H[i]=h[i]+m*g[i];
  31. v[i]=i;
  32. }
  33. for(int i=1;i<=m;i++)
  34. t[i]=k;
  35. cuts=m*k;
  36. t[m+1]=1;
  37. sort(v+1,v+n+1,crit);
  38. c[0]=v[n--];
  39. top=bottom=0;
  40. }
  41. void taie()
  42. {
  43. int co=c[bottom];bottom=(bottom+1)&Mask;
  44. if(h[co]<x)
  45. {
  46. int days=(x-h[co])/g[co];
  47. if((x-h[co])%g[co]) days++;
  48. if(d[co]+days>m)
  49. {
  50. cout<<H[co]<<'\n';
  51. exit(0);
  52. }
  53. d[co]+=days;
  54. h[co]+=days*g[co];
  55. }
  56. while(!t[d[co]]){d[co]++;h[co]+=g[co];}
  57. if(d[co]==m+1)
  58. {
  59. cout<<H[co]<<'\n';
  60. exit(0);
  61. }
  62. H[co]-=x;
  63. h[co]-=x;
  64. t[d[co]]--;
  65. cuts--;
  66. while(n>0&&H[v[n]]>=H[co])
  67. {
  68. top=(top+1)&Mask;
  69. c[top]=v[n];
  70. n--;
  71. }
  72. top=(top+1)&Mask;
  73. c[top]=co;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement