Time limit : 2sec / Memory limit : 256MB
Score : 400 points
Problem Statement
On a two-dimensional plane, there are N red points and N blue points. The coordinates of the i-th red point are (ai,bi), and the coordinates of the i-th blue point are(ci,di).
A red point and a blue point can form a friendly pair when, the x-coordinate of the red point is smaller than that of the blue point, and the y-coordinate of the red point is also smaller than that of the blue point.
At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs.
Constraints
- All input values are integers.
- 1≤N≤100
- 0≤ai,bi,ci,di<2N
- a1,a2,…,aN,c1,c2,…,cN are all different.
- b1,b2,…,bN,d1,d2,…,dN are all different.
#include<algorithm> #include<cstdio> #include<iostream> #include<vector> #include<cstring> using namespace std; int cnt[10008]; int main() { int n; while(scanf("%d",&n)!=EOF) { vector<pair<pair<int,int>,int> > a; int x,y; for(int i=0; i<n; i++) { cin >> x>> y; a.push_back(make_pair(make_pair(x,y),0)); } for(int i=0; i<n; i++) { cin >> x>> y; a.push_back(make_pair(make_pair(x,y),1)); } sort(a.begin(),a.end()); int ans=0; memset(cnt,0,sizeof(cnt)); for(int i=0;i<a.size();i++){ if(a[i].second==0) { cnt[a[i].first.second]++; } else{ for(int j=a[i].first.second-1;j>=0;j--){ if(cnt[j]>0){ cnt[j]--; ans++; break; } } } } cout<<ans<<endl; } }