首页 > 其他 > 详细

Codeforces Round #563 (Div. 2)A

时间:2019-06-08 11:52:10      阅读:132      评论:0      收藏:0      [点我收藏+]

A. Ehab Fails to Be Thanos

题目链接:http://codeforces.com/contest/1174/problem/A

题目

You’re given an array a of length 2n. Is it possible to reorder it in such way so that the sum of the first n elements isn’t equal to the sum of the last n elements?

Input
The first line contains an integer n(1≤n≤1000), where 2n is the number of elements in the array a.
The second line contains 2n space-separated integers a1, a2, …, a2n (1≤ai≤106) — the elements of the array a

Output
If there’s no solution, print “-1” (without quotes). Otherwise, print a single line containing 2n
space-separated integers. They must form a reordering of a. You are allowed to not change the order

题意

给你一个长度为2n的数组a,对其重新排序,使得前n和后n元素的总和不想等,若无法排序输出-1,可以则输出重新排序过的长度为2n的数组a。

思路

先判断a数组中的数是否一样,若一样则无法排序使其前n个数之和不为后n个数之和,输出-1;

否则,求出前n与后n个数的和,判断他们是否相等,如果相等,将a排序再顺序输出即可,不想等则直接将原来a数组反向输出即可。

 

//
// Created by hjy on 19-6-4.
//

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
int main()
{
    int n;
    while(cin>>n)
    {
        int a[maxn];
        int x;
        cin>>x;
        a[0]=x;
        int flag=a[0];
        int num=1;
        for(int i=1;i<2*n;i++)
        {
            cin>>a[i];
            if(a[i]==flag)
                num++;
        }
        if(num==2*n)
            cout<<"-1"<<endl;
        else
        {
            int l=accumulate(a,a+n,0);
            cerr<<"l="<<l<<endl;//cerr对提交无影响

            int r=accumulate(a+n,a+2*n,0);
            cerr<<"r="<<r<<endl;
            if(l!=r) {
                for(int i=0;i<2*n;i++)
                {
                    cout<<a[i]<< ;
                }
                cout<<endl;
            } else
            {
                sort(a,a+2*n);
                for(int i=0;i<2*n;i++)
                    cout<<a[i]<< ;
                cout<<endl;
            }

        }

    }
    return 0;
}

 

Codeforces Round #563 (Div. 2)A

原文:https://www.cnblogs.com/Vampire6/p/10989800.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!