E - Wrapping Chocolate
Time Limit: 4 sec / Memory Limit: 1024 MB
Score : 500500 points
Problem Statement
Takahashi has NN pieces of chocolate. The ii-th piece has a rectangular shape with a width of A_iAi centimeters and a length of B_iBi centimeters.
He also has MM boxes. The ii-th box has a rectangular shape with a width of C_iCi centimeters and a length of D_iDi centimeters.
Determine whether it is possible to put the NN pieces of chocolate in the boxes under the conditions below.
- A box can contain at most one piece of chocolate.
- A_i \leq C_jAi≤Cj and B_i \leq D_jBi≤Dj must hold when putting the ii-th piece of chocolate in the jj-th box (they cannot be rotated).
Constraints
- 1 \leq N \leq M \leq 2\times 10^51≤N≤M≤2×105
- 1 \leq A_i,B_i,C_i,D_i \leq 10^91≤Ai,Bi,Ci,Di≤109
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
NN MM A_1A1 \ldots… A_NAN B_1B1 \ldots… B_NBN C_1C1 \ldots… C_MCM D_1D1 \ldots… D_MDM
Output
If it is possible to put the NN pieces of chocolate in the boxes, print Yes
; otherwise, print No
.
Sample Input 1 Copy
Copy
2 3 2 4 3 2 8 1 5 2 10 5
Sample Output 1 Copy
Copy
Yes
We can put the first piece of chocolate in the third box and the second piece in the first box.
Sample Input 2 Copy
Copy
2 2 1 1 2 2 100 1 100 1
Sample Output 2 Copy
Copy
No
A box can contain at most one piece of chocolate.
Sample Input 3 Copy
Copy
1 1 10 100 100 10
Sample Output 3 Copy
Copy
No
Sample Input 4 Copy
Copy
1 1 10 100 10 100
Sample Output 4 Copy
Copy
Yes
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main()
{
int n, m;
cin >> n >> m;
vector<P> a(n), c(m);
rep(i, n)cin >> a[i].first;
rep(i, n)cin >> a[i].second;
rep(i, m)cin >> c[i].first;
rep(i, m)cin >> c[i].second;
sort(a.rbegin(), a.rend());
sort(c.rbegin(), c.rend());
multiset<int> s;
int ind = 0;
rep(i, n)
{
while(ind < m && a[i].first <= c[ind].first)
{
s.insert(c[ind].second);
ind++;
}
auto it = s.lower_bound(a[i].second);
if(it == s.end())
{
cout << "No" << endl;
return 0;
}
s.erase(it);
}
cout << "Yes" << endl;
return 0;
}