Odd sum CF
来源:互联网 发布:淘宝助手创建宝贝模板 编辑:程序博客网 时间:2024/06/04 19:26
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 numbern (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
32 -5 -3
-1
In the first example sum of the second and the fourth elements is 3.
题目的意思就是说从一个序列中挑选子序列,使得这个子序列的和是最大的奇数
先考虑极端情况
1.所有的元素都是正的,容易想到是对这n个元素求和,判断是不是奇数,如果是 直接输出,如果不是,就减去n个元素中最小的偶数。
2.所有的元素都是负的,那么直接找到最大的奇元素就好了;
进行推广
如果又有正的,又有负的,那么就对整数元素进行求和,盘算是不是奇数,如果是直接输出,如果不是,求max(最大整数和-最小正偶数,最大整数和+最大负偶数)
下面附上代码
#include<bits/stdc++.h>using namespace std;int main(){ int n; scanf("%d",&n); int sum=0; int minn=0x3f3f3f3f; int maxx=-0x3f3f3f3f; for(int i=0;i<n;i++) { int t; scanf("%d",&t); if(t>0) sum+=t; if(t<0&&t%2&&t>maxx) maxx=t; if(t>0&&t%2&&t<minn) minn=t; } if(sum%2==0) { cout<<max(sum-minn,sum+maxx)<<endl; } else cout<<sum<<endl;}
- Odd sum CF
- uva10783 Odd Sum
- UVa 10783 - Odd Sum
- UVA 10783 - Odd Sum
- UVa 10783-Odd Sum
- B. Odd sum
- Sicily 12986. An Odd Sum
- Sum All Odd Fibonacci Numbers
- CodeForces 797B Odd sum
- codeforces 797B Odd sum
- Codeforces 797B Odd sum
- Sum All Odd Fibonacci Numbers
- CodeWars:Sum of odd numbers
- Sum All Odd Fibonacci Numbers
- CF 710C Magic Odd Square
- CF Modulo Sum
- CF--A. Tricky Sum
- CF Lucky Sum
- 什么是Spring,IOC是什么?DI是什么?
- Reverse String
- xposed hook入门篇
- 快速入门Vue.js
- [BZOJ1211][HNOI2004]树的计数(prufer序列+数学相关)
- Odd sum CF
- adb
- Redis 一、数据结构与对象--五大数据类型的底层结构实现
- OllyDBG完美教程(超强入门级)
- JavaWeb前端知识-JQuery
- 在没有图形界面的vps里使用selenium
- CSU1335 高桥和地桥
- 《你的灯亮着吗》读后小结
- 二阶IIR滤波器结构与FPGA实现分析