码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-09-14 23:12:24      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:priority   style   typedef   round   cst   经典   string   queue   include   

题意:有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)

标签:priority   style   typedef   round   cst   经典   string   queue   include   

原文地址:https://www.cnblogs.com/gaudar/p/9649115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!