hdu 5821 ball(多校第八场,贪心)

题目链接

Ball

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 386    Accepted Submission(s): 230


Problem Description
ZZX has a sequence of boxes numbered  1,2,...,n . Each box can contain at most one ball.

You are given the initial configuration of the balls. For  1in , if the  i -th box is empty then  a[i]=0 , otherwise the i-th box contains exactly one ball, the color of which is a[i], a positive integer. Balls with the same color cannot be distinguished.

He will perform m operations in order. At the i-th operation, he collects all the balls from boxes l[i],l[i]+1,...,r[i]-1,r[i], and then arbitrarily put them back to these boxes. (Note that each box should always contain at most one ball)

He wants to change the configuration of the balls from a[1..n] to b[1..n] (given in the same format as a[1..n]), using these operations. Please tell him whether it is possible to achieve his goal.
 

Input
First line contains an integer t. Then t testcases follow. 
In each testcase: First line contains two integers n and m. Second line contains a[1],a[2],...,a[n]. Third line contains b[1],b[2],...,b[n]. Each of the next m lines contains two integers l[i],r[i].

1<=n<=1000,0<=m<=1000, sum of n over all testcases <=2000, sum of m over all testcases <=2000.

0<=a[i],b[i]<=n.

1<=l[i]<=r[i]<=n.
 

Output
For each testcase, print "Yes" or "No" in a line.
 

Sample Input
  
  
5 4 1 0 0 1 1 0 1 1 1 1 4 4 1 0 0 1 1 0 0 2 2 1 4 4 2 1 0 0 0 0 0 0 1 1 3 3 4 4 2 1 0 0 0 0 0 0 1 3 4 1 3 5 2 1 1 2 2 0 2 2 1 1 0 1 3 2 4
 

Sample Output
  
  
No No Yes No Yes
 

Author
学军中学
 

Source


题解:这是个想法题目,比赛时想了2个多小时,当时这题过了好多个,名次刷刷刷下降。。。这个题可以预先判断这两个串的颜色种类数目以及数量肯定得相等,这一步简单,然后处理第一个串,在第二个串从左往右扫,找到相应颜色所目标到达的位置,记录到另一个数组c中,然后对每一个给的区间对数组c进行排序,做完所有操作之后看看c数组的元素是不是1,2,3,4...这样依次排列的即可,若是输出yes,不是输出no。



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


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000+10;
char s[maxn];
int a[maxn],b[maxn],c[maxn];
bool have[maxn];
int cnt1[maxn],cnt2[maxn];
int l[maxn],r[maxn];
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        memset(cnt1,0,sizeof(cnt1));
        memset(cnt2,0,sizeof(cnt2));
        memset(have,false,sizeof(have));
        memset(c,0,sizeof(c));
        memset(l,0,sizeof(l));
        memset(r,0,sizeof(r));
        for(int i=1;i<=n;i++) scanf("%d",&a[i]),cnt1[a[i]]++;
        for(int i=1;i<=n;i++) scanf("%d",&b[i]),cnt2[b[i]]++;
        for(int i=1;i<=m;i++) scanf("%d%d",&l[i],&r[i]);
        bool flag=true;
        for(int i=0;i<=n;i++)
        {
            if(cnt1[i]!=cnt2[i])
            {
                flag=false;
                break;
            }
        }
        if(!flag)
        {
            puts("No");
            continue;
        }
        for(int i=1;i<=n;i++)
        {
        	for(int j=1;j<=n;j++)
        	{
        		if(a[i]==b[j]&&!have[j])
        		{
        			c[i]=j;
        			have[j]=true;
        			break;
        		}
        	}
        }
        for(int i=1;i<=m;i++)
        {
            sort(c+l[i],c+r[i]+1);
        }
        bool ok=true;
        for(int i=1;i<=n;i++)
        {
            if(c[i]!=i)
            {
                ok=false;
                break;
            }
        }
        if(ok) puts("Yes");
        else puts("No");
    }
}


标程


#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
using namespace std;
#define y0 y0z
#define y1 y1z
#define yn ynz
#define j0 j0z
#define j1 j1z
#define jn jnz
#define tm tmz
#define buli(x) (__builtin_popcountll(x))
#define bur0(x) (__builtin_ctzll(x))
#define bul2(x) (63-__builtin_clzll(x))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)
#define pw(x) ((ll(1))<<(x))
#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))
#define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a))
void getre(){int x=0;printf("%d\n",1/x);}
void gettle(){int res=1;while(1)res<<=1;printf("%d\n",res);}
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}
template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}
template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}
template<typename N>N gcd(N a,N b){return b?gcd(b,a%b):a;}
template<typename N>inline int sgn(N a){return a>0?1:(a<0?-1:0);}
#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
    #define lld "%I64d"
#else
    #define lld "%lld"
#endif
inline void gn(long long&x){
	int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
	while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
inline void gn(double&x){double t;scanf("%lf",&t);x=t;}
inline void gn(long double&x){double t;scanf("%lf",&t);x=t;}
inline void gs(char *s){scanf("%s",s);}
inline void gc(char &c){while((c=getchar())>126 || c<33);}
inline void pc(char c){putchar(c);}
#ifdef JCVB
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...) 
#endif
typedef long long ll;
typedef double db;
inline ll sqr(ll a){return a*a;}
inline db sqrf(db a){return a*a;}
const int inf=0x3f3f3f3f;
const db pi=3.14159265358979323846264338327950288L;
const db eps=1e-6;
const int mo=0;
int qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;}
int n,m;
int a[1111],b[1111];
int cnt[1111];
vi arr[1111];
int main()
{
#ifdef JCVB
	freopen("1.in","r",stdin);
	freopen("1.out","w",stdout);
	int _time_jc=clock();
#endif
	int te;gn(te);
	while(te--){
		gn(n);gn(m);
		rep(i,1,n+1)gn(a[i]);
		rep(i,1,n+1)gn(b[i]);
		rep(i,0,n+1)cnt[i]=0;
		rep(i,1,n+1){
			cnt[a[i]]++;
		}
		rep(i,1,n+1){
			cnt[b[i]]--;
		}
		int bo=1;
		rep(i,0,n+1)if(cnt[i]){
			bo=0;
			printf("No\n");
			break;
		}
		if(bo==0){
			while(m--){
				int l,r;gn(l);gn(r);
			}
			continue;
		}
		rep(i,0,n+1)arr[i].clear();
		rep(i,1,n+1)arr[b[i]].pb(i);

		per(i,1,n+1){
			int t=a[i];
			a[i]=arr[t].back();
			arr[t].pop_back();
		}

		while(m--){
			int l,r;
			gn(l);gn(r);
			sort(a+l,a+r+1);
		}
		rep(i,1,n+1)if(a[i]!=i){
			bo=0;
			printf("No\n");
			break;
		}
		if(bo==0){
			continue;
		}else{
			printf("Yes\n");
		}
	}
#ifdef JCVB
	debug("time: %d\n",int(clock()-_time_jc));
#endif
	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值