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. Android中ProgressDialog自动消失

    主要函数部分代码: final ProgressDialog proDialog = android.app.ProgressDialog .show(MainActivity.this, " ...

  2. C++ string 是否以‘\0’结尾 讨论

    转载https://blog.csdn.net/qq_31930499/article/details/80374310 之前在某篇文章中看到,C语言字符串是以’\0’结尾的,但是C++string类 ...

  3. 微信小程序 导航 4种页面跳转 详解

    1.wx.navigateTo   保留当前页面,跳转到应用内的某个页面,目前页面路径最多只能十层.  参数:url(可携带参数) .success .fail .complete 可用wxml代替: ...

  4. 将node-webkit打包后文件用nsis再打包成安装包

  5. spring与quartz整合实现分布式动态创建,删除,改变执行时间定时任务(mysql数据库)

    背景:因为在项目中用到了定时任务,当时想到了spring的quartz,写完发现费了很大功夫,光是整合就花了一上午,其中最大的问题就是版本问题,项目中用的是spring3.2.8的版本,查阅发现,3. ...

  6. Unity常用常找(二)

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/51315050 作者:car ...

  7. nginx+keepalived+consul 实现高可用集群

    继 负载均衡 之 nginx+consul+consul template,我这次将使用2台虚拟机,来做一个简单的双机负载均衡试验. 试验目标: 1. 当参加负载均衡的子节点服务,有任何其中一个或多个 ...

  8. h5dnd sortable mutil groups

    h5dnd sortable mutil groups https://codepen.io/webgeeker/pen/JmPXaN https://codepen.io/webgeeker/pen ...

  9. codeforces 371B - Fox Dividing Cheese

    #include<stdio.h> int count; int gcd(int a,int b) { if(b==0) return a;     return gcd(b,a%b); ...

  10. 控制公司(codevs 2051)

    题目描述 Description 有些公司是其他公司的部分拥有者,因为他们获得了其他公司发行的股票的一部分.例如,福特公司拥有马自达公司12%的股票.据说,如果至少满足了以下三个条件之一,公司A就可以 ...