AtCoder Beginner Contest 081 D - 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: 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.

题意: 给你n个数,让你通过移位相加,使得数列非递减

分析: 找到绝对值最大的值,如果该值为正,所有数都加上该值,使得所有数都大于零,然后从左向右扫一遍即可,如果是负数,从右向左扫一遍即可

参考代码

#include

using namespace std;


int main(){
    ios_base::sync_with_stdio(0);
    int n;cin>>n;
    int ma = 0;
    int inx = -1;
    for(int i = 1;i <= n;i++) {
        int x;cin>>x;
        if(abs(x) > abs(ma)) {
            ma = x;
            inx = i;
        }
    }
    if(inx == -1) cout<<0<else {
        cout<<2*n-1<for(int i = 1;i <= n;i++) {
            cout<' '<if(ma > 0) {
            for(int i = 1;i < n;i++) {
                cout<' '<1<else {
            for(int i = n;i > 1;i--) {
                cout<' '<1<return 0;
}
  • 如有错误或遗漏,请私聊下UP,thx

你可能感兴趣的:(----,经典思维题,----)