Codeforces Round #236 (Div. 2) B. Trees in a Row

来源:互联网 发布:php大神博客 编辑:程序博客网 时间:2024/05/01 01:01

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: nk (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 11 2 1 5
output
2+ 3 2- 4 1
input
4 11 2 3 4
output
0


题意:有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;


Accepted31 ms12 KB



#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
原创粉丝点击