codeforces contest 339


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[N];
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[N];
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;
}

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

总结 
暴搜真的好难,复杂度不好计算,只能是看到这题,觉得它没有什么办法可以做,只能试着搜一下,然后再剪枝。
当然问题的解肯定要看起来比较特殊。

  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值