[优先队列][贪心]JZOJ 6274 梦境
分析
把区间按照左端点排序,然后枚举转折点,把右端点最靠前的包含转折点的区间与其匹配一定最优(正确性难证)
最靠前可以用优先队列维护,时间复杂度nlogn
#include <iostream> #include <cstdio> #include <algorithm> #include <queue> using namespace std; typedef long long ll; const int N=2e5+10;; int n,m; struct Intervals { ll l,r; friend bool operator < (Intervals a,Intervals b) { return a.r>b.r; } }a[N]; ll b[N],ans; bool vis[N]; priority_queue<Intervals> q; bool CMP(Intervals a,Intervals b) { return a.l<b.l; } int main() { freopen("dream.in","r",stdin); freopen("dream.out","w",stdout); scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i].l,&a[i].r); for (int i=1;i<=m;i++) scanf("%lld",&b[i]); sort(b+1,b+m+1);sort(a+1,a+n+1,CMP); for (int i=1,j=1;i<=m;i++) { while (a[j].l<=b[i]&&j<=m) q.push(a[j]),j++; while (!q.empty()&&q.top().r<b[i]) q.pop(); if (!q.empty()) ans++,q.pop(); } printf("%lld",ans); }
在日渐沉没的世界里,我发现了你。