codeforces 354 D. Transferring Pyramid

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[100005];
int ans;
int f[1005];
int sum[1005],cost[1005];//数组不要开小qwq
void init() {
    scanf("%d %d",&n,&k);
    int a,b;
    siji(i,1,k) {
        scanf("%d%d",&a,&b);
        c[b].push_back(n-a+1);
    }
    ans=3*k;
}
void solve() {
    init();
    int t;
    siji(i,1,n) {
        t=min(n-i+1,780);
        memset(sum,0,sizeof(sum));
        cost[0]=f[0];
        xiaosiji(z,0,c[i].size()) {if(c[i][z]<=t)sum[c[i][z]]=1;}
        //因为我们枚举只到了(6k)^0.5,所以这以后的都不再统计,防止爆数组
        siji(z,1,t+1) sum[z]+=sum[z-1];
        siji(z,1,t) cost[z]=max(cost[z-1],f[z]);
        if(i==1) {
            siji(z,0,t+1) f[z]=-inf;
        }
        xiaosiji(j,0,t) {
            if(j==0) {
                f[j]=max(f[j+1],cost[0]-3)+3*sum[j+1];
                f[j]=max(f[j],cost[0]);//0是我们选择不在这一列放的唯一途径
            }
            else {
                f[j]=max(f[j+1],cost[j]-(j+2)*(j+1)/2-2)+3*sum[j+1];
            }
        }

    }
    printf("%d\n",ans-f[0]);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    solve();
    return 0;
}

 

转载于:https://www.cnblogs.com/ivorysi/p/6359088.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值