您的位置:首页 > 其它

codeforces contest 339

2016-11-23 22:44 218 查看
http://codeforces.com/contest/339

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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;
}


//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总结 

暴搜真的好难,复杂度不好计算,只能是看到这题,觉得它没有什么办法可以做,只能试着搜一下,然后再剪枝。

当然问题的解肯定要看起来比较特殊。

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: