codeforces402B
Trees in a Row
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: n, k (1 ≤ n, k ≤ 1000). The second line contains n space-separated integers a1, 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.
Examples
4 1
1 2 1 5
2
+ 3 2
- 4 1
4 1
1 2 3 4
0 sol:数据范围小的可怜,爆枚一个正确节点,n2模拟即可
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,m,a[N],b[N],Ans[N];
int main()
{
int i,j,Pos=-;
R(n); R(m);
for(i=;i<=n;i++) R(a[i]);
for(i=;i<=n;i++)
{
Ans[i]=;
b[i]=a[i];
for(j=i-;j>=;j--) b[j]=b[j+]-m;
for(j=i+;j<=n;j++) b[j]=b[j-]+m;
for(j=;j<=n;j++)
{
if(b[j]!=a[j]) Ans[i]++;
if(b[j]<=) {Ans[i]=0x3f3f3f3f; break;}
}
if((Pos==-)||(Ans[i]<Ans[Pos])) Pos=i;
}
Wl(Ans[Pos]);
b[Pos]=a[Pos];
for(i=Pos-;i>=;i--) b[i]=b[i+]-m;
for(i=Pos+;i<=n;i++) b[i]=b[i-]+m;
for(i=;i<=n;i++) if(a[i]!=b[i])
{
if(a[i]<b[i])
{
putchar('+'); putchar(' '); W(i); Wl(b[i]-a[i]);
}
else
{
putchar('-'); putchar(' '); W(i); Wl(a[i]-b[i]);
}
}
return ;
}
/*
Input
4 1
1 2 1 5
Output
2
+ 3 2
- 4 1 Input
4 1
1 2 3 4
Output
0
*/
最新文章
- iOS事件传递->;处理->;响应
- 团队项目——站立会议 DAY11
- Web前端优化最佳实践及工具集锦
- MongoDB的安全(五)
- Lua.LearningLua.7-userdata
- Spring强制使用CGLIB代理事务
- poj 1611 The Suspects(简单并查集)
- 24种设计模式--迭代模式【Iterator Pattern】
- UVA 11212 Editing a Book
- iOS多线程的基本使用
- Spring总结_02_Spring概述
- servlet上传文件报错(二)
- mysql order by 排序的问题
- 避免写慢SQL
- 斯巴达克斯诅咒者之战第三季/全集Spartacus迅雷下载
- git-采集编码搜索
- Oracle EBS AR 收款API收款方法标识无效
- SqlHelper类的编写
- 在返回值拒绝——reference
- CodeForces - 996B