hihocoder#1529 : 不上升序列&&Codeforces-713C:Sonya and Problem Wihtout a Legend(思维)
2017-09-27 16:42
483 查看
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
output
input
output
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]
样例输入
样例输出
思路: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;
}
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
7 2 1 5 11 5 9 11
output
9
input
5 5 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]样例输入
4 5 3 4 5
样例输出
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;
}
相关文章推荐
- 【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
- 【CF 713C】Sonya and Problem Wihtout a Legend(离散化+dp)<POJ 3666变形>
- Codeforces 713 C Sonya and Problem Wihtout a Legend
- codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)
- 【CF713C】Sonya and Problem Wihtout a Legend(离散化,DP)
- 【CodeForces】713 C. Sonya and Problem Wihtout a Legend
- 动态规划,离散化(Sonya and Problem Wihtout a Legend,cf 713C)
- codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)
- Codeforces 713C C. Sonya and Problem Wihtout a Legend (经典DP)
- cf/Codeforces Round #371 714E - Sonya and Problem Wihtout a Legend - dp +lis
- Codeforces Round #371 (Div. 2)E. Sonya and Problem Wihtout a Legend[DP 离散化 LIS相关]
- cf 372 div2 E. Sonya and Problem Wihtout a Legend dp
- Codeforces Round #371 (Div. 2) E. Sonya and Problem Wihtout a Legend (DP/LIS变形)
- Codeforces Round 371 C Sonya and Problem Wihtout a Legend
- Codeforces Round #371 C. Sonya and Problem Wihtout a Legend (DP)
- 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 贪心
- cf 714 e Sonya and Problem Wihtout a Legend