1. 程式人生 > >codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)

codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)

push_back other tin type 不為 pty push match back

題意:有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+10,INF=0x7FFFFFFF; typedef
long long lon; lon match[SZ]; struct nd{ lon ll,rr,id; nd(lon a=0,lon b=0):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=0,lon b=0):val(a),id(b){} bool operator<(bn &other) { return val<other.val; } }; int main() { std::ios::sync_with_stdio(0); lon n,m; cin>>n>>m; vector<nd> vct,src; for(lon i=0;i<n;++i) { nd tmp; cin>>tmp.ll>>tmp.rr; src.push_back(tmp); } for(lon i=1;i<n;++i) { nd tmp(src[i].ll-src[i-1].rr,src[i].rr-src[i-1].ll); tmp.id=i-1; vct.push_back(tmp); } sort(vct.begin(),vct.end(),cmp); vector<bn> bge; for(lon i=0;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=0,j=0;i<m;++i) { lon cur=bge[i].val; for(;j<n-1;++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+1; } } bool ok=1; for(lon i=0;i<n-1;++i) { //cout<<"mt: "<<match[i]<<endl; if(match[i]==0)ok=0; } if(ok) { cout<<"Yes"<<endl; for(lon i=0;i<n-1;++i) { if(i)cout<<" "; cout<<match[i]; } cout<<endl; } else cout<<"No"<<endl; return 0; }

codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)