Ball
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5821
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 1≤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.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 4Sample Output
No No Yes No YesSource
2016 Multi-University Training Contest 8##题意: 有N个盒子,每个盒子最多装一个球. 球的颜色不一定相同. 现在要进行m次区间操作: 每次操作 [l, r] 后可以随意将区间内的球重新分配回去. 问经过上述操作后是否有可能达到给定的状态.
##题解: 贪心. 为每个球标记它在最终结果中的序号. 对于颜色相同的球:左边的尽量分配小的序号. 对于m次区间操作,就将区间[l,r]中的球按最终序号排序. 每次排序都相当于让区间中的球向它们的最终位置更近一步. 最终再比较是否每个球都到位即可.
官方题解: 假设有4个红球,初始时从左到右标为1,2,3,4。那么肯定存在一种方案,使得最后结束时红球的顺序没有改变,也是1,2,3,4。 那么就可以把同色球都写成若干个不同色球了。所以现在共有n个颜色互异的球。按照最终情况标上1,2,。。,n的序号,那么贪心的来每次操作就是把一个区间排序就行了。
##代码: ``` cpp #include