Codeforces Round #236 (Div. 2) B. Trees in a Row
来源:互联网 发布:修理图片的软件 编辑:程序博客网 时间:2024/04/30 16:09
第二题其实也挺简单的,我比较喜欢做这种数据要求很有规律并且很简单的题
首先遍历以每颗树为基准,判断需要改变树高度的次数
隐含的条件就是要保证以当前树为基准时别的树高度不能小于0
取最小的那次对应基准,再判断别的树需要怎样改变
代码如下:
#include <algorithm>#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <vector>#include <map>#define MAXN 1010#define INF 0x7FFFFFFF#define ll long longusing namespace std;int a[MAXN], sum[MAXN];int main(void){int n, k;while(cin >> n >> k){for(int i=0; i<n; ++i){cin >> a[i];}for(int i=0; i<n; ++i) sum[i] = 0;for(int i=0; i<n; ++i){for(int j=0; j<n; ++j){int t1 = a[i]+(j-i)*k;//第j棵树期望的高度 int t2 = a[j]-t1;if(t1 < 0){ sum[i] = INF;//以i 为基准出现负数时,应该取消当前循环 break;}if(t2 != 0){ sum[i]++;//以i为基准需要的改变次数 }}}int flag = 0;int min = INF;for(int i=0; i<n; ++i){if(sum[i] < min){min = sum[i];flag = i;}}//以flag为基准,移动最少cout << sum[flag] << endl;for(int i=0; i<n; ++i){int step = a[flag]+(i-flag)*k - a[i];if(step > 0){cout << "+ " << i+1 << " " << step << endl;}else if(step < 0){cout << "- " << i+1 << " " << -step << endl;}} }return 0;}
这个代码也跪了。。。又是少了一个等号。。。t1<=0时都需要退出循环
代码如下:
#include <algorithm>#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <vector>#include <map>#define MAXN 1010#define INF 0x7FFFFFFF#define ll long longusing namespace std;int a[MAXN], sum[MAXN];int main(void){int n, k;cin >> n >> k;for(int i=0; i<n; ++i){cin >> a[i];}for(int i=0; i<n; ++i) sum[i] = 0;for(int i=0; i<n; ++i){for(int j=0; j<n; ++j){int t1 = a[i]+(j-i)*k;//��j��������ĸ߶� int t2 = a[j]-t1;if(t1 <= 0){ sum[i] = INF;//��i Ϊ�����ָ���ʱ��Ӧ��ȡ��ǰѭ�� break;}if(t2 != 0){ sum[i]++;//��iΪ����Ҫ�ĸı���� }}}int flag = 0;int min = INF;for(int i=0; i<n; ++i){if(sum[i] < min){min = sum[i];flag = i;}}//��flagΪ�����ƶ�����cout << sum[flag] << endl;for(int i=0; i<n; ++i){int step = a[flag]+(i-flag)*k - a[i];if(step > 0){cout << "+ " << i+1 << " " << step << endl;}else if(step < 0){cout << "- " << i+1 << " " << -step << endl;}} 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
- 【转】最大和子序列、最长递增子序列、最长公共子串、最长公共子序列、字符串编辑距离
- iOS7后台机制详解
- 浅谈编译器和解释器
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Codeforces Round #236 (Div. 2) A. Nuts
- Codeforces Round #236 (Div. 2) B. Trees in a Row
- .NET:一个非常好用的SQL数据库处理类
- Nuts
- codeforces Round #236(DIV 2)A. Nuts(数学类计算题)
- UVa 400 Unix的ls命令
- Codeforces Round #236 (Div. 2)B
- 对于机器人,电脑与有机体生命之间的进化比较(好几年前自己写的,很有感触)
- java删除文件(单个文件,文件夹,子文件夹) 20090812(jsp+servlet+access)
- Linux下重要日志文件及查看方式