您的位置:首页 > 编程语言 > C语言/C++

CF621A - Wet Shark and Odd and Even

2016-02-02 01:11 281 查看
题目大意:给你n个数,每个数选或不选,加起来得到一个偶数,求最大偶数。

排序,偶数在前,奇数在后,奇偶分别递减。偶数直接加,奇数两个两个加,扫一遍即可。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
struct aaa{
long long x;
bool friend operator<(aaa a,aaa b){
if(a.x%2==b.x%2)return a.x>b.x;
if(a.x%2==0)return 1;
return 0;
}
}a[110000];
int n;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
a[i].x=x;
}
sort(a+1,a+1+n);
long long ans=0;
for(int i=1;i<=n;i++){
if(a[i].x%2==0){
ans+=a[i].x;
}else{
if(i+1<=n){
ans+=a[i].x+a[i+1].x;
a[i].x=a[i+1].x=0;
}
}
}
cout<<ans<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ cf 贪心