標籤:
題目連結:點擊開啟連結
題意:
給定n*m的矩陣,k個操作
2種操作:
1、H x 橫向在x位置切一刀
2、V y 豎直在y位置切一刀
每次操作後輸出最大的矩陣面積
思路:
因為行列是不相干的,所以只要知道每次操作後行的最大間距和列的最大間距,相乘就是最大面積
用一個set維護橫向的所有座標點,一個multiset維護橫向的間距。
每次對行操作x則在set中找到比x大的最小數 r 和比x小的最大數l ,操作前的間距dis = r-l,在multiset中刪除dis並插入r-x 和x-l。再在set中插入x
對列操作同理。
操作完後取行列各自的最大間距相乘即可,注意相乘可能爆int
#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cmath>#include <cstring>#include <set>#include <map>#include <vector>using namespace std;typedef long long ll;const int N = 105;int heheeh;template <class T>inline bool rd(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c<'0' || c>'9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1;}template <class T>inline void pt(T x) { if (x <0) { putchar('-'); x = -x; } if (x>9) pt(x / 10); putchar(x % 10 + '0');}struct node { int l, r, len;};multiset<int> h, c;set<int> hh, cc;set<int>::iterator it;multiset<int>::iterator itx, ity;int main() { int n, m, k, x; char a[5]; scanf("%d%d%d", &m, &n, &k); hh.insert(0); hh.insert(m); h.insert(m); cc.insert(0); cc.insert(n); c.insert(n); while (k-- > 0) { scanf("%s %d", a, &x); if (a[0] == 'H') { it = cc.upper_bound(x); int r = *it; int l = *(--it); c.erase(c.lower_bound(r - l)); c.insert(r - x); c.insert(x - l); cc.insert(x); } else { it = hh.upper_bound(x); int r = *it; int l = *(--it); h.erase(h.lower_bound(r - l)); h.insert(r - x); h.insert(x - l); hh.insert(x); } itx = h.end(); ity = c.end(); printf("%I64d\n", (ll)(*(--itx))*(*(--ity))); } return 0;}
Codeforces 528A Glass Carving STL類比