#include <iostream> #include <algorithm> #include <vector> #include <stack> using namespace std; struct Cut{ long long int day; long long int height; Cut(long long int day, long long int height) { this->day = day; this->height = height; } }; struct DoneCut{ long long int day; long long int height; int firstGrass; DoneCut(long long int day, long long int height,int firstGrass) { this->day = day; this->height = height; this->firstGrass = firstGrass; } }; int main(){ ios_base::sync_with_stdio(false); long long int area, cutsC; cin >> area >> cutsC; //tempa wzrostu traw vector<long long int> grassesGrowths; grassesGrowths.resize(area); //koszenia stack<DoneCut> doneCuts; //sumaryczne tempa wzrostu wszystkich przynajmniej tak szybkich traw // jak trawa o danym numerze vector<long long int> summaryGrowths; summaryGrowths.resize(area); std::vector<Cut> cuts; cuts.reserve(cutsC); for (int i = 0; i < area; i++){ cin >> grassesGrowths[i]; } //sortujemy trawy od najwolniej do najszybciej rosnacych sort(grassesGrowths.begin(), grassesGrowths.end()); //dodajemy na poczatek skoszenie calej trawy do poziomu 0 doneCuts.push(DoneCut(0, 0, 0)); long long int summaryGrowth = 0; for (int i = area - 1; i >= 0; --i){ summaryGrowth += grassesGrowths[i]; summaryGrowths[i] = summaryGrowth; } //Wczytujemy koszenia for (int i = 0; i < cutsC; ++i){ long long int day; cin >> day; long long int height; cin >> height; cuts.push_back(Cut(day, height)); } for (int c = 0; c < cutsC; ++c){ //ilosc trawy scietej w tym koszeniu long long int grassCut = 0; //numer ostatniej trawy + 1 dla ktorej jeszcze nie wyliczylismy obciecia long long int lastGrass = area; bool foundNotCutGrass = false; long long int notCutGrassPosition = 0; while (!foundNotCutGrass && !doneCuts.empty()){ DoneCut lastCut = doneCuts.top(); //cout << " sprawdzamy koszenie do poziomu " << lastCut.height << endl; //trzeba pamietac zeby nie dodawac koszenia ktore nie scielo zadnej trawy // poniewaz tutaj wyskoczy blad (grassesGrowths[lastCut.firstGrass]) if ((cuts[c].day - lastCut.day)*grassesGrowths[lastCut.firstGrass] >= cuts[c].height - lastCut.height) { //cout << " scieto caly obszar tego koszenia\n"; //jezeli scieto caly obszar objety poprzednim koszeniem, to trzeba wyrzucic poprzednie koszenie doneCuts.pop(); //i dodac ilosc obcietej trawy if (lastGrass < area) grassCut -= summaryGrowths[lastGrass] * (cuts[c].day - lastCut.day); grassCut += summaryGrowths[lastCut.firstGrass]*(cuts[c].day - lastCut.day) + (lastCut.height - cuts[c].height)*(lastGrass - lastCut.firstGrass); lastGrass = lastCut.firstGrass; //cout << " sumaryczna ilosc scietej trawy : " << grassCut << endl; } else { foundNotCutGrass = true; notCutGrassPosition = lastCut.firstGrass; } } long long int firstCutGrass; if (lastGrass > 0){ //trzeba przeszukac pozostaly obszar <notCutGrassPosition+1, lastGrass-1> // zeby znalezc pierwszą obciętą trawe //minimalna szybkosc wzrostu trawy przy ktorej, zostala by ona skoszona tym razem long long int minimalGrowth = static_cast<long long int>(ceil(static_cast<double>(cuts[c].height - doneCuts.top().height) / (cuts[c].day - doneCuts.top().day))); if (notCutGrassPosition >= lastGrass - 1){ firstCutGrass = lastGrass; } else { firstCutGrass = lower_bound(grassesGrowths.begin() + notCutGrassPosition + 1, grassesGrowths.begin() + lastGrass , minimalGrowth) - grassesGrowths.begin(); } if (firstCutGrass < lastGrass){ //dodajemy ilosc obcietej trawy if (lastGrass < area) grassCut -= summaryGrowths[lastGrass] * (cuts[c].day - doneCuts.top().day); grassCut += summaryGrowths[firstCutGrass] * (cuts[c].day - doneCuts.top().day) + (doneCuts.top().height - cuts[c].height)*(lastGrass - firstCutGrass); } } else firstCutGrass = 0; //cout << " firstCutGrass : " << firstCutGrass << endl; //dodajemy nowe koszenie, jezeli skoszono przynajmniej jedną trawę if (firstCutGrass < area) doneCuts.push(DoneCut(cuts[c].day, cuts[c].height, firstCutGrass)); cout << grassCut << 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 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | #include <iostream> #include <algorithm> #include <vector> #include <stack> using namespace std; struct Cut{ long long int day; long long int height; Cut(long long int day, long long int height) { this->day = day; this->height = height; } }; struct DoneCut{ long long int day; long long int height; int firstGrass; DoneCut(long long int day, long long int height,int firstGrass) { this->day = day; this->height = height; this->firstGrass = firstGrass; } }; int main(){ ios_base::sync_with_stdio(false); long long int area, cutsC; cin >> area >> cutsC; //tempa wzrostu traw vector<long long int> grassesGrowths; grassesGrowths.resize(area); //koszenia stack<DoneCut> doneCuts; //sumaryczne tempa wzrostu wszystkich przynajmniej tak szybkich traw // jak trawa o danym numerze vector<long long int> summaryGrowths; summaryGrowths.resize(area); std::vector<Cut> cuts; cuts.reserve(cutsC); for (int i = 0; i < area; i++){ cin >> grassesGrowths[i]; } //sortujemy trawy od najwolniej do najszybciej rosnacych sort(grassesGrowths.begin(), grassesGrowths.end()); //dodajemy na poczatek skoszenie calej trawy do poziomu 0 doneCuts.push(DoneCut(0, 0, 0)); long long int summaryGrowth = 0; for (int i = area - 1; i >= 0; --i){ summaryGrowth += grassesGrowths[i]; summaryGrowths[i] = summaryGrowth; } //Wczytujemy koszenia for (int i = 0; i < cutsC; ++i){ long long int day; cin >> day; long long int height; cin >> height; cuts.push_back(Cut(day, height)); } for (int c = 0; c < cutsC; ++c){ //ilosc trawy scietej w tym koszeniu long long int grassCut = 0; //numer ostatniej trawy + 1 dla ktorej jeszcze nie wyliczylismy obciecia long long int lastGrass = area; bool foundNotCutGrass = false; long long int notCutGrassPosition = 0; while (!foundNotCutGrass && !doneCuts.empty()){ DoneCut lastCut = doneCuts.top(); //cout << " sprawdzamy koszenie do poziomu " << lastCut.height << endl; //trzeba pamietac zeby nie dodawac koszenia ktore nie scielo zadnej trawy // poniewaz tutaj wyskoczy blad (grassesGrowths[lastCut.firstGrass]) if ((cuts[c].day - lastCut.day)*grassesGrowths[lastCut.firstGrass] >= cuts[c].height - lastCut.height) { //cout << " scieto caly obszar tego koszenia\n"; //jezeli scieto caly obszar objety poprzednim koszeniem, to trzeba wyrzucic poprzednie koszenie doneCuts.pop(); //i dodac ilosc obcietej trawy if (lastGrass < area) grassCut -= summaryGrowths[lastGrass] * (cuts[c].day - lastCut.day); grassCut += summaryGrowths[lastCut.firstGrass]*(cuts[c].day - lastCut.day) + (lastCut.height - cuts[c].height)*(lastGrass - lastCut.firstGrass); lastGrass = lastCut.firstGrass; //cout << " sumaryczna ilosc scietej trawy : " << grassCut << endl; } else { foundNotCutGrass = true; notCutGrassPosition = lastCut.firstGrass; } } long long int firstCutGrass; if (lastGrass > 0){ //trzeba przeszukac pozostaly obszar <notCutGrassPosition+1, lastGrass-1> // zeby znalezc pierwszą obciętą trawe //minimalna szybkosc wzrostu trawy przy ktorej, zostala by ona skoszona tym razem long long int minimalGrowth = static_cast<long long int>(ceil(static_cast<double>(cuts[c].height - doneCuts.top().height) / (cuts[c].day - doneCuts.top().day))); if (notCutGrassPosition >= lastGrass - 1){ firstCutGrass = lastGrass; } else { firstCutGrass = lower_bound(grassesGrowths.begin() + notCutGrassPosition + 1, grassesGrowths.begin() + lastGrass , minimalGrowth) - grassesGrowths.begin(); } if (firstCutGrass < lastGrass){ //dodajemy ilosc obcietej trawy if (lastGrass < area) grassCut -= summaryGrowths[lastGrass] * (cuts[c].day - doneCuts.top().day); grassCut += summaryGrowths[firstCutGrass] * (cuts[c].day - doneCuts.top().day) + (doneCuts.top().height - cuts[c].height)*(lastGrass - firstCutGrass); } } else firstCutGrass = 0; //cout << " firstCutGrass : " << firstCutGrass << endl; //dodajemy nowe koszenie, jezeli skoszono przynajmniej jedną trawę if (firstCutGrass < area) doneCuts.push(DoneCut(cuts[c].day, cuts[c].height, firstCutGrass)); cout << grassCut << endl; } } |