D. Transferring Pyramid
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Vasya and Petya are using an interesting data storing structure: a pyramid.

The pyramid consists of n rows, the i-th row contains i cells. Each row is shifted half a cell to the left relative to the previous row. The cells are numbered by integers from 1 to  as shown on the picture below.

An example of a pyramid at n = 5 is:

This data structure can perform operations of two types:

  1. Change the value of a specific cell. It is described by three integers: "tiv", where t = 1 (the type of operation), i — the number of the cell to change and v the value to assign to the cell.
  2. Change the value of some subpyramid. The picture shows a highlighted subpyramid with the top in cell 5. It is described by s + 2numbers: "tiv1 v2 ... vs", where t = 2, i — the number of the top cell of the pyramid, s — the size of the subpyramid (the number of cells it has), vj — the value you should assign to the j-th cell of the subpyramid.

Formally: a subpyramid with top at the i-th cell of the k-th row (the 5-th cell is the second cell of the third row) will contain cells from rows from k to n, the (k + p)-th row contains cells from the i-th to the (i + p)-th (0 ≤ p ≤ n - k).

Vasya and Petya had two identical pyramids. Vasya changed some cells in his pyramid and he now wants to send his changes to Petya. For that, he wants to find a sequence of operations at which Petya can repeat all Vasya's changes. Among all possible sequences, Vasya has to pick the minimum one (the one that contains the fewest numbers).

You have a pyramid of n rows with k changed cells. Find the sequence of operations which result in each of the k changed cells being changed by at least one operation. Among all the possible sequences pick the one that contains the fewest numbers.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 105).

The next k lines contain the coordinates of the modified cells ri and ci (1 ≤ ci ≤ ri ≤ n) — the row and the cell's number in the row. All cells are distinct.

Output

Print a single number showing how many numbers the final sequence has.

Examples
input
4 5
3 1
3 3
4 1
4 3
4 4
output
10
input
7 11
2 2
3 1
4 3
5 1
5 2
5 5
6 4
7 2
7 3
7 4
7 5
output
26
Note

One of the possible solutions of the first sample consists of two operations:

2 4 v4 v7 v8

2 6 v6 v9 v10

The picture shows the changed cells color-highlighted. The subpyramid used by the first operation is highlighted blue and the subpyramid used by the first operation is highlighted yellow:

————————————————————————

这道题的中文题解似乎没有,贡献一篇

终于体现了板子的优越性【?】

这样的话我们就可以愉快的做完这道题

不过写起来真的超。多。坑。

建议大家写一写,跳一跳坑,有助于代码能力的成长

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#define inf 0x7f7f7f7f
//#define ivorysi
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
using namespace std;
int n,k;
vector<int> c[];
int ans;
int f[];
int sum[],cost[];//数组不要开小qwq
void init() {
scanf("%d %d",&n,&k);
int a,b;
siji(i,,k) {
scanf("%d%d",&a,&b);
c[b].push_back(n-a+);
}
ans=*k;
}
void solve() {
init();
int t;
siji(i,,n) {
t=min(n-i+,);
memset(sum,,sizeof(sum));
cost[]=f[];
xiaosiji(z,,c[i].size()) {if(c[i][z]<=t)sum[c[i][z]]=;}
//因为我们枚举只到了(6k)^0.5,所以这以后的都不再统计,防止爆数组
siji(z,,t+) sum[z]+=sum[z-];
siji(z,,t) cost[z]=max(cost[z-],f[z]);
if(i==) {
siji(z,,t+) f[z]=-inf;
}
xiaosiji(j,,t) {
if(j==) {
f[j]=max(f[j+],cost[]-)+*sum[j+];
f[j]=max(f[j],cost[]);//0是我们选择不在这一列放的唯一途径
}
else {
f[j]=max(f[j+],cost[j]-(j+)*(j+)/-)+*sum[j+];
}
} }
printf("%d\n",ans-f[]);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}

codeforces 354 D. Transferring Pyramid的更多相关文章

  1. codeforces 354 DIV2

    B - Pyramid of Glasses n层杯子,问k分钟能流满多少个杯子?和到香槟一样的过程? 思路:应为水的流速为每分钟一立方体(YY),可以做个转化,把最上层的杯子最原始的容积看成K,每个 ...

  2. codeforces 354 div2 C Vasya and String 前缀和

    C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. Codeforces Round #354 (Div. 2) B. Pyramid of Glasses 模拟

    B. Pyramid of Glasses 题目连接: http://www.codeforces.com/contest/676/problem/B Description Mary has jus ...

  4. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  5. Codeforces Round #354 (Div. 2)-B

    B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...

  6. Codeforces Round #354 (Div. 2)

    贪心 A Nicholas and Permutation #include <bits/stdc++.h> typedef long long ll; const int N = 1e5 ...

  7. Codeforces Round #354 (Div. 2)-D

    D. Theseus and labyrinth 题目链接:http://codeforces.com/contest/676/problem/D Theseus has just arrived t ...

  8. Codeforces Round #354 (Div. 2)-C

    C. Vasya and String 题目链接:http://codeforces.com/contest/676/problem/C High school student Vasya got a ...

  9. Codeforces Round #354 (Div. 2)-A

    A. Nicholas and Permutation 题目链接:http://codeforces.com/contest/676/problem/A Nicholas has an array a ...

随机推荐

  1. Hadoop生态圈-hbase常用命令

    Hadoop生态圈-hbase常用命令 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  2. gitlab的备份与恢复与迁移

    一.gitlab的备份1.1 创建备份目录,并授权 1 2 3 4 [root@linux-node1 ~]# mkdir /data/backups/gitlab -p [root@linux-no ...

  3. 内存操作函数memmove,memcpy,memset

    通过字符串的学习,我们知道字符串操作函数的操作对象是字符串,并且它的结束标志是结束符\0,当然这个说的是不 受限制的字符串函数.然而当我们想要将一段内存的数据复制到另一块内存时,我们不能使用字符串操作 ...

  4. 设计模式之————依赖注入(Dependency Injection)与控制反转(Inversion of Controller)

    参考链接: 依赖注入(DI) or 控制反转(IoC) laravel 学习笔记 —— 神奇的服务容器 PHP 依赖注入,从此不再考虑加载顺序 名词解释 IoC(Inversion of Contro ...

  5. zookeeper系列之:zookeeper简介浅谈

    一.zookeeper的定义 打开zookeeper官网,赫然一行大字,写着:“Apache ZooKeeper致力于开发和维护实现高度可靠的分布式协调的开源服务器”.什么意思呢?就是Apache Z ...

  6. The Ph.D. Grind

    The Ph.D. Grind A Ph.D. Student Memoir Summary The Ph.D. Grind, a 122-page e-book, is the first know ...

  7. spring-boot RestTemplate 连接池

    以前我们项目都是基于Apache HttpClient 连接池进行web 接口调用,后来用spring-boot, 发现 RestTemplate 挺好用. 简单介绍下: 什么是RestTemplat ...

  8. 巧妙使用CSS创建可以打印的页面

    用CSS创建打印页面,不必为打印而专门建立一个HTML文件,可以节省一些体力,其前提是按“WEB标准”用CSS+DIV布局HTML页面. 第一.在HTML页面加入为打印机设置的CSS文件 <li ...

  9. zabbix lld使用trapper方式(zabbix_sender)

    自动发现脚本文件输出格式: { "data": [ { "{#BIND_PERF}": "BIND INCOMING QUERY" }, { ...

  10. sql_injection之基本get注入

    1.代码篇 <?php error_reporting(0); include("../conn.php"); if(isset($_GET['id'])){ $id=$_G ...