[CF310]D. Case of Fugitive

题意:

给出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不熟。。。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-09 08:04:41

[CF310]D. Case of Fugitive的相关文章

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 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 556 D Case of Fugitive

这个题很显然,可以转换成这个问题: 有n-1个区间,m个数, 每个数最多只能用一次,第i个数只要能被第j个区间包含,那么这个数就可以放入这个区间内. 求出,当所有区间里都恰有一个数时的情况. 我们把所有区间按照下限升序排序,所有数升序排序之后分治即可. 分治过程,维护一个元素为区间的小堆,堆顶是上限最小的区间. 考虑第i个数,把所有能够包含它的区间都丢到堆中,然后从堆中丢一个区间出来,若这个区间可以包含这个数,那么记录下来,否则无解,结束程序. 为什么此时无解呢?因为很显然后面的数都不小于这个数

CodeForces - 556D 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 located in a row. For more comfort l

codeforces 555B Case of Fugitive

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

#310 (div.2) D. Case of Fugitive

1.题目描述:点击打开链接 2.解题思路:本题利用贪心法+优先队列解决.不过本题的贪心策略的选取是关键,有些看似正确的贪心策略实际上暗含危险.先说说正确的贪心策略:将所有的岛按照顺序求出第i个岛和i+1个岛之间桥的最小最大长度,并按照L从小到大排序,若相同则按照R从小到大排序.然后对桥由小到大排序,将所有的桥扫描一遍,枚举第i个桥时,将L值小于等于当前桥的区间按照(R,id)放入优先队列,R小的在队首,大的在队尾,每次看队首的R是否大于等于len,若满足,则记录答案,break,若不存在,则无解

Codeforces 555B - Case of Fugitive 搭桥

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

codeforce 310 div2 D题 Case of Fugitive

题目 给n个岛屿和m座桥,问桥是否足够把两两相邻的岛屿连起来,当一座桥的长度大于等于相邻两个岛屿上的点的距离最小值并且小于等于相邻两个岛屿上的点的距离最大值的时候,就可以把他们连起来. 用贪心的方法,把岛屿按照距离的最小值(即l[i+1]-r[i])排序,把桥按照长度排序,显然距离最小的岛屿应该用长度最小的桥来接,这是第一个贪心.第二个贪心是当前岛屿可以连接的所有桥中应该选距离最大值最小的.仔细想想这两个贪心思路是对的.最后看是否所有的岛屿都被连起来就行了. 代码: #include <cstd

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 co