hdu 5821 Ball 贪心
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<=n<=1000,0<=m<=1000, sum of n over all testcases <=2000, sum of m over all testcases <=2000.
0<=a[i],b[i]<=n.
1<=l[i]<=r[i]<=n.
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
Hint
题意
给你a[i]数组,给你b[i]数组
然后有m次交换,这m次交换是依次进行的,每次给你一个区间,就代表这个区间可以随意互换。
问你最后能不能得到B区间。
题解:
网络流是错的,虽然网络流能AC,但是这个做法显然是错的。
正解应该是贪心,对于最左边的值,那么他一定对应着在B[i]数组中与他相同的,且最左边的数。
根据这个,贪心的去扫一遍就好了,其实就是不断排序,为什么?
因为排序之后,一定会使得从目标到结束状态花费变得最小。
代码
#include <bits/stdc++.h>
#define rep(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define drep(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define pb push_back
#define mp make_pair
#define sf scanf
#define pf printf
#define two(x) (1<<(x))
#define clr(x,y) memset((x),(y),sizeof((x)))
#define dbg(x) cout << #x << "=" << x << endl;
const int mod = 1e9 + 7;
int mul(int x,int y){return 1LL*x*y%mod;}
int qpow(int x , int y){int res=1;while(y){if(y&1) res=mul(res,x) ; y>>=1 ; x=mul(x,x);} return res;}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
using namespace std;
const int maxn = 1000 + 15;
int a[maxn],b[maxn],ptr[maxn],l[maxn],r[maxn];
vector < int > po[maxn];
int main(int argc,char *argv[]){
int T=read();
while(T--){
int N=read(),M=read();
rep(i,0,N){
po[i].clear();
ptr[i]=0;
}
rep(i,1,N){
a[i]=read();
}
rep(i,1,N){
b[i]=read();
po[b[i]].pb(i);
}
bool ans = true;
rep(i,1,N){
if(ptr[a[i]] == po[a[i]].size()) ans = false;
else a[i]=po[a[i]][ptr[a[i]]++];
}
rep(i,1,M) l[i]=read(),r[i]=read();
if( ans == false ) pf("No\n");
else{
rep(i,1,M) sort(a+l[i],a+r[i]+1);
rep(i,1,N) if(a[i]!=i) ans = false;
if(ans == true) pf("Yes\n");
else pf("No\n");
}
}
return 0;
}
hdu 5821 Ball 贪心的更多相关文章
- HDU 5821 Ball (贪心排序) -2016杭电多校联合第8场
题目:传送门. 题意:T组数据,每组给定一个n一个m,在给定两个长度为n的数组a和b,再给定m次操作,每次给定l和r,每次可以把[l,r]的数进行任意调换位置,问能否在转换后使得a数组变成b数组. 题 ...
- HDU 5821 Ball (贪心)
Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...
- HDU 5821 Ball (排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5821 有n个盒子,每个盒子最多装一个球. 现在进行m次操作,每次操作可以将l到r之间盒子的球任意交换. ...
- HDU 4811 Ball 贪心
题目链接: 题目 Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) 问题描述 ...
- hdu 5821 Ball 思维题
Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- HDU 5821 Ball
记录一下每个位置最终到达的位置.然后每次操作排序. #pragma comment(linker, "/STACK:1024000000,1024000000") #include ...
- Hdu 4864(Task 贪心)(Java实现)
Hdu 4864(Task 贪心) 原题链接 题意:给定n台机器和m个任务,任务和机器都有工作时间值和工作等级值,一个机器只能执行一个任务,且执行任务的条件位机器的两个值都大于等于任务的值,每完成一个 ...
- D - 淡黄的长裙 HDU - 4221(贪心)
D - 淡黄的长裙 HDU - 4221(贪心) James is almost mad! Currently, he was assigned a lot of works to do, so ma ...
- hdu 5821 (贪心排序) Ball
题目:这里 题意:T组数据,两个长度都为n的数组,有m次操作,操作是对a数组而言,每次操作给一个区间范围l,r,可以将这个区间内的数任意交换顺序,问经过m次操作后, 是否可以将a数组变为b数组. 输入 ...
随机推荐
- Repeater控件的分页实现
本文讲解Repeater控件与PagedDataSource相结合实现其分页功能.PagedDataSource 类封装那些允许数据源控件(如 DataGrid.GridView)执行分页操作的属性. ...
- 视觉中的经典图像特征小结(一): 颜色直方图, HOG, LBP
[普兒原创, 如有错误和纰漏欢迎指正. 更新中...] 1. 颜色直方图 颜色空间在本质上是定义在某种坐标系统下的子空间,空间中的每一个坐标表示一种不同的颜色.颜色空间的目的在于给出某种颜色标准,使得 ...
- 以python代码解释fork系统调用
import os print('Process (%s) start...' % os.getpid()) # Only works on Unix/Linux/Mac: pid = os.fork ...
- py-faster-rcnn代码阅读2-config.py
简介 该文件指定了用于fast rcnn训练的默认config选项,不能随意更改,如需更改,应当用yaml再写一个config_file,然后使用cfg_from_file(filename)导入以 ...
- IIS部署常见问题总结
1.通过网站子目录访问后台管理 场景:前台和后台分为两个项目开发,域名只有一个(t.cn),作为前台的地址,后台的地址希望指定为 t.cn/admin 项目部署的时候,需要把后台项目转化为/admin ...
- js async await 终极异步解决方案
既然有了promise 为什么还要有async await ? 当然是promise 也不是完美的异步解决方案,而 async await 的写法看起来更加简单且容易理解. 回顾 Promise Pr ...
- 关于 VS 2010 和 VS 2013 的警告 LNK4042
由于我最近调整了一下 Jimi 的文件结构,导致出现了一个 LNK4042 的 warning,我并没有很重视,这个 warning 导致出现了一些错误. 我调试了几个小时,一开始并没有想到是这个 w ...
- CF1006C 【Three Parts of the Array】
二分查找水题 记$sum[i]$为$d[i]$的前缀和数组 枚举第一段区间的结尾$i$ 然后二分出$lower$_$bound(sum[n]-sum[i])$的位置$x$,如果$sum[x]$与$su ...
- NOIP2018 货币系统
题面 思路 先分析一下,a集合的子集肯定不存在可以用它来表示的数,a集合是不能够表示的. 所以问题简化了成为选出a的一个子集(个数最少),能够表达a集合所有能表达的数. 接下来继续分析 如:1 2 4 ...
- Java编程的逻辑 (76) - 并发容器 - 各种队列
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...