POI2008KUP-Plot purchase
POI #Year2008 #贪心 #最大子矩形 #单调栈
先把所有的 的 先标记,然后考虑不包含这些点的最大子矩形
如果这个子矩形的 那么必然存在一个合法矩形,可以这样构造
- 如果当前行的 那么删除 这一行
- 否则当前行合法,输出
// Author: xiaruize const int INF = 0x3f3f3f3f3f3f3f3f; const int MOD = 1000000007; const int N = 2e3 + 10; int k, n; int a[N][N]; int sum[N][N]; int up[N]; int top = 0, st[N]; int get(int x, int y, int xx, int yy) { return sum[xx][yy] - sum[x - 1][yy] - sum[xx][y - 1] + sum[x - 1][y - 1]; } void solve() { cin >> k >> n; rep(i, 1, n) { rep(j, 1, n) { cin >> a[i][j]; sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j]; } } rep(i, 1, n) { rep(j, 1, n + 1) { if (a[i][j] <= 2 * k && j <= n) { up[j]++; if (a[i][j] >= k) { cout << j << ' ' << i << ' ' << j << ' ' << i << endl; return; } } else up[j] = 0; while (top && up[st[top]] >= up[j]) { int y = st[top - 1] + 1, yy = j - 1; int x = i - up[st[top]] + 1; debug(x, y, i, yy); top--; if (get(x, y, i, yy) >= k) { while (get(x, y, i, yy) > 2 * k) { if (get(x + 1, y, i, yy) > 2 * k) x++; else { cout << y << ' ' << x << ' ' << yy << ' ' << x << endl; return; } } cout << y << ' ' << x << ' ' << yy << ' ' << i << endl; return; } } if (j <= n) st[++top] = j; } } cout << "NIE" << endl; } #ifndef ONLINE_JUDGE bool end_of_memory_use; #endif signed main() { // freopen(".in","r",stdin); // freopen(".out","w",stdout); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int testcase = 1; // cin >> testcase; while (testcase--) solve(); #ifndef ONLINE_JUDGE cerr << "Memory use:" << (&end_of_memory_use - &start_of_memory_use) / 1024.0 / 1024.0 << "MiB" << endl; cerr << "Time use:" << (double)clock() / CLOCKS_PER_SEC * 1000.0 << "ms" << endl; #endif return 0; }
本文作者:xiaruize's Blog
本文链接:https://www.cnblogs.com/xiaruize/p/18136784
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步