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

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

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

 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
const int N=;
const int INF=<<;
using namespace std;
int a[N],b[N],f[N];
int main()
{
int n,d;
while(cin>>n>>d){
cin>>f[];
int flag1 = ;
for (int i = ; i <= n; i++){
cin>>f[i];
if (f[i]-f[i-]!=d)
flag1 = ;
}
if (flag1){ //原序列为公差为d的等差序列
puts("");
continue;
}
int cnt,Min = INF;
for (int i = ; i <= n; i++){
int m = f[i]-d;
cnt = ;flag1 = ;
for (int j = i-; j >= ; j--){//调整f[i]左边的数使其成为公差为d的等差序列
if (m <= ){ //出现小于0的数,则说明该序列不合法
flag1 = ;
break;
}
if (f[j]!=m)
cnt++;//记录修改的数的个数
m = m-d;
}
if (flag1)
continue;
m = f[i]+d;
for (int j = i+; j <= n; j++){//调整f[i]右边的数使其成为公差为d的等差序列
if (f[j]!=m)
cnt++;
m+=d;
}
if (cnt < Min){
Min = cnt;//记录最少的操作次数
m = f[i]-d;
memset(a,-,sizeof(a));//a[]存储对数据进行增的操作
memset(b,-,sizeof(b));//b[]存储对数据进行减的操作
for (int j = i-; j >= ; j--){
if (f[j] > m)
b[j] = f[j]-m;
if (f[j] < m)
a[j] = m-f[j];
m-=d;
}
m = f[i]+d;
for (int j = i+; j <= n; j++){
if (f[j] > m)
b[j] = f[j]-m;
if (f[j] < m)
a[j] = m-f[j];
m+=d;
}
}
}
cout<<Min<<endl;
for (int i = ; i <= n; i++){
if (a[i]!=-){
printf("+");
cout<<" "<<i<<" "<<a[i]<<endl;
}
if (b[i]!=-){
printf("-");
cout<<" "<<i<<" "<<b[i]<<endl;
}
}
}
return ;
}

B. Trees in a Row(cf)的更多相关文章

  1. codeforces B. Trees in a Row 解题报告

    题目链接:http://codeforces.com/problemset/problem/402/B 题目意思:给出n个数和公差k,问如何调整使得ai + 1 - ai = k.(1 ≤ i < ...

  2. CodeForces - 402B Trees in a Row (暴力)

    题意:给定n个数,要求修改其中最少的数,使得这n个数满足ai + 1 - ai = k. 分析: 暴力,1000*1000. 1.这n个数,就是一个首项为a1,公差为k的等差数列.k已知,如果确定了a ...

  3. Codeforces Round #236 (Div. 2)

    A. Nuts time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputoutput:st ...

  4. HBase读延迟的12种优化套路

    任何系统都会有各种各样的问题,有些是系统本身设计问题,有些却是使用姿势问题.HBase也一样,在真实生产线上大家或多或少都会遇到很多问题,有些是HBase还需要完善的,有些是我们确实对它了解太少.总结 ...

  5. 单调队列应用--BZOJ 3831 Little Bird

    3831: [Poi2014]Little Bird Time Limit: 20 Sec  Memory Limit: 128 MB Description In the Byteotian Lin ...

  6. 【BZOJ】【3831】【POI2014】Little Bird

    DP/单调队列优化 水题水题水题水题 单调队列优化的线性dp…… WA了8次QAQ,就因为我写队列是[l,r),但是实际操作取队尾元素的时候忘记了……不怎么从队尾取元素嘛……平时都是直接往进放的……还 ...

  7. BZOJ 3831

    3831: [Poi2014]Little Bird Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 121  Solved: 68[Submit][S ...

  8. Bzoj 3831 [Poi2014]Little Bird

    3831: [Poi2014]Little Bird Time Limit: 20 Sec Memory Limit: 128 MB Submit: 310 Solved: 186 [Submit][ ...

  9. spark 操作hbase

    HBase经过七年发展,终于在今年2月底,发布了 1.0.0 版本.这个版本提供了一些让人激动的功能,并且,在不牺牲稳定性的前提下,引入了新的API.虽然 1.0.0 兼容旧版本的 API,不过还是应 ...

随机推荐

  1. thymeleaf在开发环境正常,但用jar运行时报错 Error resolving template template might not exist or might not be accessible

    解决方案: (1)配置中添加  spring.thymeleaf.prefix=classpath:/templates (2)指向模板的路径 不加 /

  2. python3 http.server备忘

    python3英文的 打印出来应该不错: https://docs.python.org/3/library/http.server.html#module-http.server python2.7 ...

  3. python3中整数和小数的转换

    在整数除法中,除法(/)总是返回一个浮点数,如果只想得到整数的结果,丢弃可能的分数部分,可以使用运算符 // : >>> 17 / 3 # 整数除法返回浮点型 5.666666666 ...

  4. 【Python实践-8】和为S的两个数字

    (剑指offer)输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 思路:选定第一个数字,然后遍历后面的数字求和并与S比 ...

  5. highcharts图表的常见操作

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  6. 数位dp备忘录

    hdu 4734:把限制值化为数组形式,逐位求解 hdu 4507:类似于上题,其中求平方和的方法很妙 SPOJ  BALNUM Balanced Numbers:经典数位dp,注意两点,1,不要把前 ...

  7. SSM+Shiro

    1) 表名:用户表(Sys_Users) Salt:盐(佐料).为避免被黑客等进行攻击(暴力密码破解),所以一般在注册用户信息时,系统会随机生成一个随机码.在验证时会将密码和随机码进行运算,以验证密码 ...

  8. 【Codeforces 242C】King's Path

    [链接] 我是链接,点我呀:) [题意] 让你找到(x0,y0)到(x1,y1)的一条最短路 走过的点必须在所给的n个横向路径上 [题解] 因为n条横向路径上的点最多不会超过10的5次方个,所以我们可 ...

  9. 【网络流24题】最长k可重区间集问题(费用流)

    [网络流24题]最长k可重区间集问题 [问题分析] 最大权不相交路径问题,可以用最大费用最大流解决. [建模方法] 方法1 按左端点排序所有区间,把每个区间拆分看做两个顶点<i.a>< ...

  10. - > 并查集+路径压缩(详解)(第一节)

    先举一个友爱的例子解释一下并查集: 话说江湖上散落着各式各样的大侠,有上千个之多. 他们没有什么正当职业,整天背着剑在外面走来走去,碰到和自己不是一路人的,就免不了要打一架.但大侠们有一个优点就是讲义 ...