codeforces——797B——Odd sum
来源:互联网 发布:djvu mac 打开 编辑:程序博客网 时间:2024/06/06 17:01
You are given sequence a1, a2, ..., an of integer numbers of lengthn. 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 numbersa1, 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.
求所给所给值的所有子集中 和最大的奇数 输出那个数
要注意算法。。
#include<cstdio>#include<iostream>#include<algorithm>#define MIN -1e9using namespace std;bool compare(int a,int b){ return a>b;}int main(){ int n; while(~scanf("%d",&n)) { int t2,ans=0,com[3]= {MIN,MIN,MIN},t3=MIN; while(n--) { scanf("%d",&t2); if(t2%2==0) { if(t2>0) ans+=t2; } else if(t2>0) { com[2]=t2; sort(com,com+3,compare); if(com[0]+com[1]+com[2]>0) { ans+=com[0]+com[1]; com[0]=com[2]; com[1]=com[2]=MIN; } } else if(t2>t3) t3=t2; } sort(com,com+3,compare); if(com[0]!=MIN) ans+=com[0]; else ans+=t3; if(com[1]!=MIN) if(t3+com[1]>0) ans+=t3+com[1]; cout<<ans<<endl; } return 0;}
- codeforces——797B——Odd sum
- CodeForces 797B Odd sum
- codeforces 797B Odd sum
- Codeforces 797B Odd sum
- 【CodeForces 797B】Odd sum(模拟)
- Educational Codeforces Round 19 B. Odd sum
- Educational Codeforces Round 19 B. Odd sum
- B. Odd sum
- Codeforces 710 C. Magic Odd Square(构造)——Educational Codeforces Round 16
- codeforces—— 770B —— Maximize Sum of Digits
- Codeforces Round #319 (Div. 2)——B. Modulo Sum(01背包)
- codeforces——B. Spreadsheets
- LeetCode—328. Odd Even Linked List
- Codeforces —— 359B Permutation
- CodeForces 154B——Colliders
- codeforces——B. Months and Years
- CodeForces 535B — Tavas and SaDDas
- 【codeforces】706B—Interesting drink
- 列表侧滑删除(一)
- 将java的image对象转换成jpeg图片文件形式保存
- 65、66、67
- fastjson 解析json
- 文章标题JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Pat
- codeforces——797B——Odd sum
- 调用类自身常量
- Sum of Consecutive Prime Numbers UVA
- 高斯混合模型(GMM)
- C++ Primer Plus, Chapter 14, excercise
- 决策树学习笔记
- WM9876声卡驱动框架
- HDU5093 Battle ships (BZOJ4554)
- UVA 567—Risk