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: a1≤a2≤…≤aN
Constraints
- 2≤N≤50
- −106≤ai≤106
- 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
3 -2 5 -1
Sample Output 1
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
2 -1 -3
Sample Output 2
1 2 1
- After the first operation, a=(−4,−3) and the condition is now satisfied.
Sample Input 3
5 0 0 0 0 0
Sample Output 3
0
- The condition is satisfied already in the beginning.
#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;
}