16 多校 8 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的数组a和b,再给定m次操作,每次给定l和r,每次可以把[l,r]的数进行任意调换位置,问能否在转换后使得a数组变成b数组。
题解:用结构体存储a数组,一共两个域,一个是值,一个是下标,这个下标指的是他最后应该在的位置即这个值在b数组中的下标。随后m次操作可以看做是对a数组的lr这个区间进行m次sort,sort是根据下标从小到大排序,这样会使得这个数向他应该在的位置偏移,就是把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 (贪心排序)很巧妙的思路啊~的更多相关文章
- HDU 5821 Ball (贪心排序) -2016杭电多校联合第8场
题目:传送门. 题意:T组数据,每组给定一个n一个m,在给定两个长度为n的数组a和b,再给定m次操作,每次给定l和r,每次可以把[l,r]的数进行任意调换位置,问能否在转换后使得a数组变成b数组. 题 ...
- LOJ 3059 「HNOI2019」序列——贪心与前后缀的思路+线段树上二分
题目:https://loj.ac/problem/3059 一段 A 选一个 B 的话, B 是这段 A 的平均值.因为 \( \sum (A_i-B)^2 = \sum A_i^2 - 2*B \ ...
- “盛大游戏杯”第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 描述 在一个矩形的灰度图像上,每个 ...
- hdu 5821 (贪心排序) Ball
题目:这里 题意:T组数据,两个长度都为n的数组,有m次操作,操作是对a数组而言,每次操作给一个区间范围l,r,可以将这个区间内的数任意交换顺序,问经过m次操作后, 是否可以将a数组变为b数组. 输入 ...
- HDU 6034 Balala Power!(贪心+排序)
Balala Power! Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) ...
- HDU 4811 Ball 贪心
题目链接: 题目 Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) 问题描述 ...
- hdu 5821 Ball 贪心
Ball 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...
- 2018.09.16 atcoder Garbage Collector(贪心)
传送门 昨晚打比赛的时候不是很机智啊. 这道题贪心就能过了. 我们可以发现一个明显的结论,每次选的垃圾的距离从大到小排序之后,每个距离对答案的贡献的系数是5,5,7,9,11-也就是最远的是5,其余都 ...
- HDU 4442 Physical Examination(关于贪心排序)
这个题目用贪心来做,关键是怎么贪心最小,那就是排序的问题了. 加入给定两个数a1, b1, a2, b2.那么如果先选1再选2的话,总的耗费就是a1 + a1 * b2 + a2; 如果先选2再选1, ...
随机推荐
- CSS Hack 技巧
IE Hack IE系列浏览器的hack大略如下: _nowamagic:1px;———–ie6 *nowamagic:1px;———–ie7 nowamagic:1px\0;———-ie89 now ...
- Notes for 'Making elephants fly'
1. 技术陷阱:应是需求导向, 而不是技术导向. 2. 时机最重要:而不是创造力,团队,客户,产品,或技术. 3. 模仿:能模仿就模仿,不能模仿就创新.巧匠摹形,大师窃意. good artists ...
- spring cloud服务发现注解之@EnableDiscoveryClient与@EnableEurekaClient
使用服务发现的时候提到了两种注解,一种为@EnableDiscoveryClient,一种为@EnableEurekaClient,用法上基本一致,今天就来讲下两者,下文是从stackoverflow ...
- [转载]Python正则表达式匹配反斜杠'\'问题
转载自csdnblog:Python正则表达式匹配反斜杠'\'问题 在学习Python正则式的过程中,有一个问题一直困扰我,如何去匹配一个反斜杠(即“\”)? 一.引入 在学习了Python特殊字符和 ...
- 连接zookeeper+solr的服务器超时
在连接zookeeper+solr的服务器超时,如果检查各ip地址都没有变动,各部分工程也没有书写问题,那么连接超时,就可能是该服务器内存分配过少,性能不足的问题,扩大该服务器内存即可.
- 通过BeanPostProcessor理解Spring中Bean的生命周期
通过BeanPostProcessor理解Spring中Bean的生命周期及AOP原理 Spring源码解析(十一)Spring扩展接口InstantiationAwareBeanPostProces ...
- Oracle常用表和常见操作命令
一.说明 Oracle数据库数据库名.表名.字段名等不区分大小写,字段值区分大小写. Oracle单词之间一般用下划线连接:表名最后一般加s字段名最后一般没s. 二.常见数据表 dba_*DBA拥有的 ...
- 【框架】selenium运行失败后自动截图(三)
思路: 1.写一个类继承TestListenerAdapter,覆写onTestFailure()方法 2.在onTestFailure方法里,调用selenium的TakesScreenShot的g ...
- Python 自然语言处理笔记(一)
一. NLTK的几个常用函数 1. Concordance 实例如下: >>> text1.concordance("monstrous") Displaying ...
- jquery ready&&load用法
ready和load那一个先执行 DOM文档加载的步骤 (1) 解析HTML结构 (2) 加载外部脚本和样式表文件 (3) 解析并执行脚本代码 (4) 构造HTML DOM模型 //ready (5) ...