1. 程式人生 > >SPOJ ADAFIELD Ada and Field(STL的使用:set,multiset,map的叠代器)題解

SPOJ ADAFIELD Ada and Field(STL的使用:set,multiset,map的叠代器)題解

tdi 重復 pan eat iterator %d seed class ...

題意:n*m的方格,“0 x”表示x軸在x位置切一刀,“0 y”表示y軸在y位置切一刀,每次操作後輸出當前面積最大矩形。

思路:用set分別儲存x軸y軸分割的點,用multiset(可重復)儲存x軸y軸邊,每次輸出最大的長和最大的寬的積。題目可能重復切。multiset如果直接erase(13)會把所有的13都刪掉,如果只想刪一個則erase(multiset.find(13))。第一次知道set自帶二分...

這裏multiset也可以用map<int, int, greater<int> >,然後用叠代器指向最後一個key就行了

代碼:

#include<set
> #include<map> #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define ll long long using namespace std; const int maxn = 20000 + 10; const int seed = 131; const int MOD = 1000000000 + 7; const int INF = 0x3f3f3f3f; set<ll> x, y; multiset
<ll> lenx, leny; int main(){ int T; ll n, m, q; scanf("%d", &T); while(T--){ scanf("%lld%lld%lld", &n, &m, &q); x.clear(); y.clear(); lenx.clear(); leny.clear(); x.insert(0),x.insert(n),lenx.insert(n); y.insert(
0),y.insert(m),leny.insert(m); set<ll>::iterator it; while(q--){ ll o, p; scanf("%lld%lld", &o, &p); if(o == 0 && x.count(p) == 0){ it = x.lower_bound(p); lenx.insert(*it - p); int len = *it - *(--it); lenx.erase(lenx.find(len)); lenx.insert(p - *it); x.insert(p); } else if(o == 1 && y.count(p) == 0){ it = y.lower_bound(p); leny.insert(*it - p); int len = *it - *(--it); leny.erase(leny.find(len)); leny.insert(p - *it); y.insert(p); } it = lenx.end(); ll chang = *(--it); it = leny.end(); ll kuan = *(--it); printf("%lld\n", chang * kuan); } } return 0; }

SPOJ ADAFIELD Ada and Field(STL的使用:set,multiset,map的叠代器)題解