codeforces 797B
B. 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
In the first example sum of the second and the fourth elements is 3.
解题思路:
题意为求最大的和为奇数的子序列
那么先将所有正数加起来,如果和是奇数那就是最大的奇数和,如果是偶数的话,就要把它变为奇数
有两种情况:
1.减去最小的正奇数
2.加上最大的负奇数
最后判断一下两种情况的大小,取较大值
实现代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int m,a[],b[],c[];
int i;
cin>>m;
int k = ,l= ;
for(i=;i<m;i++)
{
cin>>a[i];
if(a[i]>)
b[k++] = a[i];
else
c[l++] = a[i];
}
sort(b,b+k);
int sum = ;
for(i=k-;i>=;i--)
{
sum += b[i];
}
if(sum%==)
{
int sum1=sum,sum2=sum;
sum = -;
for(i=;i<k;i++)
{
if(b[i]%==)
sum1-=b[i];
if(abs(sum1)%==){
sum = sum1;
break;
}
}
//cout<<"sum1:"<<sum1<<endl;
sort(c,c+l);
for(i=l-;i>=;i--){
if(abs(c[i])%==)
sum2+=c[i];
if(abs(sum2)%==){
sum = max(sum,sum2);
}
}
//cout<<"sum2:"<<sum2<<endl;
}
cout<<sum<<endl;
}
codeforces 797B的更多相关文章
- Odd sum CodeForces - 797B
Odd sum CodeForces - 797B 好方法:贪心 贪心2 糟糕(不用动脑)的方法:dp ans[i][0]表示到第i个和为偶数最大,ans[i][1]表示到第i个和为奇数最大. 但是, ...
- Codeforces 797B - Odd sum
B. Odd sum 题目链接:http://codeforces.com/problemset/problem/797/B time limit per test 1 second memory l ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- Luogu3524 POI2011 Party 图论、构造
题目传送门:https://www.luogu.org/problemnew/show/P3524 大意:给一个$N$个点的图,其中一定有一个大小为$\frac{2}{3}N$的团,程序需给出一个大小 ...
- 重装系统之win10不能进入bios界面
原因 自Win10发布以来,新出厂的预装Win10的电脑都默认在UEFI模式下启动操作系统.UEFI启动是一种新的主板引导项,正被看做是有近20多年历史的BIOS 的继任者.顾名思义,快速启动是可以提 ...
- css实现按钮固定在底部
实现类似如下图的功能: 采用如下的样式来控制:
- TRIO-basic指令--FLEXLINK
Type: Axis Command Syntax: FLEXLINK(base_dist, excite_dist, link_dist, base_in, base_out, excite_acc ...
- handlebars.js 自定义helper(过滤)
将对象数据渲染到页面上: id 插入公共样式: handlebars.js 自定义helper(过滤)demo <script id="tbody-content-template&q ...
- 矩形A + B HDU2524
题意 给你n*m的棋盘问有多少个矩形 分析 先看只有一行或一列的情况有1+2+....+n个,因为矩形的类型有1个最小单位格子n个,2个最小单位格子n-1个,n个最小单位格子有一个 code #inc ...
- 作业20171116 beta2及beta发布 成绩
申诉 对成绩有疑问或不同意见的同学,请在群里[@杨贵福]. 申诉时间截止2017年12月13日 17:00. 成绩 scrum01 scrum02 scrum03 scrum04 scrum05 sc ...
- Individual Project - Word_frequency
0x00 预先准备和时间规划 1.因为要用到visual studio 2013,准备学习C#,预计一天时间能基本使用. 3.了解需求并设计基本数据结构与大致流程 20min 2.根据提议实现simp ...
- linux及安全第四周总结
学习内容:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 一.用户态.内核态 权限分级——为了系统本身更稳定,使系统不宜崩溃.(并不是所有程序员缩写的代码都很健壮!!) x86 CP ...
- 关于HashMap和Hashtable的区别
Hashtable的应用非常广泛,HashMap是新框架中用来代替Hashtable的类,也就是说建议使用HashMap,不要使用Hashtable.可能你觉得Hashtable很好用,为什么不用呢? ...