[Codeforces771E]Bear and Rectangle Strips
2024-10-12 18:33:49
Problem
给你一个2*n的矩阵,要求你用补充叠的矩阵去框,要求每个矩阵框中的数之和为0,问最多可以用几个矩阵。
Solution
首先预处理出一个点到离它最近的一段和为0的区间的左端点
然后到这往前用记忆化搜索的方式DP就可以了
Notice
注意要记忆化
Code
#include<map>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 300000;
const double eps = 1e-6, phi = acos(-1);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
map<ll, int> Derec;
map<pair<int, int>, int> F;
int Pre[3][N + 5];
ll T[3][N + 5];
int Solve(int n, int m)
{
if (!~Pre[1][n] && !~Pre[2][m] && !~Pre[0][min(n, m)]) return 0;
pair<int, int> P(n, m);
if (F.count(P)) return F[P];
int ans = 0;
if (Pre[1][n] > Pre[2][m]) ans = max(ans, Solve(Pre[1][n], m) + 1);
else if (~Pre[2][m])ans = max(ans, Solve(n, Pre[2][m]) + 1);
if (~Pre[0][min(n, m)]) ans = max(ans, Solve(Pre[0][min(n, m)], Pre[0][min(n, m)]) + 1);
return F[P] = ans;
}
int sqz()
{
int n = read();
rep(i, 1, n) T[0][i] += (T[1][i] = read());
rep(i, 1, n) T[0][i] += (T[2][i] = read());
rep(i, 0, 2)
rep(j, 1, n) T[i][j] = T[i][j - 1] + T[i][j];
rep(i, 0, 2)
{
Derec.clear(); Derec[0] = 0;
Pre[i][0] = -1;
rep(j, 1, n)
{
Pre[i][j] = max(Pre[i][j - 1], Derec.count(T[i][j]) ? Derec[T[i][j]] : -1);
Derec[T[i][j]] = j;
}
}
printf("%d\n", Solve(n, n));
}
最新文章
- bash获取properties文件资源
- Microsoft Visual Stduio 2005 Ent安装报错解决方法
- 第二章:javascript: 数组
- bc.34.B.Building Blocks(贪心)
- JavaScript入门基础
- IntelliJ IDEA 14
- Find modern, interactive web-based charts for R at the htmlwidgets gallery
- Python教程(2.6)——list和tuple简介
- php后台模板html拼接写法
- Caused by: java.sql.SQLException: Couldn&#39;t perform the operation getAutoCommit: You can&#39;t perform any operations on this connection. It has been automatically closed by Proxool for some reason (see lo
- 201421123042 《Java程序设计》第3周学习总结
- Shell 全局变量、环境变量和局部变量
- python continue的应用
- [CentOS_7.4]Linux编译安装ffmpeg
- java调用第三方的webservice应用实例
- 图灵数学&#183;统计学丛书.PDF(53本全)
- 20155326 实验四 Android程序设计实验报告
- Android Studio Xposed模块编写(二)
- RF的特征子集选取策略(spark ml)
- Java中的正则表达式Pattern与Matcher