[atcoder] agc86 D - Non-decreasing

D - Non-decreasing

题目链接:Non-decreasing

题目大意:

给一个长度为 n (1n50) 的序列。 每次可以选择两个数 ax,ay , 把 ax 加到 ay 上。最终使得a序列不递降, 题目保证 0 2n 次有解。 输出操作个数以及操作方案。

数据范围:

1n50
106ai106

解题思路:

这道题比赛的时候没想出来。C题5分钟写完就挂机了。。 比赛完了之后看了别的代码瞬间理解。
其实如果这个题 ai 全是正数或者全是负数, 应该都能想到从左往右或者从右到左累加一次, 即操作了n-1次, 就符合条件了.但是现在有正有负, 又不难发现, 只需要记录绝对值最大的数, 把这n个数都加上这个绝对值最大的数, 他们就符号统一了, 这个操作为n次, 所以题目才能保证在2n次肯定有解.

AC代码:

/********************************************
 *Author*        :ZZZZone
 *Created Time*  : 一 12/11 20:46:20 2017
 * Ended  Time*  : 一 12/11 21:04:29 2017
*********************************************/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<iostream>
using namespace std;

int n;
int a[55];

int main(){
    scanf("%d", &n);
    int p = 0;
    for(int i = 1; i <= n; i++){
        scanf("%d", &a[i]);
        if(abs(a[i]) >= abs(a[p])) p = i;
    }
    printf("%d\n", 2 * n - 1);
    for(int i = 1; i <= n; i++) printf("%d %d\n", p, i);
    if(a[p] > 0) for(int i = 1; i < n; i++) printf("%d %d\n", i, i + 1);
    else for(int i = n; i > 1; i--) printf("%d %d\n", i, i - 1);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zzzzone/article/details/78777797
今日推荐