Codeforces Round #236 (Div. 2) B. Trees in a Row
来源:互联网 发布:php大神博客 编辑:程序博客网 时间:2024/05/01 01:01
题意:有n棵树,第i棵高ai,女皇要求每棵树按k递增,每次操作可 + 或 - 树任意高度,输出最少操作数,并输出所有操作。
思路:n<1000;建一数组b[i] 按k递增,再建一数组c[j](-1000<=j<=1000)记录 a[i]-b[i] 出现次数最多的值 t; b[i]+t即为女皇要求看到的状态,且操作数最少。
曲折:RE因为没考虑b[i]有可能超过1000,WA因为没考虑最终情况树高不能低于0;
#include <stdio.h>#include <stdlib.h>int main(){ int a[1010],b[1010],c[2020]={0}; int n, k, i, max=0, t; scanf("%d%d", &n, &k); b[1] = 1; for(i = 1; i <= n; i++) { if(i-1) b[i] = b[i-1] + k; scanf("%d", &a[i]); if(b[i] <= 1000) c[a[i]-b[i]+1010]++; } for(i = 1011 - b[1]; i < 2020; i++) { if(c[i] > max) {max = c[i]; t = i-1010;} } printf("%d\n",n-max); for(i = 1; i <= n; i++) { b[i] += t; if(a[i]-b[i] == 0) continue; if(a[i]-b[i] > 0) printf("- %d %d\n", i, a[i]-b[i]); else printf("+ %d %d\n", i, b[i]-a[i]); } return 0;}
0 0
- Codeforces Round #236 (Div. 2) B. Trees in a Row
- codeforces Round # 236(DIV 2)B. Trees in a Row
- Codeforces Round #236 (Div. 2) B. Trees in a Row
- Codeforces Round #236 (Div. 2)__Trees in a Row
- codeforces 402B - Trees in a Row
- codeforces B. Trees in a Row
- codeforces 402B Trees in a Row
- codeforces#236_div2_B Trees in a Row
- codeforces Trees in a Row
- Trees in a Row CodeForces
- Codeforces round #236Div.2--A;B
- Codeforces Round #236 (Div. 2)------A,B
- Educational Codeforces Round 25 B. Five-In-a-Row
- Educational Codeforces Round 25 B. Five-In-a-Row
- Educational Codeforces Round 25 B. Five-In-a-Row
- CF236 B. Trees in a Row
- CF 402B - Trees in a Row
- CF---B. Trees in a Row
- c++作业
- [转载]10步让你成为更优秀的程序员
- 实现类似spring的可配置的AOP框架
- c++作业
- 140218 简单数据类型和转换
- Codeforces Round #236 (Div. 2) B. Trees in a Row
- poj2299 归并排序求逆序数
- http 405错误
- poj2352 树状数组
- poj2379 排序
- javaSE--随机数的两种获取方式
- CF403B/CF402D
- poj2395 Kruskal最小生成树
- Floyd算法介绍