// Krzysztof Baranski #include <cstdio> #include <vector> #include <algorithm> #include <functional> using namespace std; void print(long long n) { if(n < 0) { printf("-"); print(-n); return; } else if(n == 0) { printf("0\n"); return; } int buff[50]; int i = 0; while(n > 0) { buff[i++] = n%10; n /= 10; } while(i) { printf("%d", buff[--i]); } printf("\n"); } int main() { int n; // liczba arow int m; // liczba koszen scanf("%d %d", &n, &m); vector<int> field; for(int i=0; i<n; ++i) { int a; scanf("%d", &a); field.push_back(a); } sort(field.begin(), field.end(), greater<int>()); vector<long long> hay; hay.push_back(0); for(int i=0; i<n; ++i) { hay.push_back( hay[i] + ((long long)field[i]) ); } long long mown = 0; while(m--) { long long d,b; scanf("%lld %lld", &d, &b); // bin-search long long j, begin = 0, end = n; while(begin < end) { j = (begin + end) >> 1; if( d * ((long long)field[j]) <= b ) { end = j; } else { begin = j + 1; } } j = begin; long long whole = (d)*(hay[j]); long long rect = j * b; long long diff = whole - rect; long long result = diff - mown; result = (result < 0 ? 0 : result); mown = mown + result; print(result); } }
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | // Krzysztof Baranski #include <cstdio> #include <vector> #include <algorithm> #include <functional> using namespace std; void print(long long n) { if(n < 0) { printf("-"); print(-n); return; } else if(n == 0) { printf("0\n"); return; } int buff[50]; int i = 0; while(n > 0) { buff[i++] = n%10; n /= 10; } while(i) { printf("%d", buff[--i]); } printf("\n"); } int main() { int n; // liczba arow int m; // liczba koszen scanf("%d %d", &n, &m); vector<int> field; for(int i=0; i<n; ++i) { int a; scanf("%d", &a); field.push_back(a); } sort(field.begin(), field.end(), greater<int>()); vector<long long> hay; hay.push_back(0); for(int i=0; i<n; ++i) { hay.push_back( hay[i] + ((long long)field[i]) ); } long long mown = 0; while(m--) { long long d,b; scanf("%lld %lld", &d, &b); // bin-search long long j, begin = 0, end = n; while(begin < end) { j = (begin + end) >> 1; if( d * ((long long)field[j]) <= b ) { end = j; } else { begin = j + 1; } } j = begin; long long whole = (d)*(hay[j]); long long rect = j * b; long long diff = whole - rect; long long result = diff - mown; result = (result < 0 ? 0 : result); mown = mown + result; print(result); } } |