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, ...
随机推荐
- 函数指针做函数参数,其中有typedef的相关,感觉这是构成大河的小溪
#include<stdio.h> #include<stdlib.h> #include<string.h> int Funcadd(int a, int b) ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
- 转载-Mac下面的SecureCRT(附破解方案) 更新到最新的8.0.2
原帖地址:http://bbs.feng.com/read-htm-tid-6939481.html,爱死楼主了,哈哈 真心感谢 继续更新到8.3.0的破解,整体的破解方案都发生了的变化首先还是去ht ...
- CRM WEB UI 04明细界面添加按钮
好了,这个是个人测试玩的,略风骚...请自行鉴阅 1.明细的组件控制器中增加全局控制属性字段: 2.概览页中工具栏相关方法,重定义GET_BUTTONS METHOD IF_BSP_WD_TOOLBA ...
- Spring ApplicationListener使用方法及问题
使用场景 在一些业务场景中,当容器初始化完成之后,需要处理一些操作,比如一些数据的加载.初始化缓存.特定任务的注册等等.这个时候我们就可以使用Spring提供的ApplicationListener来 ...
- Hadoop--Unable to load native-hadoop library for your platform... using builtin-java classes where applicable Starting namenodes on [localhost]
Unable to load native-hadoop library for your platform... using builtin-java classes where applicabl ...
- JSP调试技巧
我先谈谈: 我的经验就是多装几个服务器,这个查不出错误,用另一个,这个方法很好用. ---------------------------------------------------------- ...
- linux定时删除文件脚本
#! /bin/sh # 配置项DEBUG=truefolderDir=/var/www/html/hlsrecord/EXPIRE_DAY=1 # 过期时间和时间戳deadTime=`date -d ...
- web前端开发面试题(答案)
1.xhtml和html有什么区别? HTML是一种基本的WEB网页设计语言,XHTML是一个基于XML的置标语言最主要的不同:XHTML 元素必须被正确地嵌套.XHTML 元素必须被关闭.标签名必须 ...
- css制作tips提示框,气泡框,制作三角形
有时候我们的页面会需要这样的一些提示框或者叫气泡框,运用css,我们可以实现这样的效果. 为了实现上面的效果,我们首先要理解如何制作三角形. 当我们给一个DIV不同颜色的边框的时候,我们可以得到下面的 ...