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. python---方法解析顺序MRO(Method Resolution Order)<以及解决类中super方法>

    MRO了解: 对于支持继承的编程语言来说,其方法(属性)可能定义在当前类,也可能来自于基类,所以在方法调用时就需要对当前类和基类进行搜索以确定方法所在的位置.而搜索的顺序就是所谓的「方法解析顺序」(M ...

  2. Chrome浏览器F12讲解

    Chrome浏览器相对于其他的浏览器而言,DevTools(开发者工具)非常强大.这节课将为大家介绍怎么利用Chrome浏览器的开发者工具进行HTTP请求分析 Chrome浏览器讲解 Chrome 开 ...

  3. ZeroMQ API(三) 消息

    1.初始化消息 1.1 zmq_msg_init(3) 1.1.1 名称 zmq_msg_init - 初始化空的ZMQ消息 1.1.2 概要 int zmq_msg_init(zmq_msg_t * ...

  4. Redis学习五:Redis的持久化-RDB

    RDB(Redis DataBase) 一.是什么 1.概念:在指定的时间间隔内将内存中的数据集快照写入磁盘,也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里 2.执行方式: R ...

  5. MapReduce (MRV1)设计理念与基本架构

    MapReduce 是一个分布式计算框架,主要由两部分组成:编程模型和运行时环境. 其中,编程模型为用户提供了非常易用的编程接口,用户只需要像编写串行程序一样实现几个简单的函数即可实现一个分布式程序, ...

  6. 对 jQuery 中 data 方法的误解

    一直以来都认为新版本中 data 是调用 dataset 实现的,对于低版本IE则采用 getAttribute其实一直是我误解了,也不知道最初这个想法是怎么来的.难道我被盗梦了? 今天 谢亮 兄弟和 ...

  7. xgraph和gnuplot初体验

    今天分别体验了一下xgraph和gnuplot.   xgraph是ns2自带的画图工具,使用很简单.它的标准的数据文件是ascii文本文件,每一行两个数据,以空格隔开,这样就有了两列数据.把这样的文 ...

  8. supervisor简洁用例

    supervisor是什么 superviosr是一个Linux/Unix系统上进程监控和管理的工具,它由python编写,可以用pip安装.supervisor能将一个普通的命令行进程变为后台dae ...

  9. 【译】第十篇 Integration Services:高级事件行为

    本篇文章是Integration Services系列的第十篇,详细内容请参考原文. 简介在前一篇, we introduced fault tolerance by examining method ...

  10. js数据类型隐式转换问题

    js数据类型隐式转换 ![] == false //true 空数组和基本类型转换,会先[].toString() 再继续比较 ![] == [] //true ![] //false [] == [ ...