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

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

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

时间: 2024-08-26 09:57:16

codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)的相关文章

codeforces 555B Case of Fugitive

题目连接: http://codeforces.com/problemset/problem/555/B 题目大意: 有n个岛屿(岛屿在一列上,可以看做是线性的,用来描述岛屿位置的是起点与终点),m个桥,给出每个岛屿的位置和桥的长度,问是否可以把n个岛屿连起来? 解题思路: 排序+贪心,对于n个岛屿,相邻的两个之间起点和端点可以转化为n-1个连接桥的长度区间,把区间升序排列. 对于m个桥升序排列,对于每一个桥枚举每个可以合法覆盖的区间,选取最优的,选取的时候满足L<bridge_length<

Codeforces 555B - Case of Fugitive 搭桥

题意:给出n段路和m座桥.每段路的两个端点一维坐标表示.要从m座桥中选出一些桥搭在每两条相邻的路上,使得这些路全部连通.每两条相邻的路之间只能搭一座桥,且桥的两端必须恰好分别落在两段路上.如果存在一种搭桥方法,则输出Yes并按题目给定的路形成的空隙的顺序输出每座桥的序号,若不能则输出No. 题意换个说法实际上就是给定n - 1个区间和m个数,问是否能从m个数中选出n - 1个数,恰好分别落在n - 1个区间内. 觉得可以用贪心做,开始想了下因为区间有两个端点,而且位置任意,有点不好处理.后来就想

贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

题目传送门 1 /* 2 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 3 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0),先从1开始找到已经套好的娃娃层数, 4 其他是2次操作,还要减去k-1个娃娃是只要套上就可以 5 详细解释:http://blog.csdn.net/firstlucker/article/details/46671251 6 */ 7 #include <cstdio> 8 #i

找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

题目传送门 1 /* 2 找规律/贪心:ans = n - 01匹配的总数,水 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 2e5 + 10; 12 const int INF =

构造 Codeforces Round #310 (Div. 2) B. Case of Fake Numbers

题目传送门 1 /* 2 题意:n个数字转盘,刚开始每个转盘指向一个数字(0~n-1,逆时针排序),然后每一次转动,奇数的+1,偶数的-1,问多少次使第i个数字转盘指向i-1 3 构造:先求出使第1个指向0要多少步,按照这个次数之后的能否满足要求 4 题目读的好累:( 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #i

Round 310(Div.1) B. Case of Fugitive

Round 310(Div.1) B. Case of Fugitive Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water. The only dry land there is an archipelago of n narrow islands

codeforces Round #310(Div.1) 题解

嘴巴选手真爽,一不用打代码二不用掉Rating三还可以打杂.... 感觉这套题不难,但是被出题人出瞎了... 555A. Case of Matryoshkas 题目大意:给定n个大小从1到n的套娃,初始套成k坨,每次你可以选择两个操作: 1.选择一个不在任何其他套娃里的套娃,将里面的套娃取出来(要求原先里面有套娃) 2.选择一个不再任何其他套娃里的套娃,将一个套娃塞进去(要求原先里面没有套娃) 求将所有套娃合并的最小操作次数 如果一个套娃x初始在最里面,或者满足大小为1...x?1的套娃都在它

Codeforces 556D - Case of Fugitive

556D - Case of Fugitive 思路:将桥长度放进二叉搜索树中(multiset),相邻两岛距离按上限排序,然后二分查找桥长度匹配并删除. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long const int N=2e5+5; struct node { ll x; ll y; int id; bool operator <(const node &a)const { ret

Codeforces Round #310 (Div. 1)——A水——Case of Matryoshkas

Andrewid the Android is a galaxy-famous detective. He is now investigating the case of vandalism at the exhibition of contemporary art. The main exhibit is a construction of n matryoshka dolls that can be nested one into another. The matryoshka dolls