codeforces Round # 236(DIV 2)B. Trees in a Row
来源:互联网 发布:最早截止时间优先算法 编辑:程序博客网 时间:2024/05/01 03:11
1、http://codeforces.com/contest/402/problem/B
2、题目大意:有n棵树,知道这n棵树的高度,现在要使得这n棵树满足这样的关系,第i棵树比第i-1棵树高k米,每棵树都可以增加高度或者是减少高度,求经过多少步可以使得这n棵树满足条件,输出最小的步数,然后输出每步的具体操作。
3、解题思路:一开始还以为是道DP的题目,看着数据不大,想用模拟试一下,结果测试就过了所谓的模拟就是运用了1000*1000的循环,依次将a[i]作为中心去更新左右两边的树,记录更新的次数,同时记录i,最后只要按照记录的这个i,依次将其他的树更新输出就行
4、AC代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[1005];int main(){ int n,k; while(scanf("%d%d",&n,&k)!=EOF) { for(int i=1; i<=n; i++) { scanf("%d",&a[i]); } int id; int minn=1005; for(int i=1; i<=n; i++) { int tmp=a[i]; int cnt=0;//记录每次的操作步数 int flag=0;//标记是否经过修改之后的树高度不在0-1000这样的范围里了 for(int j=1; j<=i; j++) { int ans=tmp-(i-j)*k; if(ans!=a[j] && ans>0) cnt++; if(ans<=0) { flag=1; break; } } if(flag==0) { tmp=a[i]; for(int j=i+1; j<=n; j++) { int ans=tmp+(j-i)*k; if(ans!=a[j] && ans>0) cnt++; if(ans<=0) { flag=1; break; } } } if(flag==0) { if(cnt<minn) { minn=cnt; id=i; } } } if(minn==0) { printf("0\n"); } else { printf("%d\n",minn); //printf("%d\n",id); for(int i=1; i<=id; i++) { int ans=a[id]-(id-i)*k; if(a[i]<ans) printf("+ %d %d\n",i,ans-a[i]); else if(a[i]>ans) printf("- %d %d\n",i,a[i]-ans); } for(int i=id+1; i<=n; i++) { int ans=(i-id)*k+a[id]; if(a[i]<ans) printf("+ %d %d\n",i,ans-a[i]); else if(a[i]>ans) printf("- %d %d\n",i,a[i]-ans); } } } return 0;}/*3 21 4 54 31 3 6 10*/
0 3
- 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
- 对于机器人,电脑与有机体生命之间的进化比较(好几年前自己写的,很有感触)
- java删除文件(单个文件,文件夹,子文件夹) 20090812(jsp+servlet+access)
- Linux下重要日志文件及查看方式
- 2012蓝桥杯预赛题-夺冠概率
- 002_021 Python 动态改变Python的搜索路径
- codeforces Round # 236(DIV 2)B. Trees in a Row
- Codeforces Round #236 (Div. 2)A
- Spring MVC中各个filter的用法
- 设计模式学习之——六大设计原则之四:接口隔离原则
- HDFS余额不足之时
- 从11月11号开始我要将我儿时的梦想初步完成(2009年的回忆)
- Spring框架之Filter应用
- [2014-3-17]字符和字符串处理
- 浅析C++ cout语句执行过程