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. java项目中登陆时记住密码

    1.在登陆的时候记住密码,不知自动登陆: 2.登陆页面,填写用户名,密码,点击记住密码,下次进入登陆页面的时候,填写同样的用户名,密码自动填充(在不一次会话的情况下也就是说在不关闭浏览器的情况下): ...

  2. ActiveMQ broker和客户端之间的确认

    生产者发送消息:producer ---------> broker broker返回确认:broker ---------> producer 生产者发送同步消息,broker会返回Re ...

  3. pandas dataframe 过滤——apply最灵活!!!

    按照某特定string字段长度过滤: import pandas as pd df = pd.read_csv('filex.csv') df['A'] = df['A'].astype('str') ...

  4. URL to load resources from the classpath in Java

    In Java, you can load all kinds of resources using the same API but with different URL protocols: fi ...

  5. win7 忘记密码

    你可以找个PE来修改密码,用光盘或U盘做PE都行,现在很多PE都支持密码修改的!不过下面这个方法还是要用到PE:1. 进入pe2.进入c:\windows\system32下 更改magnify.ex ...

  6. ie edge 自动给数字加下划线

    <meta name="format-detection" content="telephone=no,email=no,address=no">

  7. laravel基于Bootstrap的成功和失败的提示信息和验证提示信息

    message.blade.php <!-- 成功提示框 --> @if(Session::has("success")) <div class="al ...

  8. es6新增的math函数有哪些

    Math.trunc():用于去除一个数的小数部分,返回整数部分. Math.sign():用来判断一个数到底是正数.负数.还是零. Math.cbrt():用于计算一个数的立方根. Math.hyp ...

  9. IDEA教程之导入maven项目

    通过从网上的开源项目下载源码,一般都是maven管理的项目,此类项目可以通过导入快捷运行项目,如图为下载的一个项目: 2 打开IDEA,点击第二个选项“Import Porject”,然后选择源码根目 ...

  10. OO第二次课程总结分析

    前几次的作业都是单线程的,总体来说和以前的思维模式和调试等存在着一定的挂钩,在设计上整体难度还不算太大,这次开始了多线程编程,难度可以说是质的飞跃,构思上所考虑的不止一点两点,在整体的基础上还要考虑线 ...