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 ...
随机推荐
- 苹果与Windows双系统时间不同步的解决办法
步骤:打开C盘>Windows>System32,找到cmd.exe,右键以管理员的身份运行. Reg add HKLM\SYSTEM\CurrentControlSet\Control\ ...
- 前端知识点总结(html+css)部分
HTML 1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: - 写Html文件(充当模板的作用) ****** - 数据库获取数据,然后替换到html文件的指定位置(W ...
- Python基础语法复习
1.数据类型 List 列表 函数 append(): 在列表末尾追加. count(): 计算对象在列表中出现的次数. extend():将列表内容添加到列表中. index(): 计算对象在列表中 ...
- 转:判断js中的数据类型的几种方法
判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...
- 你真的了解volatile关键字吗?
volatile关键字经常在并发编程中使用,其特性是保证可见性以及有序性,但是关于volatile的使用仍然要小心,这需要明白volatile关键字的特性及实现的原理,这也是本篇文章的主要内容. 一. ...
- 从统计局采集最新的省市区县数据,纯js
本文更新(移步查阅): 19-04-15 新采集了2018的省市区三级坐标和行政区域边界 19-03-22 采集了2018的城市数据 18-11-28 采集了2017的城市数据 数据下载 GitHub ...
- [T-ARA][떠나지마][不要离开]
歌词来源:http://music.163.com/#/song?id=22704408 잊기엔 너무 사랑했나봐 [id-ggi-en neo-mu sa-lang-haen-na-bwa] 아직도 ...
- maven 第一个Web项目——HelloWorld
1.安装Maven,具体步骤,参照博客[maven的安装与配置]http://www.cnblogs.com/dyh004/p/8523260.html 2.配置阿里云为Maven中央仓库,具体步骤, ...
- C_数据结构_数组
//数组 # include <stdio.h> # include <malloc.h> //包含了 malloc 函数 # include <stdlib.h> ...
- hdu 1263 水果 结构的排序+sort自定义排序
水果 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...