Hdu-5821 Ball(贪心)

来源:互联网 发布:apm地面站软件 安卓 编辑:程序博客网 时间:2024/05/17 09:15
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
54 10 0 1 10 1 1 11 44 10 0 1 10 0 2 21 44 21 0 0 00 0 0 11 33 44 21 0 0 00 0 0 13 41 35 21 1 2 2 02 2 1 1 01 32 4
 

Sample Output
NoNoYesNoYes


这题开始错误贪心卡了我好久,最后还是队友A了过去,智商不够。

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


#include<iostream>#include<string>#include<algorithm>#include<cstdlib>#include<cstdio>#include<set>#include<map>#include<vector>#include<cstring>#include<stack>#include<cmath>#include<queue>#define INF 0x3f3f3f3f#define eps 1e-9#define MAXN 1005using namespace std;int t,n,m,a[MAXN],b[MAXN],pre[MAXN];vector<int> color[MAXN];int main(){scanf("%d",&t);while(t--){memset(pre,0,sizeof(pre));scanf("%d%d",&n,&m);for(int i = 0;i <= n;i++) color[i].clear();for(int i = 1;i <= n;i++) scanf("%d",&a[i]);for(int i = 1;i <= n;i++) {scanf("%d",&b[i]);color[b[i]].push_back(i);}for(int i = 1;i <= n;i++)         a[i] = color[a[i]][pre[a[i]]++];        for(int i = 1;i <= m;i++)        {        int l,r;        scanf("%d%d",&l,&r);        sort(a+l,a+1+r);}bool flag = true;for(int i = 1;i <= n;i++) if(a[i] != i)  { flag = false; break; }if(flag) cout<<"Yes"<<endl;else cout<<"No"<<endl; } } 


0 0
原创粉丝点击