BZOJ4245 [ONTAK2015]OR-XOR 【贪心】
2018-05-26 19:30
363 查看
题目链接
题解
套路①
位运算当然要分位讨论,高位优先
考虑在\(or\)下,如果该位为\(0\),则每一位都为\(0\)
套路②
我们选m段异或和,转化为\(m\)个前缀和的点,且其中有一个是\(n\)
容易发现,该位结果要为0,则选取的前缀和该位都为\(0\)
所以贪心查找所有该位为\(0\)的,首先第\(n\)个前缀和一定要为\(0\),如果其它满足有至少\(m - 1\)个,那么该位答案为\(0\),剩余为\(1\)的打上标记不能选
如果不够,那这一位就没办法了,直接放弃
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<map> #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt) #define REP(i,n) for (int i = 1; i <= (n); i++) #define mp(a,b) make_pair<int,int>(a,b) #define cls(s) memset(s,0,sizeof(s)) #define cp pair<int,int> #define LL long long int using namespace std; const int maxn = 500005,maxm = 100005,INF = 1000000000; inline LL read(){ LL out = 0,flag = 1; char c = getchar(); while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();} while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();} return out * flag; } int n,m,tag[maxn]; LL a[maxn],ans; int main(){ n = read(); m = read(); REP(i,n) a[i] = read() ^ a[i - 1]; for (LL i = 1ll << 61; i; i >>= 1){ if (a & i){ ans += i; continue; } int cnt = 1; for (int j = 1; j < n; j++) if (!tag[j] && !(a[j] & i)) cnt++; if (cnt >= m){ for (int j = 1; j < n; j++) if (!tag[j] && (a[j] & i)) tag[j] = true; } else ans += i; } printf("%lld\n",ans); return 0; }
相关文章推荐
- bzoj 4245: [ONTAK2015]OR-XOR【按位贪心】
- 【BZOJ 4245】[ONTAK2015]OR-XOR 二进制贪心
- [BZOJ4245][ONTAK2015]OR-XOR(贪心)
- BZOJ 4245: [ONTAK2015]OR-XOR|进制类|贪心
- BZOJ 4245: [ONTAK2015]OR-XOR 贪心
- 【bzoj4245】[ONTAK2015]OR-XOR 按位拆分+贪心
- 【BZOJ4245】[ONTAK2015]OR-XOR 贪心
- BZOJ4245 ONTAK2015 OR-XOR(贪心)
- BZOJ 4245 [ONTAK2015]OR-XOR 贪心 思维
- bzoj 4245 [ONTAK2015]OR-XOR 贪心
- 【bzoj4245】[ONTAK2015]OR-XOR 贪心
- [bzoj4245][ONTAK2015]OR-XOR
- BZOJ 4245: [ONTAK2015]OR-XOR
- 【BZOJ】4245: [ONTAK2015]OR-XOR
- 4245: [ONTAK2015]OR-XOR 贪心+按位处理+前缀异或和
- BZOJ4245 [ONTAK2015]OR-XOR
- [ONTAK2015] [BZOJ4245] OR-XOR - 二进制
- bzoj 4245: [ONTAK2015]OR-XOR
- [乱搞] BZOJ 4245 [ONTAK2015]OR-XOR
- BZOJ 4245 ONTAK2015 OR-XOR