[Timue: 1019. Line Painting]
原题地址:[Timue: 1019. Line Painting]
不知道该怎么解比较好,于是选择去模拟这个过程:
搞了一个map去存储住这些区间,并且在添加和删除时把前后有相交的区间处理一下,合并还是切割还是怎么样。
#include <iostream> #include <map> #include <algorithm> using namespace std; int main() { map<int, pair<int, bool>> A; A[0] = make_pair(999999999, false); int N; cin >> N; for (int i = 0; i < N; ++i) { int x, y; char z; cin >> x >> y >> z; --y; bool c = z == 'b'; decltype(A)::iterator iter; while ((iter = A.lower_bound(x)) != A.end() && iter->first <= y) { if (iter->second.first > y) { if (iter->second.second == c) { y = iter->second.first; } else { A[y + 1] = make_pair(iter->second.first, !c); } } A.erase(iter); } iter = A.lower_bound(x); bool add = true; if (iter != A.begin()) { --iter; if (iter->second.first >= x) { if (iter->second.second == c) { iter->second.first = max(y, iter->second.first); add = false; } else { if (iter->second.first > y) { A[y + 1] = make_pair(iter->second.first, !c); } iter->second.first = x - 1; } } } if (add) { A[x] = make_pair(y, c); } } pair<int, int> ans(0, -1); pair<int, int> current; bool continued = false; for (auto &x : A) { if (x.second.second) { continued = false; } else { if (!continued) { current.first = x.first; current.second = x.second.first; } else { current.second = x.second.first; } if (current.second - current.first > ans.second - ans.first) { ans = current; } continued = true; } } cout << ans.first << " " << ans.second + 1<< endl; return 0; }

浙公网安备 33010602011771号