hihocoder#1529 : 不上升序列&&Codeforces-713C:Sonya and Problem Wihtout a Legend(思维)
来源:互联网 发布:网络当红第一女主播 编辑:程序博客网 时间:2024/06/06 02:36
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.
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).
Print the minimum number of operation required to make the array strictly increasing.
72 1 5 11 5 9 11
9
55 4 3 2 1
12
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
- 样例输入
45345
- 样例输出
2
描述
给定一个长度为 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]
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;}
- hihocoder#1529 : 不上升序列&&Codeforces-713C:Sonya and Problem Wihtout a Legend(思维)
- 【DP】[CodeForces - 713C]Sonya and Problem Wihtout a Legend
- Codeforces-713C Sonya and Problem Wihtout a Legend
- codeforces 713C Sonya and Problem Wihtout a Legend【dp】
- Codeforces-713C-Sonya and Problem Wihtout a Legend
- Codeforces 713C Sonya and Problem Wihtout a Legend(离散化dp)
- Codeforces Round 371 C Sonya and Problem Wihtout a Legend
- Codeforces 713C C. Sonya and Problem Wihtout a Legend (经典DP)
- codeforces 713C(Round #371 Div2 E) Sonya and Problem Wihtout a Legend ★ ★
- Codeforces 713C Sonya and Problem Wihtout a Legend DP(经典)
- Codeforces Round #371 C. Sonya and Problem Wihtout a Legend (DP)
- [DP] Codeforces #714E. Sonya and Problem Wihtout a Legend
- 【CF 713C】Sonya and Problem Wihtout a Legend(离散化+dp)<POJ 3666变形>
- 动态规划,离散化(Sonya and Problem Wihtout a Legend,cf 713C)
- codeforces714e Sonya and Problem Wihtout a Legend
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend(贪心+DP)
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend
- codeforces 713C(Round #371 Div2 E) Sonya and Problem Wihtout a Legend Dp + 前缀 +离散化
- Android样式的开发:selector篇
- linux常用命令总结(持续添加)
- JAVA打印乘法口诀(完美排序)
- Java:override重写与overload重载
- C++函数返回map的两种方式的比较
- hihocoder#1529 : 不上升序列&&Codeforces-713C:Sonya and Problem Wihtout a Legend(思维)
- webpack实现热加载自动刷新方法
- SQL 优化
- 第一篇博客
- DynamicJasper输出动态报表
- 如何让Android WebView访问更快
- SN75176的引脚功能
- c++设计模式二,单例模式
- 保留小数,四舍五入