2022.10.23 ACM-ICPC个人训练赛题解Problem.G

 

Problem Statement

 

You are given a sequence A=(A_1,A_2,\ldots,A_N)A=(A1,A2,…,A**N) of length NN consisting of non-negative integers.

Determine if there is an even number represented as the sum of two different elements of AA. If it exists, find the maximum such number.

 

Constraints

 

  • 2\leq N \leq 2\times 10^52≤N≤2×105

  • 0\leq A_i\leq 10^90≤A**i≤109

  • The elements of AA are distinct.

  • All values in the input are integers.

 

Input

 

The input is given from Standard Input in the following format:

N
A_1A 
1
•
  A_2A 
2
•
  \ldots… A_NA 
N
•
 

Output

 

Print -1 if there is no even number represented as the sum of two different elements of AA.

If such an even number exists, print the maximum such number.

Sample 1

InputcopyOutputcopy
3 2 3 46

The values represented as the sum of two distinct elements of AA are 55, 66, and 77. We have an even number here, and the maximum is 66.

Sample 2

InputcopyOutputcopy
2 1 0-1

The value represented as the sum of two distinct elements of AA is 11. We have no even number here, so -1 should be printed.

 

题意www:

输入整数n,紧接着输入n个整数。各个整数两两相加,输出这些和当中最大的偶数和。

 

代码www:

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
​
int n;
const int N=2e5+5;   //开数组的时候可能需要const来转变一下昂;***
int a[N];
int b[N];
int c;
int conta=0,contb=0;
​
int main() {
​
    cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>c;
        if(c&1) {      //把这些数分为奇数和偶数分别存起来,会发现规律的昂(奇+奇=偶、偶+偶=奇);***
            a[conta]=c;
            conta++;
        } else {
            b[contb]=c;     //在这里,要学会巧妙地敲入数组下标昂!
            contb++;
        }
    }
    sort(a,a+conta+1);
    sort(b,b+contb+1); //这里为什么要加一捏?(思考思考,回头补上答案嗷)
    int sum=-1;
    if(conta>=2) {                            //这里的>=2别有洞天嗷,注意观察并动笔写一写嗷;
        sum=max(sum,a[conta-1]+a[conta]);       //注意昂,可能奇数之和要比偶数之和大,但是这里,在奇数那里会进一步比较sum哒,所以不用担心吼吼吼;
    }
    if(contb>=2) {
        sum=max(sum,b[contb-1]+b[contb]);
    }
    cout<<sum<<'\n';
    return 0;
}

okk,一篇blog完成啦啦啦o(* ̄▽ ̄*)ブ

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值