Codeforces 402B《Trees in a Row》

Translate

有n个正整数,可以对一个数进行修改(修改后也是正整数),要求修改之后满足 a_i+k=a{i+1} ,求最少的修改次数以及具体的修改方案

Description

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 integers a1, 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.

Examples

input
4 1
1 2 1 5

output
2

  • 3 2
  • 4 1

input
4 1
1 2 3 4

output
0

解析

本文同步发布于洛谷博客


先说点题外话

这道题是我今天(发题解的那一天,2019.08.07)打 ACM 的时候做的,当时看到这题口胡了一下做法,没敢写,交给队友去写了,然后一遍AC

感谢队友把我带飞


言归正传。

考虑枚举第一棵树的高度,因为这样就能直接确定后面的树的高度了

然后假设当前枚举第一棵树高度为 l ,那么第 i 棵树的高度 h_i 就应该是 l + k(i - 1) ,这个应该很好理解吧,如果不懂可以评论区留个言,我找个时间写一下

那么再套一层循环,枚举所有的树,如果当前枚举到的第 i 棵树的高度不是 l + k(i - 1) ,就意味着这棵树需要被修改,需要增高 / 降低的高度是 \text{abs}(l - k(i - 1)) ,需要增高还是降低取决于 l - k(i - 1) 的符号

统计一下修改了多少次,作为一个方案,最后取一个最优方案的输出就行了


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>

#define FILE_IN(__fname) freopen(__fname, "r", stdin)
#define FILE_OUT(__fname) freopen(__fname, "w", stdout)
#define rap(a,s,t,i) for (int a = s; a <= t; a += i)
#define basketball(a,t,s,i) for (int a = t; a > s; a -= i)
#define countdown(s) while (s --> 0)
#define IMPROVE_IO() std::ios::sync_with_stdio(false)

using std::cin;
using std::cout;
using std::endl;

typedef long long int lli;

int getint() { int x; scanf("%d", &x); return x; }
lli getll() { long long int x; scanf("%lld", &x); return x; }

const int MAXNKH = 1000 + 10; // the max of n, k, height
const int LOC = 0;
const int MODIFY = 1;

int n, k, ans = 0x7f7f7f7f, ansf[MAXNKH][2], p, f[MAXNKH][2];
int height[MAXNKH];

int main() {
n = getint(); k = getint();
rap (i, 1, n, 1) height[i] = getint();
rap (h1, 1, 1000, 1) {
// 枚举第一棵树的高度,可以直接确定后面树的高度
p = 0; memset(f, 0, sizeof f);
rap (i, 1, n, 1) {
if (height[i] - (i - 1) * k != h1) {
// 记一下要修改的树的下标和要增加 / 减少的值
f[++p][LOC] = i;
f[p][MODIFY] = (h1 + (i - 1) * k) - height[i];
}
}
if (p < ans) {
// 更优的方案,更新一下
ans = p;
memcpy(ansf, f, sizeof f);
}
}
printf("%d\n", ans);
rap (i, 1, ans, 1) printf("%c %d %d\n", (ansf[i][MODIFY] < 0 ? '-' : '+'), ansf[i][LOC], std::abs(ansf[i][MODIFY]));
return 0;
}

// Code by Handwer