HDU 5821 Ball

有n个盒子,第i个盒子里有一个颜色为a[i]的球,依次进行m个给定的操作:重排li到ri盒子里的球。问最后能否达成给定的局面。
n,m,a[i]≤1000.

比赛时并不会诠释重排,试图用网络流用类似PIGS的方法建图,因为颜色的限制放弃了。
后来同学看了一份CE代码(核心数组move是系统关键字==),然后推断出了AC代码==。

假设有4个红球,初始时从左到右标为1,2,3,4。那么肯定存在一种方案,使得最后结束时红球的顺序没有改变,也是1,2,3,4。 那么就可以把同色球都写成若干个不同色球了。所以现在共有n个颜色互异的球。按照最终情况标上1,2,。。,n的序号,那么贪心的来每次操作就是把一个区间排序就行了。

如果一一对应确实当成排序就好了==,由相同颜色的球拓扑关系肯定可以不变又可以成功的把同色球分开。嗯,就是这样。

比赛的时候没想出来对不起大家QAQ。

贪心

#include <set>
#include <ctime>
#include <queue>
#include <cstdio>
#include <bitset>
#include <cctype>
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inf (1<<30)
#define INF (1ll<<62)
#define fi first
#define se second
#define rep(x,s,t) for(register int x=s,t_=t;x<t_;++x)
#define per(x,s,t) for(register int x=t-1,s_=s;x>=s_;--x)
#define prt(x) cout<<#x<<":"<<x<<" "
#define prtn(x) cout<<#x<<":"<<x<<endl
#define pb(x) push_back(x)
#define hash asfmaljkg
#define rank asfjhgskjf
#define y1 asggnja
#define y2 slfvm
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
template<class T>void sc(T &x){
    int f=1;char c;x=0;
    while(c=getchar(),c<48)if(c=='-')f=-1;
    do x=x*10+(c^48);
    while(c=getchar(),c>47);
    x*=f;
}
template<class T>void nt(T x){
    if(!x)return;
    nt(x/10);
    putchar(x%10+'0');
}
template<class T>void pt(T x){
    if(x<0)putchar('-'),x=-x;
    if(!x)putchar('0');
    else nt(x);
}
template<class T>void ptn(T x){
    pt(x);putchar('\n');
}
template<class T>void pts(T x){
    pt(x);putchar(' ');
}
template<class T>inline void Max(T &x,T y){if(x<y)x=y;}
template<class T>inline void Min(T &x,T y){if(x>y)x=y;}

int n,m;
const int maxn=1005;
int mark[maxn],allc;
int a[maxn],b[maxn];
void solve(){
    sc(n);sc(m);
    rep(i,0,n)sc(a[i]);
    rep(i,0,n)sc(b[i]);
    ++allc;
    int l,r;
    rep(i,0,n){
        bool ok=false;
        rep(j,0,n)if(mark[j]!=allc&&b[j]==a[i]){
            ok=true;
            mark[j]=allc;
            a[i]=j;
            b[j]=j;
            break;
        }
        if(!ok){
            puts("No");
            rep(j,0,m)sc(l),sc(r);
            return;
        }
    }
    rep(i,0,m){
        sc(l);sc(r);--l;
        sort(a+l,a+r);
    }
    rep(i,0,n)if(a[i]^b[i]){
        puts("No");
        return;
    }
    puts("Yes");
}

int main(){
//  freopen("pro.in","r",stdin);
//  freopen("chk.out","w",stdout);
    int cas;sc(cas);
    while(cas--)solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值