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

[Codeforces771E]Bear and Rectangle Strips的更多相关文章

  1. Codeforces 771E Bear and Rectangle Strips DP

    题意: 一个由大写字母组成的长度为\(n(n \leq 75)\)的字符串,每次操作可以交换相邻位置的两个字母,求最少操作多少次使字符串中不出现子串VK 分析: VK之外的字母具体是什么,我们并不关心 ...

  2. VK Cup 2017 - Round 1

    和FallDream组队瞎打一通--B两个人写的都挂了233,最后只剩下FallDream写的A和我写的C,最后我yy了个E靠谱做法结果打挂了,结束之后改了改就A了,难受. AC:AC Rank:18 ...

  3. VK Cup 2017 - Round 1 (CDE)

    771C Bear and Tree Jumps 大意: 给定树,每步能走到距离不超过$k$的任意点,记$f(s,t)$为$s$到$t$的最少步数,求$\sum\limits_{s<t}f(s, ...

  4. [LeetCode] Perfect Rectangle 完美矩形

    Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...

  5. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  6. [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  7. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  8. [LeetCode] Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...

  9. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

随机推荐

  1. nodejs 癞子麻将

    'use strict'; var _ = require('lodash'); var quick = require('quick-pomelo'); var P = quick.Promise; ...

  2. Oracle 24角色管理

    了解什么是角色 Oracle角色(role)就是一组权限(或者说是权限的集合). 用户可以给角色赋予指定的权限,然后将角色赋给相应的用户. 三种标准的角色 connect(连接角色) 拥有connec ...

  3. RocketMQ使用笔记

    apache rocketmq document : http://rocketmq.apache.org/community/ rocketmq 工具下载地址:https://github.com/ ...

  4. hdu4777 树状数组

    题意:给了n个数,然后又m次查询,询问[L,R] 内有多少个数与其他的数不互质. 解: 我们首先可以通过处理得出每个数的有效区间,LR 就是 左边L位置上的数 和他不互质, 右边R位置上的数和不互质, ...

  5. JavaWeb-----ServletConfig对象和servletContext对象

    1.ServletConfig ServletConfig:代表当前Servlet在web.xml中的配置信息 String getServletName()  -- 获取当前Servlet在web. ...

  6. C# 调用C++的dll 那些事

    之前从来没搞过C++,最近被安排的任务需要调用C++的接口,对于一个没用过 Dependency 的小白来说,原本以为像平时的Http接口那样,协议,端口一定义,方法参数一写就没事,结果踩了无数的坑. ...

  7. 转:C#中Undo/Redo的一个简易实现

    一个比较常见的改进用户体验的方案是用Redo/Undo来取代确认对话框,由于这个功能比较常用,本文简单的给了一个在C#中通过Command模式实现Redo/Undo方案的例子,以供后续查询. clas ...

  8. springMVC学习之路4-最后的征程:整合hibernate

    今天小编很开心,因为学习springMVC基础的路终于走到了尽头,也成功搭建了一个SSH框架,暗自在心里默默地开心了1秒钟. 好了,回归正题:整合Hibernate.上一节,我为大家分享我整合spri ...

  9. 论文笔记【一】Chinese NER Using Lattice LSTM

    论文:Chinese NER Using Lattice LSTM 论文链接:https://arxiv.org/abs/1805.02023 论文作者:Yue Zhang∗and Jie Yang∗ ...

  10. 【数据使用】3k水稻数据库现成SNP的使用

    ---恢复内容开始--- 我们经常说幻想着使用已有数据发表高分文章,的确,这样的童话故事每天都在发生,但如何走出第一步我们很多小伙伴不清楚,那么我们就从水稻SNP数据库的使用来讲起. http://s ...