优先队列:
代码:
过了50%
#include <stdio.h> #include <algorithm> #include<iostreami> using namespace std; int ans[1001]; struct { int x,y; }a[1001]; bool cmp(int x,int y){ return x>y; } int main(){ int n; cin>>n; for(int i=0;i<n;i++){ cin>>a[i].x>>a[i].y; for(int j = a[i].x;j<=a[i].y;j++) ans[j]++; } sort(ans,ans+1001,cmp); cout<<ans[0]<<endl; return 0; }
过了100%
#include<iostream> #include<stdio.h> #include<queue> #include<algorithm> using namespace std; typedef long long ll; struct node{ int x,y; }a[100010]; bool cmp(node a,node b){ if(a.x==b.x) return a.y<b.y; return a.x<b.x; } int main(){ int n; scanf("%d",&n); ll ans = 0,res = 0; for(int i=0;i<n;i++) scanf("%d%d",&a[i].x,&a[i].y); sort(a,a+n,cmp); priority_queue < int, vector<int>, greater<int> > que; que.push(a[0].y); for(int i=1;i<n;i++){ while(!que.empty()){ int c = que.top(); if(c<a[i].x){ que.pop(); } else break; } que.push(a[i].y); res = que.size(); ans = max(ans,res); } cout<<ans<<endl; return 0; }