【HDU5821】Ball(贪心)

记录一个菜逼的成长。。

题目大意:
给你一个a序列 和 b序列,问能否经过操作把a变成b。操作:取出l 到 r的数,可以以任意的顺序放回。
题解:
假设有4个红球,初始时从左到右标为1,2,3,4。那么肯定存在一种方案,使得最后结束时红球的顺序没有改变,也是1,2,3,4。 那么就可以把同色球都写成若干个不同色球了。所以现在共有n个颜色互异的球。按照最终情况标上1,2,。。,n的序号,那么贪心的来每次操作就是把一个区间排序就行了。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <deque>
#include <cctype>
#include <bitset>
#include <stack>
#include <cmath>
using namespace std;
#define ALL(v) (v).begin(),(v).end()
#define cl(a) memset(a,0,sizeof(a))
#define fin freopen("D://in.txt","r",stdin)
#define fout freopen("D://out.txt","w",stdout)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 1000 + 10;
template <typename T>
inline void read(T &x){
    T ans=0;
    char last=' ',ch=getchar();
    while(ch<'0' || ch>'9')last=ch,ch=getchar();
    while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    if(last=='-')ans=-ans;
    x = ans;
}
/***************************************************************/
PII a[maxn];
int b[maxn],vis[maxn];
bool cmp(PII a,PII b)
{
    return a.second < b.second;
}
int main()
{
    //fin;
    //fout;
    int T;scanf("%d",&T);
    while(T--){
        cl(vis);
        int n,m;
        bool ans = true;
        scanf("%d%d",&n,&m);
        for( int i = 1; i <= n; i++ )
            read(a[i].first);
        for( int i = 1; i <= n; i++ )
            read(b[i]);
        for( int i = 1; i <= n; i++ ){
            for( int j = 1; j <= n; j++ ){
                if(!vis[j] && b[j] == a[i].first){
                    vis[j] = 1;
                    a[i].second = j;
                    break;
                }
            }
        }
        int l,r;
        while(m--){
            scanf("%d%d",&l,&r);
            sort(a+l,a+r+1,cmp);
        }
        for( int i = 1; i <= n; i++ )
        if(a[i].second != i || a[i].first != b[i]){ans = false;break;}
        printf("%s\n",ans?"Yes":"No");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值