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. 解决移动端 手机号input 属性为 number,maxlength无效情况

    <input type="number" oninput="if(value.length>11)value=value.slice(0,11)" ...

  2. 题解 NOI2018 归程

    题解 NOI2018 归程 题意 本题的故事发生在魔力之都,在这里我们将为你介绍一些必要的设定. 魔力之都可以抽象成一个 n 个节点.m 条边的无向连通图(节点的编号从 1 至 n).我们依次用 l, ...

  3. Ubuntu下安装oh-my-zsh

    本文参考 https://www.jianshu.com/p/9a5c4cb0452d 此文已在ubuntu下确实安装成功,只不过懒得截图了,可以参照上述地址,我在他原基础上进行了一些更改. 安装Oh ...

  4. Python学习:ModuleNotFoundError: No module named 'pygal.i18n' 的解决方法

    最近在学<Python编程:从入门到实践>,16.2小结中 from pygal.i18n import COUNTRIES 获取两个字母的国别码,我用的pygal的版本是2.4.0(终端 ...

  5. Latex Notes

    latex Table of Contents 1. Presentation/Slides with Beamer 2. Drawing in LaTex With TikZ 3. Tracked ...

  6. scanf与getchar

    如下: 5  5 R  R  R  R  R R  R  R  R  R R  R  R  R  R R  R  R  R  R R  R  R  R  R 只允许用scanf,如何写读取函数. 由于 ...

  7. Leetcode 93.复制IP地址

    复制IP地址 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135& ...

  8. 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); ...

  9. T1683 车厢重组 codevs

    http://codevs.cn/problem/1683/  时间限制: 1 s  空间限制: 1000 KB  题目等级 : 白银 Silver 题目描述 Description 在一个旧式的火车 ...

  10. Ubuntu安装deb时错误:“dpkg:错误:另外一个进程已经为 dpkg 状态数据库 加锁”解决

    以下方式任选一个即可: 1.重启系统 2.执行(这种方式不要尝试,系统很容易挂) sudo rm /var/lib/dpkg/lock 然后执行修复 sudo dpkg --configure -a