• codeforces402B


    Trees in a Row

     CodeForces - 402B 

    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 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

    Input
    4 1
    1 2 1 5
    Output
    2
    + 3 2
    - 4 1
    Input
    4 1
    1 2 3 4
    Output
    0

    sol:数据范围小的可怜,爆枚一个正确节点,n2模拟即可
    #include <bits/stdc++.h>
    using namespace std;
    typedef int ll;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0'); return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=1005;
    int n,m,a[N],b[N],Ans[N];
    int main()
    {
        int i,j,Pos=-1;
        R(n); R(m);
        for(i=1;i<=n;i++) R(a[i]);
        for(i=1;i<=n;i++)
        {
            Ans[i]=0;
            b[i]=a[i];
            for(j=i-1;j>=1;j--) b[j]=b[j+1]-m;
            for(j=i+1;j<=n;j++) b[j]=b[j-1]+m;
            for(j=1;j<=n;j++)
            {
                if(b[j]!=a[j]) Ans[i]++;
                if(b[j]<=0) {Ans[i]=0x3f3f3f3f; break;}
            }
            if((Pos==-1)||(Ans[i]<Ans[Pos])) Pos=i;
        }
        Wl(Ans[Pos]);
        b[Pos]=a[Pos];
        for(i=Pos-1;i>=1;i--) b[i]=b[i+1]-m;
        for(i=Pos+1;i<=n;i++) b[i]=b[i-1]+m;
        for(i=1;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 0;
    }
    /*
    Input
    4 1
    1 2 1 5
    Output
    2
    + 3 2
    - 4 1
    
    Input
    4 1
    1 2 3 4
    Output
    0
    */
    View Code
     
  • 相关阅读:
    CodeForces Round #545 Div.2
    HDU 2222 Keywords Search
    拓扑排序
    CodeForces Round #553 Div2
    CodeForces Round #552 Div.3
    CodeForces Round #549 Div.2
    #Leetcode# 997. Find the Town Judge
    Educational Codeforces Round 62
    #Leetcode# 524. Longest Word in Dictionary through Deleting
    圆方树小结
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10800924.html
Copyright © 2020-2023  润新知