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. Java 集合类框架

    1 package test; import java.util.ArrayList; import java.util.Collection; import java.util.Date; impo ...

  2. 18 os/os.path模块中关于文件/目录常用的函数使用方法 (转)

    os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...

  3. 杨韬的Python/Jupyter学习笔记

    Python语法学习 https://zhuanlan.zhihu.com/p/24162430 Python 安装库 安装Jupyter Notebook 先安装Python cmd 进入K:\Ju ...

  4. JavaScript 作用域链其实很简单

    概念 作用域链的用途,是保证对执行环境有权访问的所有变量和函数的有序访问.其本质就是一个指向变量对象的指针列表.在js中,当某个函数被调用时,会创建一个执行环境(execution context)及 ...

  5. idea下maven下载插件慢问题

    在pom.xml中添加maven 依赖包时,我就发现不管是否用了FQ,下载速度都好慢,就1M的东西能下半天,很是苦恼,于是到网上搜资料,然后让我查到了.说是使用阿里的maven镜像就可以了.我于是亲自 ...

  6. canutils上板测试问题记录

    ltp-ddt运行can_loopback时出错 pan(1881): Must supply a file collection or a command 原因runtest/ddt/can_loo ...

  7. Docker Compose 介绍安装

    Compose介绍 Compose是一个定义和管理多容器的工具,也是一种容器编排工具,前身是Pig,使用Python语言编写.使用Compose配置文件描述多个容器应用的架构,biubiu使用什么镜像 ...

  8. Python if条件判断

    if else 判断交流 1.判断用户名密码对不对. import getpass _username = 'devin' _password = 'abc123' username = input( ...

  9. Shell 解释器初识

    1.脚本文件要以.sh结尾,第一行要跟#!/bin/bash解释器. 2.运行shell脚本. (1)添加权限:可以加x执行权限,./123.sh (2)命令执行:bash 123.sh,sh 123 ...

  10. CentOS 6.5优化开机启动服务

    使用chkconfig命令列举出所有服务,配合管道筛选出开机默认启动的服务,再去掉level0(关机).level4(无意义)和level6(重启)的显示,使结果更直观. chkconfig | gr ...