codeforces contest 339
2016-11-23 22:44
218 查看
http://codeforces.com/contest/339
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
C 339C Xenia
and Weights
题意:给出已有的几种砝码,重量在[1,10],每种都有任意多个。
首先在天平的左边放一个,然后再右边,再左边,......
每次放完之后,该侧的砝码总重量必须大于另一侧,并且和上一次放的砝码种类不一样。问能否操作m次。
题解:暴搜。
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
D 339D Xenia
and Bit Operations
题意:对于一个是2的幂次个数的数组,我们定义它的val值。首先把相邻的数OR起来,得到新的序列,再XOR起来,直到最后剩下一个数。每次询问,单点修改一个值,求最后的val。
题解:线段树。
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
E 339E Three
Swaps
题意:把1~n的序列经过三次翻转,得到题目给出的序列,问经过了哪几次翻转(最多三次)。
题解:暴搜。
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总结
暴搜真的好难,复杂度不好计算,只能是看到这题,觉得它没有什么办法可以做,只能试着搜一下,然后再剪枝。
当然问题的解肯定要看起来比较特殊。
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
C 339C Xenia
and Weights
题意:给出已有的几种砝码,重量在[1,10],每种都有任意多个。
首先在天平的左边放一个,然后再右边,再左边,......
每次放完之后,该侧的砝码总重量必须大于另一侧,并且和上一次放的砝码种类不一样。问能否操作m次。
题解:暴搜。
#include <algorithm> #include <iostream> #include <cstring> #include <vector> #include <cstdio> #include <string> #include <cmath> #include <queue> #include <set> #include <map> using namespace std; typedef long long ll; #define de(x) cout << #x << "=" << x << endl char s[20]; int a[20],ans[1005]; int n,m; bool dfs(int det,int cnt,int pre) {//第cnt次 if(cnt==m+1) { return 1; } for(int i=1;i<=n;++i) { if(a[i]!=pre&&a[i]>det) { if(dfs(a[i]-det,cnt+1,a[i])) {//方案可行 ans[cnt]=a[i]; return 1; } } } return 0; } int main() { while(~scanf("%s%d",s+1,&m)) { n=0; for(int i=1;i<=10;++i) { if(s[i]-'0') a[++n]=i; } if(dfs(0,1,0)) { puts("YES"); for(int i=1;i<=m;++i) printf("%d%c",ans[i],(i==m)?'\n':' '); } else { puts("NO"); } } return 0; }
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
D 339D Xenia
and Bit Operations
题意:对于一个是2的幂次个数的数组,我们定义它的val值。首先把相邻的数OR起来,得到新的序列,再XOR起来,直到最后剩下一个数。每次询问,单点修改一个值,求最后的val。
题解:线段树。
#include <algorithm> #include <iostream> #include <cstring> #include <vector> #include <cstdio> #include <string> #include <cmath> #include <queue> #include <set> #include <map> using namespace std; typedef long long ll; #define de(x) cout << #x << "=" << x << endl const int N=524300; int tree ; int n,m; int build(int l,int r,int pos) { if(l==r) { scanf("%d",&tree[pos]); return 1;//上一级执行or } int mid=l+r>>1; int t=build(l,mid,pos<<1); build(mid+1,r,pos<<1|1); if(t) tree[pos]=tree[pos<<1]|tree[pos<<1|1]; else tree[pos]=tree[pos<<1]^tree[pos<<1|1]; return t^1; } int update(int l,int r,int pos,int p,int val) {//a[p]=val; if(l==r&&r==p) { tree[pos]=val; return 1; } int mid=l+r>>1; int t; if(p<=mid) { t=update(l,mid,pos<<1,p,val); } else { t=update(mid+1,r,pos<<1|1,p,val); } if(t) tree[pos]=tree[pos<<1]|tree[pos<<1|1]; else tree[pos]=tree[pos<<1]^tree[pos<<1|1]; return t^1; } int main() { while(~scanf("%d%d",&n,&m)) { build(1,1<<n,1); int a,b; for(int i=1;i<=m;++i) { scanf("%d%d",&a,&b); update(1,1<<n,1,a,b); printf("%d\n",tree[1]); } } return 0; }
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
E 339E Three
Swaps
题意:把1~n的序列经过三次翻转,得到题目给出的序列,问经过了哪几次翻转(最多三次)。
题解:暴搜。
#include <algorithm> #include <iostream> #include <cstring> #include <vector> #include <cstdio> #include <string> #include <cmath> #include <queue> #include <set> #include <map> using namespace std; typedef long long ll; #define de(x) cout << #x << "=" << x << endl const int N=1005; int a ; int n; vector<int> l,r; void dfs(int cnt) { int flag=1; for(int i=1;i<=n;++i) { if(a[i]!=i) { flag=0; break; } } if(flag) { int sz=l.size(); printf("%d\n",sz); for(int i=sz-1;i>=0;--i) { printf("%d %d\n",l[i],r[i]); } exit(0); } if(cnt>3) return ; //进行第cnt次操作 for(int i=1;i<=n;++i) { for(int j=i+1;j<=n;++j) { if(abs(a[j]-a[i-1])==1||abs(a[i]-a[j+1])==1) {//可以翻转 l.push_back(i);r.push_back(j); reverse(a+i,a+j+1); dfs(cnt+1); //还能走到这一步,表示上一个方案不可行 reverse(a+i,a+j+1); l.pop_back();r.pop_back(); } } } } int main() { while(~scanf("%d",&n)) { for(int i=1;i<=n;++i) scanf("%d",&a[i]); a[0]=0;a[n+1]=n+1; l.clear();r.clear(); dfs(1); } return 0; }
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总结
暴搜真的好难,复杂度不好计算,只能是看到这题,觉得它没有什么办法可以做,只能试着搜一下,然后再剪枝。
当然问题的解肯定要看起来比较特殊。
相关文章推荐
- codeforces contest 352
- Codeforces April Fools Contest 2017
- CodeForces - 725D Contest Balloons 贪心
- Codeforces 2016 ACM Amman Collegiate Programming Contest B. The Little Match Girl(贪心)
- 【74.89%】【codeforces 551A】GukiZ and Contest
- CodeForces-2015 HIAST Collegiate Programming Contest-Gym-100952A-Who is the winner?
- codeforces contest 551 problem E(分块)
- codeforces contest 855 problem B(前缀后缀)
- CodeForces 377B-Preparing for the Contest(二分+贪心)
- CodeForces 339D D. Xenia and Bit Operations(线段树的单点更新)
- CodeForces 339D————线段树之单点修改
- CodeForces-339B-Xenia and Ringroad
- CodeForces 339 A.Helpful Maths(水~)
- Codeforces 2016 ACM Amman Collegiate Programming Contest B. The Little Match Girl(贪心)
- Codeforces 339
- CodeForces 377 B. Preparing for the Contest
- codeforces contest 332
- codeforces 659B B. Qualifying Contest(水题+sort)
- Codeforces 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest I题(模拟)
- CodeForces 659 B. Qualifying Contest(结构体排序的问题)