#include <cstdio>
#include <cstdint>
#include <vector>
#include <bitset>
#include <tuple>
#include <algorithm>
#include <climits>
#include <numeric>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;
map<int, long long> bestHeightPerColor;
map<long long, set<int>> colorsAchievableFromHeight;
map<int, set<long long>> heightsAchievebleFromColor;
priority_queue<long long> topHeights;
int n, c;
map<int, set<int>> inputData;//height -> list of colors
vector<pair<int,int>> inputDataFlat; //height, color
long long findBestHeightOfOtherColor(int color)
{
queue<long long> comeBacks;
long long result = -1;
while(!topHeights.empty() && result == -1)
{
long long topHeightOtherColor = topHeights.top();
int possibilitiesToAchieve = colorsAchievableFromHeight[topHeightOtherColor].size();
topHeights.pop();
if(possibilitiesToAchieve == 0)
{
continue;
}
if(colorsAchievableFromHeight[topHeightOtherColor].find(color) != colorsAchievableFromHeight[topHeightOtherColor].end())
{
if (possibilitiesToAchieve > 1)
{
result = topHeightOtherColor;
}
}
else
{
result = topHeightOtherColor;
}
comeBacks.push(topHeightOtherColor);
}
while (!comeBacks.empty())
{
long long comeBack = comeBacks.front();
topHeights.push(comeBack);
comeBacks.pop();
}
return result;
}
long long findUpdatedHeightForColor(int color, long long additionalHeight)
{
long long result = bestHeightPerColor[color] + additionalHeight;
long long newCandidate = additionalHeight + findBestHeightOfOtherColor(color) - c;
//cout<<"condidering :"<<additionalHeight<<" "<<color<<endl;
//cout<<"best continuation: "<<result<<endl;
//cout<<"best z kara: "<<newCandidate<<endl;
result = max(result, newCandidate);
return result;
}
void updateHeightForColor(int color, long long newHeight)
{
for(auto itColor = heightsAchievebleFromColor[color].begin(); itColor!=heightsAchievebleFromColor[color].end(); ++itColor)
{
long long achievableHeight = (*itColor);
colorsAchievableFromHeight[achievableHeight].erase(color);
}
heightsAchievebleFromColor[color].clear();
bestHeightPerColor[color] = newHeight;
heightsAchievebleFromColor[color].insert(newHeight);
colorsAchievableFromHeight[newHeight].insert(color);
topHeights.push(newHeight);
}
void init()
{
scanf("%d %d", &n, &c);
int lastA = -1;
topHeights.push(0);
for (int i = 0; i < n; i++)
{
int a, w;
scanf("%d %d", &a, &w);
inputData[a].insert(w);
bestHeightPerColor[w] = 0;
heightsAchievebleFromColor[w].insert(0);
colorsAchievableFromHeight[0].insert(w);
}
for (auto itA = inputData.begin(); itA!=inputData.end(); ++itA ) {
int inputHeight = itA->first;
map <int, long long>colorToNewValue;
for(auto itW = itA->second.begin(); itW!= itA->second.end(); ++itW)
{
int inputColor = *(itW);
long long newValue = findUpdatedHeightForColor(inputColor, inputHeight);
colorToNewValue[inputColor] = newValue;
}
for(auto it = colorToNewValue.begin(); it != colorToNewValue.end(); ++it)
{
updateHeightForColor(it->first, it->second);
}
}
}
int main()
{
init();
long long result = topHeights.top();
printf("%lld\n", 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 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 | #include <cstdio> #include <cstdint> #include <vector> #include <bitset> #include <tuple> #include <algorithm> #include <climits> #include <numeric> #include <iostream> #include <cstring> #include <iomanip> #include <cmath> #include <map> #include <set> #include <queue> using namespace std; map<int, long long> bestHeightPerColor; map<long long, set<int>> colorsAchievableFromHeight; map<int, set<long long>> heightsAchievebleFromColor; priority_queue<long long> topHeights; int n, c; map<int, set<int>> inputData;//height -> list of colors vector<pair<int,int>> inputDataFlat; //height, color long long findBestHeightOfOtherColor(int color) { queue<long long> comeBacks; long long result = -1; while(!topHeights.empty() && result == -1) { long long topHeightOtherColor = topHeights.top(); int possibilitiesToAchieve = colorsAchievableFromHeight[topHeightOtherColor].size(); topHeights.pop(); if(possibilitiesToAchieve == 0) { continue; } if(colorsAchievableFromHeight[topHeightOtherColor].find(color) != colorsAchievableFromHeight[topHeightOtherColor].end()) { if (possibilitiesToAchieve > 1) { result = topHeightOtherColor; } } else { result = topHeightOtherColor; } comeBacks.push(topHeightOtherColor); } while (!comeBacks.empty()) { long long comeBack = comeBacks.front(); topHeights.push(comeBack); comeBacks.pop(); } return result; } long long findUpdatedHeightForColor(int color, long long additionalHeight) { long long result = bestHeightPerColor[color] + additionalHeight; long long newCandidate = additionalHeight + findBestHeightOfOtherColor(color) - c; //cout<<"condidering :"<<additionalHeight<<" "<<color<<endl; //cout<<"best continuation: "<<result<<endl; //cout<<"best z kara: "<<newCandidate<<endl; result = max(result, newCandidate); return result; } void updateHeightForColor(int color, long long newHeight) { for(auto itColor = heightsAchievebleFromColor[color].begin(); itColor!=heightsAchievebleFromColor[color].end(); ++itColor) { long long achievableHeight = (*itColor); colorsAchievableFromHeight[achievableHeight].erase(color); } heightsAchievebleFromColor[color].clear(); bestHeightPerColor[color] = newHeight; heightsAchievebleFromColor[color].insert(newHeight); colorsAchievableFromHeight[newHeight].insert(color); topHeights.push(newHeight); } void init() { scanf("%d %d", &n, &c); int lastA = -1; topHeights.push(0); for (int i = 0; i < n; i++) { int a, w; scanf("%d %d", &a, &w); inputData[a].insert(w); bestHeightPerColor[w] = 0; heightsAchievebleFromColor[w].insert(0); colorsAchievableFromHeight[0].insert(w); } for (auto itA = inputData.begin(); itA!=inputData.end(); ++itA ) { int inputHeight = itA->first; map <int, long long>colorToNewValue; for(auto itW = itA->second.begin(); itW!= itA->second.end(); ++itW) { int inputColor = *(itW); long long newValue = findUpdatedHeightForColor(inputColor, inputHeight); colorToNewValue[inputColor] = newValue; } for(auto it = colorToNewValue.begin(); it != colorToNewValue.end(); ++it) { updateHeightForColor(it->first, it->second); } } } int main() { init(); long long result = topHeights.top(); printf("%lld\n", result); } |
English