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. Duboo学习-SPI

    待补充 现将Dubbo-SPI相关源码流程图更新

  2. 总结struts2 iterator status的用法

    1:#status.odd 是否奇数行 2:#status.count 当前行数 3:#status.index 当前行的序号,从0开始『#status.count=#status.index+1』 ...

  3. 浮动和margin负值 三列布局

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

  4. IN语句改写EXISTS

    -- IN SELECT T1.* FROM role_menu T1 WHERE T1.ROLEUUID IN ( SELECT T2.uuid FROM role T2 WHERE T2.UUID ...

  5. vticker.js--垂直滚动插件

    一.使用要求 列表必须是ul>li的格式 html代码 <div class=" myvticker'"> <ul> <li>1.新闻标题 ...

  6. hdu 2255KM算法模板

    #include<stdio.h> #include<string.h> #define N  400 #define inf 0x7fffffff int Max(int a ...

  7. Quartz原理解密

    Quartz原理解密 Author: Dorae Date:2018年7月17日15:55:02 转载请注明出处 一.quartz概述 quartz是一个用java实现的开源任务调度框架,可以用来创建 ...

  8. java ee标准DataSource理解

  9. Setting .xap MIME Type for Silverlight

    http://www.adefwebserver.com/dotnetnukehelp/misc/Silverlight/SettingMimeType.html Windows 2003: In I ...

  10. [JavaEE] Implement a REST Endpoint

    1. Create a rest folder with JAXRSConfiguration.java: package com.pluralsight.bookstore.rest; import ...