#include <cstdio> #include <iostream> #include <cmath> #include <string> #include <sstream> #include <vector> #include <cstdlib> #include <algorithm> #include <map> using namespace std; vector<long> sort_indexes(const vector<long long> &v) { // initialize original index locations vector<long> idx(v.size()); for (long i = 0; i<idx.size(); i++) { idx[i] = i; } // sort indexes based on comparing values in v sort(idx.begin(), idx.end(), [&v](long i1, long i2) {return v[i1] < v[i2];}); return idx; } int main() { long n,m; scanf("%ld %ld\n", &n, &m); long long* t = new long long[n]; long long* dt = new long long[n+1]; vector<long long> d,ds; map<long, long long> results; for (long i=0; i<n; i++) { scanf("%lld\n", &t[i]); if (i == 0) { dt[i] = t[i]; } else { dt[i] = t[i] - t[i-1]; } //printf("%lld", t[i]); } for (long i=0; i<m; i++) { long long dtmp; scanf("%lld\n", &dtmp); d.push_back(dtmp); ds.push_back(dtmp); // printf("%lld", d[i]); } vector<long> sort_idx = sort_indexes(d); sort(ds.begin(), ds.end()); // for (long i=0; i < sort_idx.size(); i++) { // printf("%lld: %lld, %ld\n", d[i], ds[i], sort_idx[i]); // } long long prev_x = -1; long long prev_s = -1; for (long i=0; i<m; i++) { long long x = ds[i]; if (x == prev_x) { results[sort_idx[i]] = prev_s; } else{ long long r = 0, rn = 0; long long s = 0; for (long j = 0; j < n; j++) { rn = r + x - dt[j]; if (rn > 0) { r = rn; s += r; } else { r = 0; } } prev_s = s; results[sort_idx[i]] = s; } } for (long i=0; i<m; i++) { printf("%lld\n", results[i]); } 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #include <cstdio> #include <iostream> #include <cmath> #include <string> #include <sstream> #include <vector> #include <cstdlib> #include <algorithm> #include <map> using namespace std; vector<long> sort_indexes(const vector<long long> &v) { // initialize original index locations vector<long> idx(v.size()); for (long i = 0; i<idx.size(); i++) { idx[i] = i; } // sort indexes based on comparing values in v sort(idx.begin(), idx.end(), [&v](long i1, long i2) {return v[i1] < v[i2];}); return idx; } int main() { long n,m; scanf("%ld %ld\n", &n, &m); long long* t = new long long[n]; long long* dt = new long long[n+1]; vector<long long> d,ds; map<long, long long> results; for (long i=0; i<n; i++) { scanf("%lld\n", &t[i]); if (i == 0) { dt[i] = t[i]; } else { dt[i] = t[i] - t[i-1]; } //printf("%lld", t[i]); } for (long i=0; i<m; i++) { long long dtmp; scanf("%lld\n", &dtmp); d.push_back(dtmp); ds.push_back(dtmp); // printf("%lld", d[i]); } vector<long> sort_idx = sort_indexes(d); sort(ds.begin(), ds.end()); // for (long i=0; i < sort_idx.size(); i++) { // printf("%lld: %lld, %ld\n", d[i], ds[i], sort_idx[i]); // } long long prev_x = -1; long long prev_s = -1; for (long i=0; i<m; i++) { long long x = ds[i]; if (x == prev_x) { results[sort_idx[i]] = prev_s; } else{ long long r = 0, rn = 0; long long s = 0; for (long j = 0; j < n; j++) { rn = r + x - dt[j]; if (rn > 0) { r = rn; s += r; } else { r = 0; } } prev_s = s; results[sort_idx[i]] = s; } } for (long i=0; i<m; i++) { printf("%lld\n", results[i]); } return 0; } |