hihocoder#1529 : 不上升序列&&Codeforces-713C:Sonya and Problem Wihtout a Legend(思维)

来源:互联网 发布:网络当红第一女主播 编辑:程序博客网 时间:2024/06/06 02:36

C. Sonya and Problem Wihtout a Legend
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Sonya was unable to think of a story for this problem, so here comes the formal description.

You are given the array containing n positive integers. At one turn you can pick any element and increase or decrease it by 1. The goal is the make the array strictly increasing by making the minimum possible number of operations. You are allowed to change elements in any way, they can become negative or equal to 0.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 3000) — the length of the array.

Next line contains n integer ai (1 ≤ ai ≤ 109).

Output

Print the minimum number of operation required to make the array strictly increasing.

Examples
input
72 1 5 11 5 9 11
output
9
input
55 4 3 2 1
output
12
Note

In the first sample, the array is going to look as follows:

2 3 5 6 7 9 11

|2 - 2| + |1 - 3| + |5 - 5| + |11 - 6| + |5 - 7| + |9 - 9| + |11 - 11| = 9

And for the second sample:

1 2 3 4 5

|5 - 1| + |4 - 2| + |3 - 3| + |2 - 4| + |1 - 5| = 12

时间限制:40000ms
单点时限:2000ms
内存限制:256MB

描述

给定一个长度为 n 的非负整数序列 a[1..n]。

你每次可以花费 1 的代价给某个 a[i] 加1或者减1。

求最少需要多少代价能将这个序列变成一个不上升序列。

输入

第一行一个正整数 n

接下来 n 行每行一个非负整数,第 i 行表示 a[i]。

1 ≤ n ≤ 500000

0 < a[i] ≤ 109

输出

一个非负整数,表示答案。

样例解释

[5,3,4,5] -> [5,4,4,4]


样例输入
45345
样例输出
2
思路:codeforces那题数据较小,可以用DP。但hihocoder这题数据太大,要用优先队列。其中原理,可以自行思考。

codeforces-713C:

#include<bits/stdc++.h>using namespace std;int n;priority_queue<int>p;int main(){    __int64 ans=0;    scanf("%d",&n);    for(int i=1,a;i<=n;i++)    {        scanf("%d",&a);        a-=i;        p.push(a);        if(p.top()>a)        {            ans+=p.top()-a;            p.pop();            p.push(a);        }    }    cout<<ans<<endl;    return 0;}
hihocoder#1529:

#include<bits/stdc++.h>using namespace std;int main(){int n;long long ans=0;cin>>n;priority_queue<int>s;for(int i=0;i<n;i++)    {int a;scanf("%d",&a);a=-a;s.push(a);if(s.top()>a){ans+=s.top()-a;s.pop();s.push(a);}}cout<<ans<<endl;}



阅读全文
1 0