[CF310]D. Case of Fugitive

解决如何在给定n个线段之间建立桥梁的问题,条件是桥梁必须满足特定长度要求。通过使用二分查找和集合来优化算法,降低复杂度以处理大规模数据。询问是否能连接所有线段,并给出符合条件的桥的编号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:
给出n个线段,在n个线段之间搭桥,给出m个桥的长度,假如满足条件
To reach the goal, Andrewid needs to place a bridge between each pair of adjacent islands. A bridge of length a can be placed between the i-th and the (i + 1)-th islads, if there are such coordinates of x and y, that li ≤ x ≤ ri, li + 1 ≤ y ≤ ri + 1 and y - x = a.
,问最后可以连接所有的线段吗?是的话输出线段所对应的桥的编号。

分析:
数据量比较大,虽然题意很简单,但是要想一些办法降低复杂度。这个数据量二分可以nlogn可以处理,那这里就用了set来处理,二分就用了lower_bound,另外题一点,pair

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <utility> 
#include<stack>
#include <algorithm>
#define read freopen("q.in","r",stdin)
#define LL long long
#define maxn 200005
using namespace std;

set <pair<pair<LL,LL>, int> > s;
multiset<pair<LL,LL> > k;
int res[maxn];
int main()
{ //'std::set<std::pair<std::pair<long long int, long long int>, int> >::iterator' has no member named 'first'
    LL n,m,x,y,a,b;
    int i,j;
    std::ios::sync_with_stdio(false);
    cin>>n>>m;

    for(i=0;i<n;i++)
    {
        cin>>x>>y;
        if(i>0)s.insert(make_pair(make_pair(y-a,x-b),i));
        a=x;b=y;
    }
    for(i=1;i<=m;i++)
    {
        cin>>x;
        k.insert(make_pair(x,i));
    }
    for(set<pair<pair<LL,LL>, int> >:: iterator it =s.begin();it!=s.end();it++)
    {
        multiset<pair<LL,LL> > :: iterator  j=k.lower_bound(make_pair(it->first.second,-1));
        if(j==k.end() || j->first>it->first.first) 
        {
            cout<<"No"<<endl;
            return 0;
        }
        res[it->second]=j->second;
        k.erase(j);
    }
    cout<<"Yes"<<endl;
    for(i=1;i<n;i++)cout<<res[i]<<" ";
    cout<<endl;
}

ps:觉得对STL不熟。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值