B. Trees in a Row(cf)

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 <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <iostream>
 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<<Min<<endl;
68         for (int i = 1; i <= n; i++){
69             if (a[i]!=-1){
70                 printf("+");
71                 cout<<" "<<i<<" "<<a[i]<<endl;
72             }
73             if (b[i]!=-1){
74                 printf("-");
75                 cout<<" "<<i<<" "<<b[i]<<endl;
76             }
77         }
78     }
79     return 0;
80 }
View Code
原文地址:https://www.cnblogs.com/lahblogs/p/3604644.html