AtCodr Non-decreasing_锻炼思维的好题

D - Non-decreasing


Time limit : 2sec / Memory limit : 256MB

Score : 600 points

Problem Statement

Snuke has an integer sequence, a, of length N. The i-th element of a (1-indexed) is ai.

He can perform the following operation any number of times:

  • Operation: Choose integers x and y between 1 and N (inclusive), and add ax to ay.

He would like to perform this operation between 0 and 2N times (inclusive) so that a satisfies the condition below. Show one such sequence of operations. It can be proved that such a sequence of operations always exists under the constraints in this problem.

  • Condition: a1a2aN

Constraints

  • 2N50
  • −106ai106
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a1 a2  aN

Output

Let m be the number of operations in your solution. In the first line, print m. In the i-th of the subsequent m lines, print the numbers x and y chosen in the i-th operation, with a space in between. The output will be considered correct if m is between 0 and 2N (inclusive) and a satisfies the condition after the m operations.


Sample Input 1

Copy
3
-2 5 -1

Sample Output 1

Copy
2
2 3
3 3
  • After the first operation, a=(−2,5,4).
  • After the second operation, a=(−2,5,8), and the condition is now satisfied.

Sample Input 2

Copy
2
-1 -3

Sample Output 2

Copy
1
2 1
  • After the first operation, a=(−4,3) and the condition is now satisfied.

Sample Input 3

Copy
5
0 0 0 0 0

Sample Output 3

Copy
0
  • The condition is satisfied already in the beginning.
题意:
给定一个序列,要求在2N次操作内将所给序列变成一个非递减序列。
操作x y:用序列中的第x位数加到序列中第y位数上

题解:
对于一个非负序列,循环a[i+1]+=a[i]即可。
对于一个非正序列,循环a[i]+=a[i+1]即可。
对于其他序列,考虑将其转化成上述两种序列。
如果abs(max_value)>=abs(min_value),序列中每个元素都累加max_value,序列就变成一个非负序列了。
否则,序列中每个元素都累加min_value,序列就变成了一个非正序列了。
时间复杂度O(N).

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define INF 0x3f3f3f3f

using namespace std;
const int maxn=55;
int N,a[maxn];

int main()
{
    while(~scanf("%d",&N))
    {
        int t_min=INF,t_max=-INF;
        int pos_min=1;
        int pos_max=1;
        for(int i=1;i<=N;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=N;i++)
        {
            if(a[i]<t_min)
                t_min=a[i],pos_min=i;
            if(a[i]>t_max)
                t_max=a[i],pos_max=i;
        }
        printf("%d\n",N*2-2);
        if(abs(t_min)<=abs(t_max))
        {
            for(int i=1;i<=N;i++)
            {
                if(i==pos_max) continue;
                printf("%d %d\n",pos_max,i);
            }
            for(int i=2;i<=N;i++)
                printf("%d %d\n",i-1,i);
        }
        else
        {
            for(int i=1;i<=N;i++)
            {
                if(i==pos_min) continue;
                printf("%d %d\n",pos_min,i);

            }
            for(int i=N-1;i>=1;i--)
                printf("%d %d\n",i+1,i);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值