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. vue中import xxx from 和 import {xxx} from的区别

    1.import xxx from import FunName from ‘../xxx’ 对应js中的引用: export defualt function FunName() { return ...

  2. 51nod1019 逆序数

    1019 逆序数 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为 ...

  3. crontab定时任务_net

    2017年2月25日, 星期六 crontab定时任务 19. crontab 定时任务 通过crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell script脚本.时间间隔 ...

  4. mysql自学路线

    入门: -Head First:PHP & MySQL.Lynn Beighley -MySQL必知必会 -MySQL5.5从零开始学.刘增杰 -MYSQL完全手册 (the Complete ...

  5. [转载]function与感叹号

    http://swordair.com/function-and-exclamation-mark/ 最近有空可以让我静下心来看看各种代码,function与感叹号的频繁出现,让我回想起2个月前我回杭 ...

  6. 升级lamp中php5.6到php7.0过程

    升级过程我就直接摘录博友,http://www.tangshuang.net/1765.html,几乎问题和解决办法都是参照他的,所以我也就不另外写了.谢谢!! 周末看了一下php7的一些情况,被其强 ...

  7. UNIX环境高级编程 第3章 文件I/O

    前面两章说明了UNIX系统体系和标准及其实现,本章具体讨论UNIX系统I/O实现,包括打开文件.读文件.写文件等. UNIX系统中的大多数文件I/O只需要用到5个函数:open.read.write. ...

  8. kworker内核工作队列详解

    工作队列是另一种将工作推后执行的形式,它可以把工作交给一个内核线程去执行,这个下半部是在进程上下文中执行的,因此,它可以重新调度还有睡眠.    区分使用软中断/tasklet还是工作队列比较简单,如 ...

  9. 洛谷P3760异或和

    传送门啦 传送门啦 一般这种位运算的题都要把每一位拆开来看,因为位运算每个位的结果这和这一位的数有关. 这样我们用s[i]表示a的前缀和,即 $ a[1]+a[2]+....a[i] $ ,然后我们从 ...

  10. Vue select 下拉菜单

    1.html <div id="app-8"> <select v-model="selected"> <option v-for ...