题意:有n-1个缝隙,在上面搭桥,每个缝隙有个ll,rr值,ll<=长度<=rr的才能搭上去。求一种搭桥组合。

经典问题,应列入acm必背300题中。属于那种不可能自己想得出来的题。将二元组[ll,rr]排序(ll相同时再rr),长度x排序(升序)。一个全局优先队列pq(rr小的顶部)。for循环,对每个x,将ll比它小的放入优先队列pq,如果pq仍为空,说明这块桥用不上,不为空,看top的rr是否大于x,如果大于,这块桥就能用上,并且给当前的top一定是可行的。

乱码:

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include <stack>
using namespace std;
const int SZ=2e5+,INF=0x7FFFFFFF;
typedef long long lon;
lon match[SZ]; struct nd{
lon ll,rr,id;
nd(lon a=,lon b=):ll(a),rr(b){}
}; bool cmp(nd &x,nd &y)
{
if(x.ll!=y.ll)return x.ll<y.ll;
else return x.rr<y.rr;
} struct ope{
const bool operator()(nd &x,nd &y)const
{
return x.rr>y.rr;
}
}; struct bn{
lon val,id;
bn(lon a=,lon b=):val(a),id(b){}
bool operator<(bn &other)
{
return val<other.val;
}
}; int main()
{
std::ios::sync_with_stdio();
lon n,m;
cin>>n>>m;
vector<nd> vct,src;
for(lon i=;i<n;++i)
{
nd tmp;
cin>>tmp.ll>>tmp.rr;
src.push_back(tmp);
}
for(lon i=;i<n;++i)
{
nd tmp(src[i].ll-src[i-].rr,src[i].rr-src[i-].ll);
tmp.id=i-;
vct.push_back(tmp);
}
sort(vct.begin(),vct.end(),cmp);
vector<bn> bge;
for(lon i=;i<m;++i)
{
lon tmp;
cin>>tmp;
bge.push_back(bn(tmp,i));
}
sort(bge.begin(),bge.end());
priority_queue<nd,vector<nd>,ope> pq;
vector<lon> res;
for(lon i=,j=;i<m;++i)
{
lon cur=bge[i].val;
for(;j<n-;++j)
{
if(vct[j].ll<=cur)
{
pq.push(vct[j]);
}
else break;
}
if(pq.empty())
{
continue;
}
nd top=pq.top();
if(top.rr<cur)
{
}
else
{
pq.pop();
match[top.id]=bge[i].id+;
}
}
bool ok=;
for(lon i=;i<n-;++i)
{
//cout<<"mt: "<<match[i]<<endl;
if(match[i]==)ok=;
}
if(ok)
{
cout<<"Yes"<<endl;
for(lon i=;i<n-;++i)
{
if(i)cout<<" ";
cout<<match[i];
}
cout<<endl;
}
else cout<<"No"<<endl;
return ;
}
05-15 02:35