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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值