HDU 5821 Ball(贪心)
来源:互联网 发布:大宗商品网络交易平台 编辑:程序博客网 时间:2024/05/18 00:11
Ball
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 138 Accepted Submission(s): 77
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. For1≤i≤n , 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.
You are given the initial configuration of the balls. For
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.
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
Author
学军中学
Source
2016 Multi-University Training Contest 8
Recommend
wange2014 | We have carefully selected several similar problems for you: 5831 5830 5829 5828 5827
题目大意:
有N个箱子,每个箱子有一个颜色为Ai的球,或没有球。给出M个区间,每次可以把这个区间的球重新排列,问经过这些排列后能不能形成序列B。
解题思路:
比赛时一眼看过去感觉像网络流的最大流,然后霸占着电脑建了两个小时的图,最后自己证明了这题用网络流不可做(╯°□°)╯︵ ┻━┻
正解是用贪心。我们可以观察发现,对于同一种颜色的球如果可以通过交换得到目标序列,那么第一个对应第一个,第二个对应第二个……一定是可行的。所以我们对B的每个球重新编号,让A中和它颜色一样的第一个球和它编号相同。这样就变成了N个不同的球,而且最终要求得到的是一个0~N-1的序列。所以,我们只要贪心地对每个区间排序,这样得到的一定是最接近目标序列的。最后我们判断,排序后的区间和目标序列是否相同,如果相同则输出“Yes”,否则输出“No”。
AC代码:
题目大意:
有N个箱子,每个箱子有一个颜色为Ai的球,或没有球。给出M个区间,每次可以把这个区间的球重新排列,问经过这些排列后能不能形成序列B。
解题思路:
比赛时一眼看过去感觉像网络流的最大流,然后霸占着电脑建了两个小时的图,最后自己证明了这题用网络流不可做(╯°□°)╯︵ ┻━┻
正解是用贪心。我们可以观察发现,对于同一种颜色的球如果可以通过交换得到目标序列,那么第一个对应第一个,第二个对应第二个……一定是可行的。所以我们对B的每个球重新编号,让A中和它颜色一样的第一个球和它编号相同。这样就变成了N个不同的球,而且最终要求得到的是一个0~N-1的序列。所以,我们只要贪心地对每个区间排序,这样得到的一定是最接近目标序列的。最后我们判断,排序后的区间和目标序列是否相同,如果相同则输出“Yes”,否则输出“No”。
AC代码:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <queue>using namespace std;const int maxn=1000+3;int N,M,A[maxn],B[maxn];bool vis[maxn];int main(){ int T; scanf("%d",&T); while(T--) { memset(vis,0,sizeof vis); bool flag=true; scanf("%d%d",&N,&M); for(int i=0;i<N;++i) scanf("%d",&A[i]); for(int i=0;i<N;++i) { scanf("%d",&B[i]); int j; for(j=0;j<N;++j) if(!vis[j]&&A[j]==B[i]) { A[j]=i; vis[j]=true; break; } if(j==N)//A中没有能够与其匹配的球 flag=false; B[i]=i; } for(int i=0;i<M;++i) { int l,r;//区间左右端点 scanf("%d%d",&l,&r); sort(A+l-1,A+r); } for(int i=0;i<N;++i) if(A[i]!=B[i])//A经过交换不能变成B { flag=false; break; } if(flag) puts("Yes"); else puts("No"); } return 0;}
0 0
- HDU 5821 Ball(贪心)
- [HDU 5821] Ball (贪心)
- HDU 5821 Ball (贪心)
- HDU 5821 Ball ( 贪心)
- HDU-5821-Ball(贪心)
- HDU 5821 Ball(贪心)
- HDU 5821 Ball(贪心)
- 贪心(Ball,HDU 5821)
- Hdu-5821 Ball(贪心)
- HDU 5821 Ball【贪心】
- HDU 5821 Ball 贪心
- hdu 5821 Ball 贪心(多校)
- HDU 5821 Ball 贪心+映射
- HDU 5821-A - Ball-贪心
- Hdu 5821 Ball 贪心+模拟
- hdu 5821 Ball 贪心+思维
- HDU 4811 Ball(贪心)
- HDU 4811 Ball(贪心)
- SIP库拷贝到项目中时遇到的问题总结
- IOC和AOP
- API Blueprint
- 如何让PJSIP 支持IPV6
- VOC Segmentation GT图像颜色表生成分析
- HDU 5821 Ball(贪心)
- LinkedBlockingQueue的put,add跟offer的区别
- css选择器——:only-of-type
- 八皇后问题解法(Lua版)
- QTreeWidget学习之拖拽节点后,需要点击两次节点才生效
- ADT下载地址,完整版
- jsp分页总结
- 【1】【匹配】HDU5821 Ball
- Android如何在局域网中发送广播