Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. using namespace std;
  5.  
  6. #define pb push_back
  7. const int MAXN = 2 * 1e5 + 10;
  8.  
  9. int n, m, k[MAXN], sum = 0;
  10. vector<int> discount[MAXN];
  11.  
  12. int lastDis[MAXN * 3];
  13. bool check(int d){
  14.     fill(lastDis, lastDis + d, 0);
  15.     for(int i = 1; i <=n; i++){
  16.         if(discount[i].size()==0)continue;
  17.         int l = 0, r = discount[i].size(), m;
  18.         while(r-l > 1){
  19.             m = (l+r)/2;
  20.             if(discount[i][m] <= d)l = m;
  21.             else r = m;
  22.         }
  23.         lastDis[discount[i][l]] += k[i];
  24.     }
  25.    
  26.     int money = 0, left = sum;
  27.     for(int i = 1; i <= d; i++){
  28.         money++;
  29.         if(money <= lastDis[i]){
  30.             left -= money;
  31.             money = 0;
  32.         }else{
  33.             left -= lastDis[i];
  34.             money -= lastDis[i];
  35.         }
  36.     }
  37.     if(money >= 2 * left)return true;
  38.     return false;
  39. }
  40.  
  41. int main(){
  42.     cin>>n>>m;
  43.     for(int i = 1; i <= n; i++){
  44.         cin>>k[i];
  45.         sum += k[i];
  46.     }
  47.     int ti, di;
  48.     for(int i = 0; i < m; i++){
  49.         cin>>di>>ti;
  50.         discount[ti].pb(di);
  51.     }
  52.     for(int i = 1; i <= n; i++){
  53.         sort(discount[i].begin(), discount[i].end());
  54.     }
  55.     int l = sum, r = 2 * sum, m;
  56.     while(l != r){
  57.         m = (l + r) / 2;
  58.         if(check(m))r = m;
  59.         else l = m + 1;
  60.     }
  61.  
  62.     cout<<l;
  63.    
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement