Description

The closing ceremony of Squanch Code Cup is held in the big hall with n × m seats, arranged in n rows, m seats in a row. Each seat has two coordinates (x, y) (1 ≤ x ≤ n, 1 ≤ y ≤ m).

There are two queues of people waiting to enter the hall: k people are standing at (0, 0) and n·m - k people are standing at (0, m + 1). Each person should have a ticket for a specific seat. If person p at (x, y) has ticket for seat (xp, yp) then he should walk |x - xp| + |y - yp| to get to his seat.

Each person has a stamina — the maximum distance, that the person agrees to walk. You should find out if this is possible to distribute all n·m tickets in such a way that each person has enough stamina to get to their seat.

Input

The first line of input contains two integers n and m (1 ≤ n·m ≤ 104) — the size of the hall.

The second line contains several integers. The first integer k (0 ≤ k ≤ n·m) — the number of people at (0, 0). The following k integers indicate stamina of each person there.

The third line also contains several integers. The first integer l (l = n·m - k) — the number of people at (0, m + 1). The following l integers indicate stamina of each person there.

The stamina of the person is a positive integer less that or equal to n + m.

Output

If it is possible to distribute tickets between people in the described manner print "YES", otherwise print "NO".

Examples
Input
2 2
3 3 3 2
1 3
Output
YES
Input
2 2
3 2 3 3
1 2
Output
NO

正解:贪心
解题报告:

  因为我们想使得到两个出发点的距离小,但是不能同时保证两个,那么我们只能先保证一个最优,才想办法判断另外一个。显然把所有点按到左下角距离排序,可以对于左下角的点判断可达,然后我们每次走离左上角尽可能远的点,这样相当于是帮左上角的点分担了一部分远的点,同时可以保证合法性。

 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int MAXN = ;
int n,m,k,tot;
int a[MAXN];
struct node{
int x,y,z,dis;
bool operator < (const node &a) const{
return a.z>z;
}
}s[MAXN]; priority_queue<node>Q;
inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} inline bool cmp(node q,node qq){ return q.dis<qq.dis; } inline void work(){
n=getint(); m=getint(); k=getint();
for(int i=;i<=k;i++) a[i]=getint();
sort(a+,a+k+);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
s[++tot].x=i,s[tot].y=j,s[tot].z=m+-j+i,s[tot].dis=i+j;
sort(s+,s+tot+,cmp); int u=; bool ok=true; for(int i=;i<=k;i++) {
while(a[i]>=s[u].dis && u<=tot) Q.push(s[u]),u++;
if(Q.empty()) { ok=false; break; }
Q.pop();//清空
}
if(!ok) { printf("NO"); return; }
while(u<=tot) Q.push(s[u]),u++; k=getint(); for(int i=;i<=k;i++) a[i]=getint();
sort(a+,a+k+);
for(int i=k;i>=;i--) {
if(a[i]<Q.top().z) { ok=false; break; }
Q.pop();
}
if(!ok) { printf("NO"); return; }
printf("YES");
} int main()
{
work();
return ;
}

codeforces 720A:Closing ceremony的更多相关文章

  1. Codeforces 720A. Closing ceremony

    A. Closing ceremony time limit per test 2 seconds memory limit per test 256 megabytes The closing ce ...

  2. Codeforces 731C:Socks(并查集)

    http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,m天,k个颜色,每个袜子有一个颜色,再给出m天,每天有两只袜子,每只袜子可能不同颜色,问 ...

  3. Codeforces 747D:Winter Is Coming(贪心)

    http://codeforces.com/problemset/problem/747/D 题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意. ...

  4. Codeforces 747C:Servers(模拟)

    http://codeforces.com/problemset/problem/747/C 题意:有n台机器,q个操作.每次操作从ti时间开始,需要ki台机器,花费di的时间.每次选择机器从小到大开 ...

  5. codeforces 723B:Text Document Analysis

    Description Modern text editors usually show some information regarding the document being edited. F ...

  6. Codeforces 749D:Leaving Auction(set+二分)

    http://codeforces.com/contest/749/problem/D 题意:有几个人在拍卖场竞价,一共有n次喊价,有q个询问,每一个询问有一个num,接下来num个人从这次拍卖中除去 ...

  7. Codeforces 749B:Parallelogram is Back(计算几何)

    http://codeforces.com/problemset/problem/749/B 题意:已知平行四边形三个顶点,求另外一个顶点可能的位置. 思路:用向量来做. #include <c ...

  8. Codeforces 749C:Voting(暴力模拟)

    http://codeforces.com/problemset/problem/749/C 题意:有n个人投票,分为 D 和 R 两派,从1~n的顺序投票,轮到某人投票的时候,他可以将对方的一个人K ...

  9. Codeforces 746D:Green and Black Tea(乱搞)

    http://codeforces.com/contest/746/problem/D 题意:有n杯茶,a杯绿茶,b杯红茶,问怎么摆放才可以让不超过k杯茶连续摆放,如果不能就输出NO. 思路:首先,设 ...

随机推荐

  1. Jsp c标签数值格式化

    整数带千分符显示:<fmt:formatNumber value="${num}" type="number"/> 整数显示:<fmt:for ...

  2. js判断滚动条到底部

    判断滚动条到底部,需要用到DOM的三个属性值,即scrollTop.clientHeight.scrollHeight. scrollTop为滚动条在Y轴上的滚动距离. clientHeight为内容 ...

  3. 005医疗项目-模块一:用户的查找:1.用户表查询的sql语句

    这是医疗项目的第一个模块:做一个用户的查询,可以根据用户的账号,用户的名称,单位的名称,用户的类型去查询.要求效果如下:

  4. Eclipse添加注释简介

    (1)在方法或者属性上面添加注释:在方法或者属性字段的上面一行输/**,然后回车.一般情况下添加的注释格式如下所示,当然注释的格式是可以修改的:   1 2 3 4 5 /**   * @param ...

  5. 【转】【C#】C# 不常用关键字

    1.__arglist 让我们先从__arglist开始. __arglist是用来给方法传送参数.通常我们是通过函数头部指定的参数列表给方法传递参数的.如果我们想要给方法传递一组新的参数,我们需要重 ...

  6. [IIS][ASP.NET]“拒绝访问临时目录”的解决方法

    开始以为是“C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files”文件夹权限的问题,但怎么设置这个权限也解决不 ...

  7. tee命令

    tee命令 http://liubin.blog.51cto.com/282313/131298 https://en.wikipedia.org/wiki/Tee_(command)

  8. 实战 SQL Server 2008 数据库误删除数据的恢复

    SQL Server中误删除数据的恢复本来不是件难事,从事务日志恢复即可.但是,这个恢复需要有两个前提条件: 1. 至少有一个误删除之前的数据库完全备份. 2. 数据库的恢复模式(Recovery m ...

  9. 2015年新版C#从入门到精通(第2版)视频教学录像【无水印版】

    <c#从入门到精通(第2版)>以零基础讲解为宗旨,用实例引导读者学习,深入浅出地介绍了c#的相关知识和实战技能.<c#从入门到精通(第2版)>第1篇[c#语言基础]主要讲解c# ...

  10. 使用ObjectAnimator设置动画

    ObjectAnimator是ValueAnimator的子类,他本身就已经包含了时间引擎和值计算,所以它拥有为对象的某个属性设置动画的功能.这使得为任何对象设置动画更加的容易.你不再需要实现 Val ...