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
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
Source
2016 Multi-University Training Contest 8
##题意:
有N个盒子,每个盒子最多装一个球. 球的颜色不一定相同.
现在要进行m次区间操作:
每次操作 [l, r] 后可以随意将区间内的球重新分配回去.
问经过上述操作后是否有可能达到给定的状态.
##题解:
贪心.
为每个球标记它在最终结果中的序号. 对于颜色相同的球:左边的尽量分配小的序号.
对于m次区间操作,就将区间[l,r]中的球按最终序号排序.
每次排序都相当于让区间中的球向它们的最终位置更近一步.
最终再比较是否每个球都到位即可.
官方题解:
假设有4个红球,初始时从左到右标为1,2,3,4。那么肯定存在一种方案,使得最后结束时红球的顺序没有改变,也是1,2,3,4。 那么就可以把同色球都写成若干个不同色球了。所以现在共有n个颜色互异的球。按照最终情况标上1,2,。。,n的序号,那么贪心的来每次操作就是把一个区间排序就行了。
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 1010
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;
typedef pair<int,int> pii;
pii ball[maxn];
int main(int argc, char const *argv[])
{
//IN;
int t; cin >> t;
while(t--)
{
int n, m;
scanf("%d %d", &n,&m);
for(int i=1; i<=n; i++) {
int x; scanf("%d", &x);
ball[i] = make_pair(0, x);
}
for(int i=1; i<=n; i++) {
int color; scanf("%d", &color);
for(int j=1; j<=n; j++) {
if(ball[j].second == color && !ball[j].first) {
ball[j].first = i;
break;
}
}
}
while(m--) {
int l,r; scanf("%d %d", &l, &r);
sort(ball+l, ball+r+1);
}
bool flag = 1;
for(int i=1; i<=n; i++) {
if(ball[i].first != i) {
flag = 0; break;
}
}
if(flag) puts("Yes");
else puts("No");
}
return 0;
}
HDU 5821 Ball (贪心)的更多相关文章
- hdu 5821 Ball 贪心
Ball 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...
- HDU 5821 Ball (贪心排序) -2016杭电多校联合第8场
题目:传送门. 题意:T组数据,每组给定一个n一个m,在给定两个长度为n的数组a和b,再给定m次操作,每次给定l和r,每次可以把[l,r]的数进行任意调换位置,问能否在转换后使得a数组变成b数组. 题 ...
- 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数组. 输入 ...
随机推荐
- HBase的Shell操作
1.进入命令行 bin/hbase shell 2.输入help 查看各种命令组. 命令是分组的,可以执行help 'general'查看general组的命令. 3.常用命令 --显示有哪些表 li ...
- php整理(四): mysql
PHP学习(四)---PHP与数据库MySql 主要有以下的内容: 1.怎么连接数据库 2.怎么操作数据库 (1)怎么执行sql语言 (2)怎么处理返回的结果集 方法一:面向过程(已经过时,只是了解) ...
- android中最先被执行的activity
像C.C++.JAVA都有一个主函数作为程序的入口点,但是Android中并没有一个明确的主窗口,那么在有多个Activity的情况下,最先被执行的是哪个呢?这完全取决于配置文件AndroidMain ...
- 配置hibernate根据实体类自动建表功能
Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 如何使用呢?很简单,只要在hibernate.cfg.xml里加上如下代码 Xml代码<propert ...
- Ural1076(km算法)
题目大意 给出n*n表格,第a[i,j]表示i到j的权值,现在我们要将每个a[i,j]=sum[j]-a[i,j], 求出当前二分图a[][]最小匹配 最小匹配只需将权值取负后,求二分图最大匹配,使用 ...
- bzoj3272 3638
好题,这道题可以用线段树来快速模拟费用流寻找最长增广路 这样修改怎么做也很显然了 type node=record s,lx,rx,mx,lp,rp,pb,pe:longint; end; ..*,. ...
- Codeforces Round #269 (Div. 2)
A 题意:给出6根木棍,如果有4根相同,2根不同,则构成“bear”,如果剩余两个相同,则构成“elephant” 用一个数组分别储存各个数字出现的次数,再判断即可 注意hash[i]==5的时候,也 ...
- BZOJ2337: [HNOI2011]XOR和路径
题解: 异或操作是每一位独立的,所以我们可以考虑每一位分开做. 假设当前正在处理第k位 那令f[i]表示从i到n 为1的概率.因为不是有向无环图(绿豆蛙的归宿),所以我们要用到高斯消元. 若有边i-& ...
- 学习PHP C扩展之面向对象开发方式 (转)
PHP OOP面向对象之C语言开发方式 学习PHP C扩展有一段时间了,PHP手册里大部分讲的PHP的函数开发方式,网上找OOP资料比较少,想起上个月测试redis 的时候,下载PHP扩展redis源 ...
- python - pip 从已有的安装列表 安装
已经安装好的机器:sudo pip freeze > install_list.list 需要安装的机器:sudo pip install -r install_list.list