等差数列

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


解题方法:暴力求解,将分别从1,2,3,4,5……开始的等差数列与所给数据比较
#include <iostream>
#include <cstring>
using namespace std;
int a[1005],b[1005];

int main()
{
    int n,k;
    int i,j;
    while(cin>>n>>k)
    {
        memset(b,0,sizeof(b));
        for(i=0;i<n;i++)
        cin>>a[i];
        for(i=1;i<1005;i++)
        {
            for(j=0;j<n;j++)
            {
                if(a[j]==i+j*k)
                b[i]++;
            }
        }
        int y=b[0];
        int f=0;
        for(i=1;i<1005;i++)
        if(y<b[i])
        {
            y=b[i];
            f=i;
        }
        cout<<n-y<<endl;
        for(i=0;i<n;i++)
        {
            if(a[i]<f+i*k)
            cout<<"+ "<<i+1<<" "<<f+i*k-a[i]<<endl;
            if(a[i]>f+i*k)
            cout<<"- "<<i+1<<" "<<a[i]-f-i*k<<endl;
        }
    }
    return 0;
}

 

错误思想:将输入数据分别减去(i-1)*k,如果输入数据是等差数列,这时每个数应该相等(实际输入不是等差数列),从中找出相同数最多的数,其它数则为应当增减的数,此方法存在漏洞:若输入数据为5个,d=1,输入1 1 2 3 4,则处理后为0 1 2 3 4,而题中要求数据大于0,故错误!其代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;
int a[1005],b[10000000];

int main()
{
    int n,k;
    int i,j,t,x,maxn,minn;
    while(cin>>n>>k)
    {
        minn=99999999;
        maxn=-99999999;
        memset(b,0,sizeof(b));
        for(i=0;i<n;i++)
        {
            scanf("%d",&x);
            a[i]=x-i*k;
            if(a[i]>maxn) maxn=a[i];
            if(a[i]<minn) minn=a[i];
        }
        t=0;
        for(i=maxn;i>=minn;i--)
        {
            for(j=0;j<n;j++)
            {
                if(a[j]==i)
                b[t]++;
            }
            t++;
        }

        int y=b[0];
        x=maxn-0;
        for(i=1;i<t;i++)
        {
            if(y<b[i])
            {
                y=b[i];
                x=maxn-i;
            }
        }
        cout<<n-y<<endl;
        for(i=0;i<n;i++)
        {
            if(a[i]>x)
            cout<<"- "<<i+1<<" "<<a[i]-x<<endl;
            if(a[i]<x)
            cout<<"+ "<<i+1<<" "<<x-a[i]<<endl;
        }
    }
    return 0;
}

 

 

posted @ 2015-11-06 22:36  茶飘香~  阅读(221)  评论(0编辑  收藏  举报