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.
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 lintegers indicate stamina of each person there.
The stamina of the person is a positive integer less that or equal to n + m.
If it is possible to distribute tickets between people in the described manner print "YES", otherwise print "NO".
贪心水过详情见代码
# include <iostream>
# include <algorithm>
# include <vector>
using namespace std;
typedef long long int ll;
int n,m,k,x;
vector<int> a,b;
int main(){
cin>>n>>m>>k;
for (int i=0;i<k;i++) cin>>x,a.push_back(x);
cin>>x; for (int i=0;i<n*m-k;i++) cin>>x,b.push_back(x);
sort(a.begin(),a.end()),sort(b.begin(),b.end());
for (int i=n;i>=1;i--)
for (int j=1;j<=m;j++)
{
auto p=lower_bound(a.begin(),a.end(),i+j);
auto q=lower_bound(b.begin(),b.end(),i+m+1-j);
if (p!=a.end()) a.erase(p);
else if (q!=b.end()) b.erase(q);
else return cout<<"NO",0;
}
cout<<"YES";
}