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. 移动端开发需要加的meta

    移动端开发需要加的meta和常用的css3媒体查询样式,移动开发中头部要加的一些常用meta. <meta name="viewport" content="ini ...

  2. 20.IO流部分笔记

    20.IO流部分笔记 2018/09/06 1.IO流  1.1 创建字节输出流对象,如果没有就自动创建一个 FileOutputStram fos = new FileOutputStram(&qu ...

  3. python爬虫28 | 你爬下的数据不分析一波可就亏了啊,使用python进行数据可视化

    通过这段时间 小帅b教你从抓包开始 到数据爬取 到数据解析 再到数据存储 相信你已经能抓取大部分你想爬取的网站数据了 恭喜恭喜 但是 数据抓取下来 要好好分析一波 最好的方式就是把数据进行可视化 这样 ...

  4. jmeter 性能测试

    1. Ramp-up Period(in seconds)代表多长时间内启动所有线程 2. Aggregate Report Samples:总共发给服务器的请求数量 Average:单个请求的平均响 ...

  5. JavaScript 面向对象的编程(二) 类的封装

    类的定义 方式一 var Book = function(id, name, price){ //私有属性,外部不能直接访问 var num = 1; //私有方法, function checkId ...

  6. 【17】AngularJS Bootstrap

    AngularJS Bootstrap AngularJS 的首选样式表是 Twitter Bootstrap, Twitter Bootstrap 是目前最受欢迎的前端框架. Bootstrap 你 ...

  7. [luoguP2890] [USACO07OPEN]便宜的回文Cheapest Palindrome(DP)

    传送门 f[i][j] 表示区间 i 到 j 变为回文串所需最小费用 1.s[i] == s[j] f[i][j] = f[i + 1][j - 1] 2.s[i] != s[j] f[i][j] = ...

  8. Python函数基础---参数、变量

    函数:指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可. def sayhi( ): # 函数名 print('hello world') sayhi( ) # ...

  9. codevs4343 找回密码

    题目描述 Description jrMz 很喜欢动漫<叛逆的鲁鲁修>(额= =不知道是不是因为他盯上了动画片里的 MM),他准备以一种神奇的方式降临<叛逆的鲁鲁修>世界,所以 ...

  10. [poj2425]A Chess Game_博弈论

    A Chess Game poj-2425 题目大意:题目链接 注释:略. 想法:这个题就是为什么必须要用记忆化搜索.因为压根就不知道后继是谁. 我们通过SG定理可知:当前游戏的SG值等于所有子游戏的 ...