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

You are given the initial configuration of the balls. For 1≤i≤n1≤i≤n, if the ii-th box is empty then a[i]=0a[i]=0, otherwise the i-th box contains exactly one ball, the color of which is aii, 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 lii,lii+1,...,rii-1,rii, 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 a1..n1..n to b1..n1..n (given in the same format as a1..n1..n), using these operations. Please tell him whether it is possible to achieve his goal. 

InputFirst line contains an integer t. Then t testcases follow. 
In each testcase: First line contains two integers n and m. Second line contains a11,a22,...,ann. Third line contains b11,b22,...,bnn. Each of the next m lines contains two integers lii,rii.

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

0<=aii,bii<=n.

1<=lii<=rii<=n.OutputFor 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 非常巧妙的思路。以下题意和题解转自http://www.cnblogs.com/Ritchie/p/5761913.html

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

题解:用结构体存储a数组,一共两个域,一个是值,一个是下标,这个下标指的是他最后应该在的位置即这个值在b数组中的下标。随后m次操作可以看做是对a数组lr这个区间进行msortsort是根据下标从小到大排序,这样会使得这个数向他应该在的位置偏移,就是把a数组往b数组上靠,该向左的向左去,该向右的向右去,如果最后的时候都偏移到了自己应该在的位置就是Yes,否则就是No。复杂度O(max((n*n),(m*n*log(n))))

 #include <iostream>
#include <cstdio>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std; struct node
{
int num,x;
}a[];
int b[];
bool vis[]; bool cmp(node p,node q)
{
return p.x<q.x;
} int main()
{
int T,n,m,l,r;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
scanf("%d",&a[i].num);
memset(vis,false,sizeof(vis));
for(int i=;i<n;i++)
{
scanf("%d",&b[i]);
for(int j=;j<n;j++)
if(a[j].num==b[i]&&!vis[j])
{
a[j].x=i;
vis[j]=true;
break;
}
}
while(m--)
{
scanf("%d%d",&l,&r);
sort(a+l-,a+r,cmp);
}
bool flag=true;
for(int i=;i<n;i++)
{
if(!vis[i])
{
flag=false;
break;
}
if(a[i].x!=i)
{
flag=false;
break;
}
}
if(flag)
printf("Yes\n");
else
printf("No\n"); }
return ;
}

16 多校 8 Ball (贪心排序)很巧妙的思路啊~的更多相关文章

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

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

  2. LOJ 3059 「HNOI2019」序列——贪心与前后缀的思路+线段树上二分

    题目:https://loj.ac/problem/3059 一段 A 选一个 B 的话, B 是这段 A 的平均值.因为 \( \sum (A_i-B)^2 = \sum A_i^2 - 2*B \ ...

  3. “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp,K,二分+排序,L,矩阵快速幂,M,线段树区间更新+Lazy思想,N,超级快速幂+扩展欧里几德,O,BFS】

    黑白图像直方图 发布时间: 2017年7月9日 18:30   最后更新: 2017年7月10日 21:08   时间限制: 1000ms   内存限制: 128M 描述 在一个矩形的灰度图像上,每个 ...

  4. hdu 5821 (贪心排序) Ball

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

  5. HDU 6034 Balala Power!(贪心+排序)

    Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  6. HDU 4811 Ball 贪心

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

  7. hdu 5821 Ball 贪心

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

  8. 2018.09.16 atcoder Garbage Collector(贪心)

    传送门 昨晚打比赛的时候不是很机智啊. 这道题贪心就能过了. 我们可以发现一个明显的结论,每次选的垃圾的距离从大到小排序之后,每个距离对答案的贡献的系数是5,5,7,9,11-也就是最远的是5,其余都 ...

  9. HDU 4442 Physical Examination(关于贪心排序)

    这个题目用贪心来做,关键是怎么贪心最小,那就是排序的问题了. 加入给定两个数a1, b1, a2, b2.那么如果先选1再选2的话,总的耗费就是a1 + a1 * b2 + a2; 如果先选2再选1, ...

随机推荐

  1. 移动端rem适配 flex.js

    (function() { document.addEventListener('DOMContentLoaded', function () { var html = document.docume ...

  2. Java中涉及到金额业务的处理

    一.MySql数据库中如何定义关于金额字段: 建议定义成[DECIMAL]类型,而不是float或者是double,因为这个两者是以二进制储存的,存在一定的误差.具体事例可参考https://blog ...

  3. hdu-6341-搜索

    Problem J. Let Sudoku Rotate Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  4. Oracle11g ADG环境实施文档-1204

    Oracle11g adg 环境搭建实施手册-1204 2017年8月30日 9:16 11g adg 环境搭建实施手册-0824 2017年8月24日 10:18 ################# ...

  5. 解决PLSQL Developer 插入中文 乱码问题(转)

    原文地址:解决PLSQL Developer 插入中文 乱码问题 PLSQL Developer 插入中文 乱码问题,如图     这个是由于oracle服务器端字符编码 和 Oracle 客户端 字 ...

  6. python零碎知识点

    0.规范化 使用Ctrl+Alt+L可以将代码排列格式更加规范化 1.浮点数 1.23x109就是1.23e9或者12.3e8:0.000012可以写成1.2e-5 2.字符串 >>> ...

  7. div中文字上下居中

    <div class="title">Title</div> 1. 将div高度设成定值 2. 将line-height设成定值 3. 将text-alig ...

  8. 微信小程序 获取位置、移动选点、逆地址解析

    WGS- 地心坐标系,即GPS原始坐标体系.在中国,任何一个地图产品都不允许使用GPS坐标,据说是为了保密.GoogleEarth及GPS芯片使用. .GCJ-02火星坐标系,国测局02年发布的坐标体 ...

  9. html盒子水平和垂直居中

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. Android 平台架构

    Android 平台主要组件如下 Linux 内核 Android 平台的基础是 Linux 内核.例如,Android Runtime (ART) 依靠 Linux 内核来执行底层功能,例如线程和低 ...