CodeForces 797B Odd sum

排序。

正的偶数肯定都是可以加进去的,因为加偶数不改变奇偶性。奇数从大到小排序,取个最大的前缀和。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;

int n;
int a[100010];

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);

	sort(a+1,a+1+n);
	int sum=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]%2==0&&a[i]>0) sum = sum+a[i];
	}

	int x=0;
	int y=-2000000000;
	for(int i=n;i>=1;i--)
	{
		if(a[i]%2==0) continue;
		x=x+a[i];
		if(x%2==0) continue;
		y = max(y,x);
	}

	printf("%d\n",sum+y);

	return 0;
}
时间: 2024-08-05 09:45:39

CodeForces 797B Odd sum的相关文章

Odd sum CodeForces - 797B

Odd sum CodeForces - 797B 好方法:贪心 糟糕(不用动脑)的方法:dp ans[i][0]表示到第i个和为偶数最大,ans[i][1]表示到第i个和为奇数最大. 但是,仍然容易写挂!(注意细节) 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 typedef long long LL; 6 LL ans[100010][2]

Codeforces Round #575 (Div. 3) B. Odd Sum Segments (构造,数学)

B. Odd Sum Segments time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are given an array a consisting of n integers a1,a2,-,an. You want to split it into exactly k non-empty non-intersecting

Codeforces 396B On Sum of Fractions 数论

题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/20076297 题目大意:给出一个n,ans = ∑(2≤i≤n)1/(v(i)*u(i)), v(i)为不大于i的最大素数,u(i)为大于i的最小素数, 求ans,输出以分式形式. 解题思路:一开始看到这道题1e9,暴力是不可能了,没什么思路,后来在纸上列了几项,突然想到高中时候求等差数列时候用到

Codeforces 396B On Sum of Fractions 规律题

题目链接:点击打开链接 我们把 1 / { u(i)*v(i) }拆开->  (1/(u(i)-v(i)) * ( 1/v(i) - 1/u(i) ) 若n +1  是素数,则显然(1/(u(i)-v(i)) * ( 1/v(i) - 1/u(i) ) 这样完全相同的式子有 u(i)-v(i) 个 那么就可以把前面系数约掉,那么剩下的式子就是 1/2 - 1/(n+1) 若不是,则我们找到第一个<=n的素数,即v(n) 和第一个>n的素数,即 u(n) 然后前面的 2-v(n)求和,即

Educational Codeforces Round 19 B. Odd sum(贪心或dp)

题意:给出一组数,从中拿出几个,要让它们之和最大并且为奇数. 这道题给出的n不大,贪心暴力一下就可以了.(-?-;) 1.贪心 我是先把数据大于0并且为偶数的数都先加起来(保证开始的sum是偶数),数据大于0且为奇数的存在a数组里,数据小于0的存在b数组里. 然后如果有a数组有奇数个,直接加起来输出就好了.奇*奇=奇 偶数个的话就从sum中拿出a1,b1.特判一下a1(a数组里面最小的那个)和b1(b数组里最大的那个)哪个的绝对值大. a1<b1 直接输出之前的sum,a1>=b1则输出sum

Codeforces 121A Lucky Sum

Lucky Sum Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 121A64-bit integer IO format: %I64d      Java class name: (Any) Petya loves lucky numbers. Everybody knows that lucky numbers are positive int

codeforces 85D D. Sum of Medians Vector的妙用

D. Sum of Medians Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/85/D Description In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive el

Codeforces 577B Modulo Sum:数学 结论【选数之和为m的倍数】

题目链接:http://codeforces.com/problemset/problem/448/C 题意: 给你n个数字,给定m. 问你是否能从中选出若干个数字,使得这些数字之和为m的倍数. 题解: 其实就是要找一些数字,使得之和mod m为0. 开一个vector,存当前已经能够构成的数字之和mod m之后的值. 一开始vector为空,然后枚举n个数字a[i],对于每个数字枚举当前vector中的值v[i],将没有出现过的(a[i]+v[i])%m值加入vector中. 最后判断下vec

Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)

F. SUM and REPLACE time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Let D(x) be the number of positive divisors of a positive integer x. For example, D(2)?=?2 (2 is divisible by 1 and 2), D(6)?