Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend 贪心

C. Sonya and Problem Wihtout a Legend

题目连接:

http://codeforces.com/contest/713/problem/C

Description

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.

Sample Input

7
2 1 5 11 5 9 11

Sample Output

9

Hint

题意

给你一个长度为n个序列,然后你每次操作可以使得一个数减小一,或者使得一个数增加一,问你最少多少次操作,可以使得这个序列为单增的。

题解:

我们让每一个数,一开始-=i,然后就可以把单增变成不降序列了。

这下我们就可以贪心了,扔到一个堆里面去,每次拿出最大的元素,变成x就好了

代码

#include<bits/stdc++.h>
using namespace std;
int n;
long long ans;
priority_queue<int>s;
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int x;scanf("%d",&x);
        x-=i;
        s.push(x);
        if(s.top()>x)
        {
            ans+=s.top()-x;
            s.pop();
            s.push(x);
        }
    }
    cout<<ans<<endl;
}
原文地址:https://www.cnblogs.com/qscqesze/p/5929195.html