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));
}

最新文章

  1. bash获取properties文件资源
  2. Microsoft Visual Stduio 2005 Ent安装报错解决方法
  3. 第二章:javascript: 数组
  4. bc.34.B.Building Blocks(贪心)
  5. JavaScript入门基础
  6. IntelliJ IDEA 14
  7. Find modern, interactive web-based charts for R at the htmlwidgets gallery
  8. Python教程(2.6)——list和tuple简介
  9. php后台模板html拼接写法
  10. 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
  11. 201421123042 《Java程序设计》第3周学习总结
  12. Shell 全局变量、环境变量和局部变量
  13. python continue的应用
  14. [CentOS_7.4]Linux编译安装ffmpeg
  15. java调用第三方的webservice应用实例
  16. 图灵数学&#183;统计学丛书.PDF(53本全)
  17. 20155326 实验四 Android程序设计实验报告
  18. Android Studio Xposed模块编写(二)
  19. RF的特征子集选取策略(spark ml)
  20. Java中的正则表达式Pattern与Matcher

热门文章

  1. Nginx技术研究系列7-Azure环境中Nginx高可用性和部署架构设计
  2. 什么是软件开发工具包(SDK)
  3. Linux基础命令---lpq查看打印队列
  4. (转) SpringMVC学习笔记-
  5. Tensorflow serving with Kubernetes
  6. MVC开发模式的数据运行流程
  7. vue-i18n使用ES6语法以及空格换行问题
  8. No Directionality widget found
  9. mysql 主键外键
  10. ukylin 使用deepin-wine