先按照minSPF排序,然后每次选择最大可以选的就可以了。
注意反向map的lower还是upper
1 /* *********************************************** 2 Author :BPM136 3 Created Time :2018/7/15 17:05:21 4 File Name :3614.cpp 5 ************************************************ */ 6 7 #include<iostream> 8 #include<cstdio> 9 #include<algorithm> 10 #include<cstdlib> 11 #include<cmath> 12 #include<cstring> 13 #include<vector> 14 #include<map> 15 using namespace std; 16 17 typedef long long ll; 18 typedef map<int,int, greater<int > > MIIG; 19 20 const int N = 100005; 21 22 int n,m; 23 24 struct cow { 25 int mn,mx; 26 }a[N]; 27 bool cmp_a(cow a,cow b) { 28 if(a.mn==b.mn) return a.mx<b.mx; 29 return a.mn>b.mn; 30 } 31 32 int main() { 33 MIIG M; 34 M.clear(); 35 36 scanf("%d%d",&n,&m); 37 for(int i=1;i<=n;i++) scanf("%d%d",&a[i].mn,&a[i].mx); 38 for(int i=1;i<=m;i++) { 39 int v,nm; 40 scanf("%d%d",&v,&nm); 41 M[v]+=nm; 42 } 43 sort(a+1,a+n+1,cmp_a); 44 int ans=0; 45 for(int i=1;i<=n;i++) { 46 MIIG::iterator t=M.lower_bound(a[i].mx); 47 if(t==M.end() || t->first<a[i].mn) continue; 48 t->second--; 49 ans++; 50 if(t->second==0) M.erase(t); 51 } 52 cout<<ans<<endl; 53 return 0; 54 }