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 ...
随机推荐
- linux中断源码分析 - 初始化(二)
本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 本篇文章主要讲述源码中是如何对中断进行一系列的初始化的. 回顾 在上一篇概述中,介绍了几个对于中断来说非常重要的 ...
- Lua 中的条件表达式
下面这代码段看上去很熟悉,就是C#里面的条件表达式,很多其它语言也都有这么一个条件表达式. ; ; string c = "c"; string d = "d" ...
- 认识ASP.NET Windows身份认证
本文摘自:细说ASP.NET Windows身份认证 Forms身份认证虽然使用广泛,不过,如果是在 Windows Active Directory 的环境中使用ASP.NET, 那么使用Windo ...
- odoo学习之带出信息
# 输入客户带出它默认的发运方式和包装方式 def on_change_partner_id_return(self,cr,uid,ids,partner_id,context=None): resu ...
- 阿里云Centos搭建jdk环境
当我们开始了自己的开发,那么云服务器是一定少不了的,当然也有很多同学只是在本地做开发研究. 这里记录一下我自己在阿里云上搭建环境的过程. 趁着优惠的时候,我在阿里云上购买了ECS云服务器,并且搭载了C ...
- Centos7.3下安装Jumpserver 1.0.0(支持windows组件)
Jumpserver最新版本支持windows组件,废话不多介绍了,下面直接介绍下部署过程: 0)系统环境 CentOS 7.3 IP: 192.168.10.210 [root@jumpserver ...
- Jmeter-使用Ultimate Thread Group插件来设置负载场景
前言: Jmeter插件相关请移步:https://www.jianshu.com/p/130c7fddeddf 自定义线程组:jp@gc - Ultimate Thread Group,功能强大,可 ...
- [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!
测试mysqld启动mysql server的时候,报如下错误: 2015-12-17 00:46:02 10785 [ERROR] Fatal error: Please read "Se ...
- Linux内核分析 笔记七 可执行程序的装载 ——by王玥
一.预处理.编译.链接和目标文件的格式 (一)可执行程序是怎么得来的? 1. 2.可执行文件的创建——预处理.编译和链接 shiyanlou:~/ $ cd Code ...
- zookeeper安装和使用 windows环境(转)
原文地址: http://blog.csdn.net/tlk20071/article/details/52028945 简介 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是G ...