#include <iostream> #include <string> #include <map> #include <set> #include <vector> #include <algorithm> const int MAX = 500010; long long grass[MAX]; long long grassum[MAX]; int n,m; int binSearch(long long b, long long d) { int l = 0; int r = n-1; while (l < r) { int m = (l+r+1)/2; long long v = grass[m]*d; if (v <= b) r = m-1; else l = m; } return l; } int main(int argc, char **argv) { std::ios_base::sync_with_stdio(0); std::cin >> n >> m; for (int i=0;i<n;++i) std::cin >> grass[i]; std::sort(grass, grass+n); std::reverse(grass, grass+n); for (int i=1;i<=n;++i) grassum[i] = grassum[i-1] + grass[i-1]; long long sumHay = 0; for (int i=0;i<m;++i) { int d,b; std::cin >> d >> b; int j = binSearch(b, d); //std::clog << ":: " << j << std::endl; long long hay = grassum[j+1]*d - (j+1)*b; long long diff = hay - sumHay; if (diff < 0) std::cout << 0 << "\n"; else { std::cout << diff << "\n"; sumHay += diff; } } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <iostream> #include <string> #include <map> #include <set> #include <vector> #include <algorithm> const int MAX = 500010; long long grass[MAX]; long long grassum[MAX]; int n,m; int binSearch(long long b, long long d) { int l = 0; int r = n-1; while (l < r) { int m = (l+r+1)/2; long long v = grass[m]*d; if (v <= b) r = m-1; else l = m; } return l; } int main(int argc, char **argv) { std::ios_base::sync_with_stdio(0); std::cin >> n >> m; for (int i=0;i<n;++i) std::cin >> grass[i]; std::sort(grass, grass+n); std::reverse(grass, grass+n); for (int i=1;i<=n;++i) grassum[i] = grassum[i-1] + grass[i-1]; long long sumHay = 0; for (int i=0;i<m;++i) { int d,b; std::cin >> d >> b; int j = binSearch(b, d); //std::clog << ":: " << j << std::endl; long long hay = grassum[j+1]*d - (j+1)*b; long long diff = hay - sumHay; if (diff < 0) std::cout << 0 << "\n"; else { std::cout << diff << "\n"; sumHay += diff; } } return 0; } |