Codeforces 797B - Odd sum
1 second
256 megabytes
standard input
standard output
You are given sequence a1, a2, ..., an of integer numbers of length n. Your task is to find such subsequence that its sum is odd and maximum among all such subsequences. It's guaranteed that given sequence contains subsequence with odd sum.
Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
You should write a program which finds sum of the best subsequence.
The first line contains integer number n (1 ≤ n ≤ 105).
The second line contains n integer numbers a1, a2, ..., an ( - 104 ≤ ai ≤ 104). The sequence contains at least one subsequence with odd sum.
Print sum of resulting subseqeuence.
4
-2 2 -3 1
3
3
2 -5 -3
-1
题目大意:求最大奇数和的子序列的和。
方法:先将正数加起来,得sum
如果sum为奇数,输出sum;
否则,sum1=sum-最小正奇数,sum2=sum+最大负奇数,输出max(sum1,sum2)。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<iostream>
#define ll long long
using namespace std;
const int N=1e5;
int a[N];
int temp1=-1e5,temp2=1e5;
int main()
{
int n;
int sum=;
int index1=-,index2=-;
int sum1=-0x7f7f7f7f,sum2=-0x7f7f7f7f;
cin>>n;
for(int i=;i<n;i++)
{
cin>>a[i];
if(a[i]>)
{
sum+=a[i];
if(a[i]%)
{
temp2=min(temp2,a[i]);
}
}
else if(a[i]<)
{
if(a[i]%)
temp1=max(temp1,a[i]);
}
}
if(sum%)cout<<sum<<endl;
else
{
sum1=sum-temp2;
sum2=sum+temp1;
cout<<max(sum1,sum2)<<endl;
}
return ;
}
有点乱,我是输入的时候就找最大负奇数和最小正奇数的。
Codeforces 797B - Odd sum的更多相关文章
- Odd sum CodeForces - 797B
Odd sum CodeForces - 797B 好方法:贪心 贪心2 糟糕(不用动脑)的方法:dp ans[i][0]表示到第i个和为偶数最大,ans[i][1]表示到第i个和为奇数最大. 但是, ...
- Codeforces Round #575 (Div. 3) B. Odd Sum Segments (构造,数学)
B. Odd Sum Segments time limit per test3 seconds memory limit per test256 megabytes inputstandard in ...
- Codeforces Round #617 (Div. 3)A. Array with Odd Sum(水题)
You are given an array aa consisting of nn integers. In one move, you can choose two indices 1≤i,j≤n ...
- Odd sum (对本菜鸡来说是个极坑题)
https://codeforces.com/problemset/problem/797/B time limit per test 1 second memory limit per test 2 ...
- B. Odd Sum Segments CF(分割数组)
题目地址 http://codeforces.com/contest/1196/problem/B B. Odd Sum Segments time limit per test 3 seconds ...
- Codeforces 396B On Sum of Fractions 数论
题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...
- codeforces 797B
B. Odd sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- codeforces 963A Alternating Sum
codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
随机推荐
- fjwc2019 D3T2 送分题
#185. 「2019冬令营提高组」送分题 这是原题..... P3615 如厕计划 手推一推你发现,显然男性不能多于女性. 然后你或许可以发现一个神奇的性质. 对于每个序列,我们记$M$为$1$,$ ...
- SSL/TLS代理(termination proxy)
A TLS termination proxy (or SSL termination proxy) is a proxy server that is used by an institution ...
- 【题解】Luogu P4979 矿洞:坍塌
原题传送门:P4979 矿洞:坍塌 这是某场膜你赛的题,最后我一百零几分rank三十几滚粗 这是我唯一ac的一题 这题比较简单qaq 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自己看看 ...
- answerOpenCV轮廓类问题解析
contour在opencv中是一个基础的数据结构,灵活运用的话,作用很大.以contour为关键字,在answerOpenCV中能够发现很多有趣的东西. 1.无法解决的问题 http://answe ...
- 20145208 蔡野 《网络对抗》Exp8 Web基础
20145208 蔡野 <网络对抗>Exp8 Web基础 本实践的具体要求有: (1).Web前端HTML(1分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POS ...
- 【Python028--引入文件】
一.打开文件 1.open()函数 打开模式 执行操作 ‘r’ 以只读方式打开文件(默认) ‘w’ 以写入的方式打开文件,会覆盖已存在的文件 ‘x’ 如果文件已经存在,使用此模式打开将引发异常 ...
- log4j2使用介绍
工作中,用到了log4j2,以前只接触过log4j,也没有太过深入,这次就稍微系统的学习了以下log4j2. 一.引入pom.xml 使用maven作为项目的构建环境,pom.xml使用slf4j,s ...
- topcoder srm 683 div1
problem1 link 肯定存在相邻两堆满足不会存在任何操作在这两堆之间进行.然后就成为一条链,那么只需要维护链的前缀和即可判断当前堆和前一堆之间需要多少次操作. problem2 link 对于 ...
- Elasticsearch查询Index以及删除
查询Index信息 GET /bank HTTP/1.1Host: localhost:9200 { "bank": { "aliases": {}, &quo ...
- oracle单行函数 之 数字函数
Round(数字 \ 列 [,保留小数的位数]):四舍五入 select Round(1234.45,1) from dual = 1234.5 Trunc(数字 \ 列 [,保留小数的位数] ...