NC212456. FinalBazarek
描述
输入描述
第一行一个整数n(1<=n<=1000000),表示商品数量。
接下来一行有n个整数,表示每件商品的价格,范围在[1,10^9]。
接下来一行有一个整数m(1<=m<=1000000),表示询问数量。
接下来m行,每行一个整数k[i](1<=k[i]<=n)。
输出描述
对于每个询问,输出一行表示保证奇数的情况下最大的总价。若无法满足要求,输出-1。
示例1
输入:
4 4 2 1 3 3 2 3 4
输出:
7 9 -1
C++ 解法, 执行用时: 609ms, 内存消耗: 39436K, 提交时间: 2022-04-27 19:39:04
#include<bits/stdc++.h> #define endl "\n" using namespace std; #define int long long int rd(void) { int x = 0, f = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int maxn = 1e6 + 10; long long ji[maxn], ou[maxn], a[maxn], b[maxn]; int k[maxn]; int t1, t2; bool cmp(int a, int b) { return a > b; } int n, m; signed main() { n = rd(); for (int i = 1; i <= n; i++) { b[i] = rd(); } sort(b + 1, b + 1 + n, cmp); for (int i = 1; i <= n; i++) { if (b[i] & 1) { ji[++t1] = b[i]; } else { ou[++t2] = b[i]; } } for (int i = 1; i <= t1; i++) { // cout << ji[i] << " "; ji[i] += ji[i - 1]; } // cout << endl; for (int j = 1; j <= t2; j++) { // cout << ou[j] << " "; ou[j] += ou[j - 1]; } // cout << endl; int x = 0; for (int i = 1; i <= n; i++) { if (b[i] & 1) x++; k[i] = x; } for (int i = 1; i <= n; i++) { b[i] += b[i - 1]; } m = rd(); while (m--) { x = rd(); // cout << k[x] << endl; if (k[x] & 1) { cout << b[x] << endl; } else { int t = k[x]; long long mx = -1000; if (t == 0) { if (t1 == 0) { cout << "-1\n"; } else { cout << ou[x - 1] + ji[1] << endl; } } else { if (t + 1 <= t1 && x - (t + 1) <= t2&&x-(t+1)>=0) { mx = max(mx, ou[x - (t + 1)] + ji[t + 1]); } if (t - 1 <= t1 && x - (t - 1) <= t2&&x-(t-1)>=0) { mx = max(mx, ou[x - (t - 1)] + ji[t - 1]); } if (mx == -1000||mx%2==0) { cout << -1 << endl; } else { cout << mx << endl; } } } } return 0; }