codeforces 720A:Closing ceremony

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 ;
}
上一篇:s11 day100路飞项目逻辑购物车一


下一篇:UVA - 11137 Ingenuous Cubrency[背包DP]