#include <cstdlib> #include <iostream> #include <math.h> #include <vector> using namespace std; long long myMax(long long a, long long b) { if (a > b) { return a; } else { return b; } } long long calculateDelay(int bakeTime, vector<long long>* entryTimes) { long long delay = 0; long long tLast = 0; for (int i=0; i<entryTimes->size(); i++) { long long entryTime = (*entryTimes)[i]; if (tLast < myMax(entryTime - bakeTime, 0)) { tLast = entryTime - bakeTime; //cout << "Delay correction " << tLast << endl; } if (tLast < entryTime) { delay += ((tLast + bakeTime) - entryTime); //cout << "Delay 1 " << delay << endl; } else { delay += (bakeTime + (tLast - entryTime)); //cout << "Delay 2 " << delay << endl; } tLast += bakeTime; //cout << tLast << " " << delay << " " << entryTime << endl; } return delay; } int main(int argc, char** argv) { int n; int m; vector<long long> entryTimes; long long currentEntryTime; int bakeTime; cin >> n; cin >> m; for (int i=0; i<n; i++) { cin >> currentEntryTime; entryTimes.push_back(currentEntryTime); } for (int i=0; i<m; i++) { cin >> bakeTime; cout << calculateDelay(bakeTime, &entryTimes) << endl; } 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 | #include <cstdlib> #include <iostream> #include <math.h> #include <vector> using namespace std; long long myMax(long long a, long long b) { if (a > b) { return a; } else { return b; } } long long calculateDelay(int bakeTime, vector<long long>* entryTimes) { long long delay = 0; long long tLast = 0; for (int i=0; i<entryTimes->size(); i++) { long long entryTime = (*entryTimes)[i]; if (tLast < myMax(entryTime - bakeTime, 0)) { tLast = entryTime - bakeTime; //cout << "Delay correction " << tLast << endl; } if (tLast < entryTime) { delay += ((tLast + bakeTime) - entryTime); //cout << "Delay 1 " << delay << endl; } else { delay += (bakeTime + (tLast - entryTime)); //cout << "Delay 2 " << delay << endl; } tLast += bakeTime; //cout << tLast << " " << delay << " " << entryTime << endl; } return delay; } int main(int argc, char** argv) { int n; int m; vector<long long> entryTimes; long long currentEntryTime; int bakeTime; cin >> n; cin >> m; for (int i=0; i<n; i++) { cin >> currentEntryTime; entryTimes.push_back(currentEntryTime); } for (int i=0; i<m; i++) { cin >> bakeTime; cout << calculateDelay(bakeTime, &entryTimes) << endl; } return 0; } |