AtCoder Grand Contest 020 C - Median Sum
阿新 • • 發佈:2018-06-01
CI https TE ems and fin sub oot tco
題目:here
題解:要轉化一下,找所有子集的中間值,等價於找一個子集,滿足這個子集的和最接近整個序列的和的一半。也就是一個背包判斷可行性的問題。重點來了,bitset優化,至於為什麽?我也不懂啊啊啊啊!!!
註意:總和為奇數的時候。這些都不是重點,重點只有一句:bs | = bs << tp。復雜度變成了O( N^2 * max(Ai) / 64 ),這兒有個大佬
#pragma warning(disable:4996) #include<bitset> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define ll long long #define lson root<<1 #define rson root<<1|1 #define mid (l+r)>>1 #define mem(arr, in) memset(arr, in, sizeof(arr)) using namespace std; const int maxn = 4000005; int n; bitset<maxn> bs; int main() { while (cin >> n) {int res = 0; bs[0] = 1; for (int i = 1; i <= n; i++) { int tp; cin >> tp; res += tp; bs |= bs << tp; } int m = (res + 1) / 2; for (int i = m; i <= res; i++) if (bs[i]) { printf("%d\n", i); break; } } return 0; }
AtCoder Grand Contest 020 C - Median Sum