博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
B. Trees in a Row(cf)
阅读量:5260 次
发布时间:2019-06-14

本文共 4047 字,大约阅读时间需要 13 分钟。

B. Trees in a Row
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The Queen of England has n trees growing in a row in her garden. At that, the i-th (1 ≤ i ≤ n) tree from the left has height ai meters. Today the Queen decided to update the scenery of her garden. She wants the trees' heights to meet the condition: for all i (1 ≤ i < n),ai + 1 - ai = k, where k is the number the Queen chose.

Unfortunately, the royal gardener is not a machine and he cannot fulfill the desire of the Queen instantly! In one minute, the gardener can either decrease the height of a tree to any positive integer height or increase the height of a tree to any positive integer height. How should the royal gardener act to fulfill a whim of Her Majesty in the minimum number of minutes?

Input

The first line contains two space-separated integers: n, k (1 ≤ n, k ≤ 1000). The second line contains n space-separated integersa1, a2, ..., an (1 ≤ ai ≤ 1000) — the heights of the trees in the row.

Output

In the first line print a single integer p — the minimum number of minutes the gardener needs. In the next p lines print the description of his actions.

If the gardener needs to increase the height of the j-th (1 ≤ j ≤ n) tree from the left by x (x ≥ 1) meters, then print in the corresponding line "+ j x". If the gardener needs to decrease the height of the j-th (1 ≤ j ≤ n) tree from the left by x (x ≥ 1) meters, print on the corresponding line "- j x".

If there are multiple ways to make a row of trees beautiful in the minimum number of actions, you are allowed to print any of them.

Sample test(s)
input
4 1 1 2 1 5
output
2 + 3 2 - 4 1
input
4 1 1 2 3 4
output
0

题意:对一个序列,通过最少次的对某些数据的增或减,使得该序列成为公差为k的等差序列。

思路:遍历所有的数,假设当前数为等差序列中的数,通过增减其它数,将所有的数变为等差序列中的数,记录最小的操作次数,并将操作的数记录下来。注意:调整后序列中的值必须全部大于0,在这跪了好多次。。。

1 #include 
2 #include
3 #include
4 #include
5 const int N=1005; 6 const int INF=1<<28; 7 using namespace std; 8 int a[N],b[N],f[N]; 9 int main()10 {11 int n,d;12 while(cin>>n>>d){13 cin>>f[1];14 int flag1 = 1;15 for (int i = 2; i <= n; i++){16 cin>>f[i];17 if (f[i]-f[i-1]!=d)18 flag1 = 0;19 }20 if (flag1){ //原序列为公差为d的等差序列21 puts("0");22 continue;23 }24 int cnt,Min = INF;25 for (int i = 1; i <= n; i++){26 int m = f[i]-d;27 cnt = 0;flag1 = 0;28 for (int j = i-1; j >= 1; j--){
//调整f[i]左边的数使其成为公差为d的等差序列29 if (m <= 0){ //出现小于0的数,则说明该序列不合法30 flag1 = 1;31 break;32 }33 if (f[j]!=m)34 cnt++;//记录修改的数的个数35 m = m-d;36 }37 if (flag1)38 continue;39 m = f[i]+d;40 for (int j = i+1; j <= n; j++){
//调整f[i]右边的数使其成为公差为d的等差序列41 if (f[j]!=m)42 cnt++;43 m+=d;44 }45 if (cnt < Min){46 Min = cnt;//记录最少的操作次数47 m = f[i]-d;48 memset(a,-1,sizeof(a));//a[]存储对数据进行增的操作49 memset(b,-1,sizeof(b));//b[]存储对数据进行减的操作50 for (int j = i-1; j >= 1; j--){51 if (f[j] > m)52 b[j] = f[j]-m;53 if (f[j] < m)54 a[j] = m-f[j];55 m-=d;56 }57 m = f[i]+d;58 for (int j = i+1; j <= n; j++){59 if (f[j] > m)60 b[j] = f[j]-m;61 if (f[j] < m)62 a[j] = m-f[j];63 m+=d;64 }65 }66 }67 cout<
<
View Code

 

转载于:https://www.cnblogs.com/lahblogs/p/3604644.html

你可能感兴趣的文章
nginx修改内核参数
查看>>
C 筛选法找素数
查看>>
TCP为什么需要3次握手与4次挥手(转载)
查看>>
IOC容器
查看>>
Windows 2003全面优化
查看>>
URAL 1002 Phone Numbers(KMP+最短路orDP)
查看>>
web_day4_css_宽度
查看>>
electron入门心得
查看>>
格而知之2:UIView的autoresizingMask属性探究
查看>>
我的Hook学习笔记
查看>>
js中的try/catch
查看>>
寄Android开发Gradle你需要知道的知识
查看>>
简述spring中常有的几种advice?
查看>>
整理推荐的CSS属性书写顺序
查看>>
ServerSocket和Socket通信
查看>>
css & input type & search icon
查看>>
源代码的下载和编译读后感
查看>>
Kafka学习笔记
查看>>
Octotree Chrome安装与使用方法
查看>>
Windows 环境下基于 Redis 的 Celery 任务调度模块的实现
查看>>