#include <iostream> using namespace std; struct grass{ int growth; long long height; grass(int g) : growth(g), height(0) {} inline void grow(int days){ height += growth * days; } inline int trim(int h){ if(h >= height) return 0; int diff = height - h; height = h; return diff; } }; int main(){ ios_base::sync_with_stdio(0); int n, m, a, day, height, deltaDay, lastDay, trimmedMass; lastDay = 0; cin >> n >> m; grass** field = new grass*[n]; for (int i = 0; i < n; ++i){ cin >> a; field[i] = new grass(a); } for(int i = 0; i < m; ++i){ trimmedMass = 0; cin >> day >> height; deltaDay = day - lastDay; lastDay = day; for(int j = 0; j < n; ++j){ field[j]->grow(deltaDay); trimmedMass += field[j]->trim(height); } cout << trimmedMass << endl; } }
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 | #include <iostream> using namespace std; struct grass{ int growth; long long height; grass(int g) : growth(g), height(0) {} inline void grow(int days){ height += growth * days; } inline int trim(int h){ if(h >= height) return 0; int diff = height - h; height = h; return diff; } }; int main(){ ios_base::sync_with_stdio(0); int n, m, a, day, height, deltaDay, lastDay, trimmedMass; lastDay = 0; cin >> n >> m; grass** field = new grass*[n]; for (int i = 0; i < n; ++i){ cin >> a; field[i] = new grass(a); } for(int i = 0; i < m; ++i){ trimmedMass = 0; cin >> day >> height; deltaDay = day - lastDay; lastDay = day; for(int j = 0; j < n; ++j){ field[j]->grow(deltaDay); trimmedMass += field[j]->trim(height); } cout << trimmedMass << endl; } } |