[Codeforces771E]Bear and Rectangle Strips
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的更多相关文章
- Codeforces 771E Bear and Rectangle Strips DP
题意: 一个由大写字母组成的长度为\(n(n \leq 75)\)的字符串,每次操作可以交换相邻位置的两个字母,求最少操作多少次使字符串中不出现子串VK 分析: VK之外的字母具体是什么,我们并不关心 ...
- VK Cup 2017 - Round 1
和FallDream组队瞎打一通--B两个人写的都挂了233,最后只剩下FallDream写的A和我写的C,最后我yy了个E靠谱做法结果打挂了,结束之后改了改就A了,难受. AC:AC Rank:18 ...
- VK Cup 2017 - Round 1 (CDE)
771C Bear and Tree Jumps 大意: 给定树,每步能走到距离不超过$k$的任意点,记$f(s,t)$为$s$到$t$的最少步数,求$\sum\limits_{s<t}f(s, ...
- [LeetCode] Perfect Rectangle 完美矩形
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- [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 ...
- [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 ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- [LeetCode] Maximal Rectangle 最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
随机推荐
- 把vim插入状态的光标改为竖线
和终端有关系,如果是Konsole的终端,把下面两行加到.vimrc文件里就可以 let &t_SI = "\<Esc>]50;CursorShape=1\x7" ...
- ECMAScript6 入门教程 初学记录let命令 块级作用域
一.基本语法-let命令 (1)ES6新增了let命令,用来声明变量.所声明的变量,只在let命令所在的代码块内有效. 循环的计数器,就很合适使用let命令.计数器i只在for循环体内有效,在循环体外 ...
- html5 javascript 事件练习2
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- Nginx技术研究系列4-Nginx监控-Nginx+Telegraf+Influxb+Grafana
搭建了Nginx集群后,需要继续深入研究的就是日常Nginx监控. Nginx如何监控?相信百度就可以找到:nginx-status 通过Nginx-status,实时获取到Nginx监控数据后,如何 ...
- Oracle expdp impdp中 exclude/include 的使用
exclude和include参数能够在使用expdp或impdp是对特定的对象或对象类型进行筛选或过滤.比如因工作的需要导出特定的表或不导出特定的表.视图以及存储过程.索引.约束.授权统计信息等等. ...
- Hadoop -- 概念
hadoop 一个开源的,稳定的,可扩展的,分布式的计算框架 实现单一的服务器到成千上万机器之间共同处理数据的一个可扩展性框架 Hadoop 项目主要包含以下四个模块 Hadoop Common:为其 ...
- GoldenGate BR(bounded Recovery)简单说明
背景 Oracle数据库的在线日志包含已提交的和未提交的事务,但OGG只会将已提交的事务写入到队列文件.因此,针对未提交的事务,特别是未提交的长事务,OGG会怎样处理呢? 有些长事务是在批处理作业中, ...
- Html table、thead、tr、th、td 标签
Html table.thead.tr.th.td 标签 案例一 <!-- table 表格标签,配置表格使用.border="1" 添加表格框架 --> <ta ...
- 程序员的快速开发框架:Github上 10 大优秀的开源后台控制面板
程序员的快速开发框架:Github上 10 大优秀的开源后台控制面板 Web 开发中几乎的平台都需要一个后台管理,但是从零开发一套后台控制面板并不容易,幸运的是有很多开源免费的后台控制面板可以给开发者 ...
- 【BZOJ2301】Problem B
Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...