CodeForces 732 B.Cormen — The Best Friend Of a Man(贪心)
2017-05-01 22:03
405 查看
Description
给出一个长度为n的序列a,现在要给a序列的每个元素加若干值变成b序列使得b序列任意两个相邻数之和至少为k,问总共至少需要加多少
Input
第一行两个整数n和k,之后n个整数a[i]表示该序列(1<=n,k<=500,0<=a[i]<=500)
Output
输出总共至少需要加多少,之后输出b序列
Sample Input
3 5
2 0 1
Sample Output
4
2 3 2
Solution
贪心,如果a[i]+a[i+1] < k,那么就把a[i+1]变成k-a[i],对答案累加k-a[i]-a[i+1]
Code
给出一个长度为n的序列a,现在要给a序列的每个元素加若干值变成b序列使得b序列任意两个相邻数之和至少为k,问总共至少需要加多少
Input
第一行两个整数n和k,之后n个整数a[i]表示该序列(1<=n,k<=500,0<=a[i]<=500)
Output
输出总共至少需要加多少,之后输出b序列
Sample Input
3 5
2 0 1
Sample Output
4
2 3 2
Solution
贪心,如果a[i]+a[i+1] < k,那么就把a[i+1]变成k-a[i],对答案累加k-a[i]-a[i+1]
Code
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<queue> #include<map> #include<set> #include<ctime> using namespace std; typedef long long ll; #define INF 0x3f3f3f3f #define maxn 555 int n,k,a[maxn]; int main() { while(~scanf("%d%d",&n,&k)) { for(int i=1;i<=n;i++)scanf("%d",&a[i]); int ans=0; for(int i=2;i<=n;i++) { if(a[i-1]+a[i]>=k)continue; ans+=k-a[i-1]-a[i]; a[i]=k-a[i-1]; } printf("%d\n",ans); for(int i=1;i<=n;i++)printf("%d%c",a[i],i==n?'\n':' '); } return 0; }
相关文章推荐
- Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man(贪心)
- Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man(贪心)
- Cormen — The Best Friend Of a Man (贪心)
- CodeForce:732B-Cormen — The Best Friend Of a Man
- codeforces 732B - Cormen --- The Best Friend Of a Man
- Codeforces 732B-Cormen—The Best Friend Of a Man
- Codeforces 732B Cormen — The Best Friend Of a Man【贪心】
- CodeForces-732B-Cormen — The Best Friend Of a Man(简单模拟)
- 【56.74%】【codeforces 732B】Cormen --- The Best Friend Of a Man
- CodeForces 732B 之 Cormen — The Best Friend Of a Man
- CodeForces 732B Cormen — The Best Friend Of a Man (贪心)
- codeforces 732B Cormen — The Best Friend Of a Man (水题)
- CodeForces 732B Cormen — The Best Friend Of a Man
- 【codeforces 723 B Cormen — The Best Friend Of a Man】
- Codeforces Round #377 (Div. 2) Cormen --- The Best Friend Of a Man
- codeforces 732B Cormen — The Best Friend Of a Man
- B. Cormen — The Best Friend Of a Man
- Codeforces Round #377 (Div. 2) B. Cormen — The Best Friend Of a Man
- CodeForces 732B Cormen — The Best Friend Of a Man
- CodeForces 732B Cormen — The Best Friend Of a Man(带着宠物轧马路)