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

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

Source


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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 1010
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

typedef pair<int,int> pii;

pii ball[maxn];

int main(int argc, char const *argv[])

{

//IN;

int t; cin >> t;
while(t--)
{
int n, m;
scanf("%d %d", &n,&m);
for(int i=1; i<=n; i++) {
int x; scanf("%d", &x);
ball[i] = make_pair(0, x);
}
for(int i=1; i<=n; i++) {
int color; scanf("%d", &color);
for(int j=1; j<=n; j++) {
if(ball[j].second == color && !ball[j].first) {
ball[j].first = i;
break;
}
}
} while(m--) {
int l,r; scanf("%d %d", &l, &r);
sort(ball+l, ball+r+1);
} bool flag = 1;
for(int i=1; i<=n; i++) {
if(ball[i].first != i) {
flag = 0; break;
}
} if(flag) puts("Yes");
else puts("No");
} return 0;

}

HDU 5821 Ball (贪心)的更多相关文章

  1. hdu 5821 Ball 贪心

    Ball 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...

  2. HDU 5821 Ball (贪心排序) -2016杭电多校联合第8场

    题目:传送门. 题意:T组数据,每组给定一个n一个m,在给定两个长度为n的数组a和b,再给定m次操作,每次给定l和r,每次可以把[l,r]的数进行任意调换位置,问能否在转换后使得a数组变成b数组. 题 ...

  3. HDU 5821 Ball (排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5821 有n个盒子,每个盒子最多装一个球. 现在进行m次操作,每次操作可以将l到r之间盒子的球任意交换. ...

  4. HDU 4811 Ball 贪心

    题目链接: 题目 Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) 问题描述 ...

  5. hdu 5821 Ball 思维题

    Ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  6. HDU 5821 Ball

    记录一下每个位置最终到达的位置.然后每次操作排序. #pragma comment(linker, "/STACK:1024000000,1024000000") #include ...

  7. Hdu 4864(Task 贪心)(Java实现)

    Hdu 4864(Task 贪心) 原题链接 题意:给定n台机器和m个任务,任务和机器都有工作时间值和工作等级值,一个机器只能执行一个任务,且执行任务的条件位机器的两个值都大于等于任务的值,每完成一个 ...

  8. D - 淡黄的长裙 HDU - 4221(贪心)

    D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...

  9. hdu 5821 (贪心排序) Ball

    题目:这里 题意:T组数据,两个长度都为n的数组,有m次操作,操作是对a数组而言,每次操作给一个区间范围l,r,可以将这个区间内的数任意交换顺序,问经过m次操作后, 是否可以将a数组变为b数组. 输入 ...

随机推荐

  1. cdev_init函数

    linux-2.6.22/include/linux/cdev.hstruct cdev {   struct kobject kobj;          // 每个 cdev 都是一个 kobje ...

  2. Java 基础之认识 Annotation

    Java 基础之认识 Annotation 从 JDK 1.5 版本开始,Java 语言提供了通用的 Annotation 功能,允许开发者定义和使用自己的 Annotation 类型.Annotat ...

  3. POJ 1166 The Clocks (爆搜 || 高斯消元)

    题目链接 题意: 输入提供9个钟表的位置(钟表的位置只能是0点.3点.6点.9点,分别用0.1.2.3)表示.而题目又提供了9的步骤表示可以用来调正钟的位置,例如1 ABDE表示此步可以在第一.二.四 ...

  4. LA 3029 - City Game (简单扫描线)

    题目链接 题意:给一个m*n的矩阵, 其中一些格子是空地(F), 其他是障碍(R).找一个全部由F 组成的面积最大的子矩阵, 输出其面积乘以3的结果. 思路:如果用枚举的方法,时间复杂度是O(m^2 ...

  5. sdut 2840 Best string Orz~ (dp)

    题目 题意:有n1个o, n2个r, n3个z, n4个~, 求有多少种组合使 组合出来的字符串的任意前缀都满足 o的个数>=r的个数, r的个数>=z的个数 …………………… 思路:递推 ...

  6. UVa 1638 (递推) Pole Arrangement

    很遗憾,这么好的一道题,自己没想出来,也许太心急了吧. 题意: 有长度为1.2.3...n的n个杆子排成一行.问从左到右看能看到l个杆子,从右往左看能看到r个杆子,有多少种排列方法. 分析: 设状态d ...

  7. php.ini配置中文详解

    ;;;;;;;;;;; ; 警告 ; ;;;;;;;;;;; ; 此配置文件是对于新安装的PHP的默认设置. ; 默认情况下,PHP使用此配置文件安装 ; 此配置针对开发目的,并且*不是*针对生产环境 ...

  8. C扩展Python - official docs - defining new type

    1. Code & Official_doc: THIS 2. My question. #include <Python.h> /* * 1.PyTypeObject doc, ...

  9. FFMPEG + SDL音频播放分析

    目录 [hide] 1 抽象流程: 2 关键实现: 2.1 main()函数 2.2 decode_thread()读取文件信息和音频包 2.3 stream_component_open():设置音 ...

  10. 编译安装lnmp

    http://wenku.baidu.com/view/ec45d5dd28ea81c758f578cc.html