题目开头:Youare given sequence a1, a2, ..., an of integer numbers of length n. Your task is to find such subsequence that itssum is odd and maximum among all such subsequences. It's guaranteed that givensequence contains subsequence with odd sum.
题目链接:http://codeforces.com/problemset/problem/797/B
题目大意:给串数字(有正数有负数),找几个数字相加使得得到最大为奇数的和。
大概思路:正数都相加,得到的sum如果为奇数,则输出。如果得到的sum为偶数,则需要①减去一个正数的奇数 或者②加上一个负奇数 , 使得sum为奇数。 则①②就是找出一个绝对值最小的奇数m, 然后sum - m。
AC代码
#include<cstdio> #include<cmath> #include<algorithm> #include<cstring> using namespace std; const int MaxN = 1e5 + 5; int a[MaxN]; int main() { int n; int m = 100001; //找出最小的奇数(绝对值) long long sum = 0; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &a[i]); if(a[i] > 0) sum = sum + a[i]; if(a[i] % 2 != 0 && fabs(a[i]) < m) m = fabs(a[i]); } if(sum % 2 == 0) sum = sum - m; printf("%lld ", sum); }