Codeforces Round #310 (Div. 2) D 贪心+set应用



链接:戳这里


D. Case of Fugitive
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 let's represent them as non-intersecting segments on a straight line: island i has coordinates [li, ri], besides, ri < li + 1 for 1 ≤ i ≤ n - 1.

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.

The detective was supplied with m bridges, each bridge can be used at most once. Help him determine whether the bridges he got are enough to connect each pair of adjacent islands.

Input
The first line contains integers n (2 ≤ n ≤ 2·105) and m (1 ≤ m ≤ 2·105) — the number of islands and bridges.

Next n lines each contain two integers li and ri (1 ≤ li ≤ ri ≤ 1018) — the coordinates of the island endpoints.

The last line contains m integer numbers a1, a2, ..., am (1 ≤ ai ≤ 1018) — the lengths of the bridges that Andrewid got.

Output
If it is impossible to place a bridge between each pair of adjacent islands in the required manner, print on a single line "No" (without the quotes), otherwise print in the first line "Yes" (without the quotes), and in the second line print n - 1 numbers b1, b2, ..., bn - 1, which mean that between islands i and i + 1 there must be used a bridge number bi.

If there are multiple correct answers, print any of them. Note that in this problem it is necessary to print "Yes" and "No" in correct case.

Examples
input
4 4
1 4
7 8
9 10
12 14
4 5 3 8
output
Yes
2 3 1 
input
2 2
11 14
17 18
2 9
output
No
input
2 1
1 1
1000000000000000000 1000000000000000000
999999999999999999
output
Yes

Note
In the first sample test you can, for example, place the second bridge between points 3 and 8, place the third bridge between points 7 and 10 and place the first bridge between points 10 and 14.

In the second sample test the first bridge is too short and the second bridge is too long, so the solution doesn't exist.


题意:

按顺序给出n个岛屿的跨度[l,r],表示该岛屿的着陆长度。Ri<Li+1 && Li>Ri-1

给出m座桥的长度,现在通过使用m座桥将n个岛屿串联起来。问如何拜访桥的位置


思路:

贪心,每个岛屿需要的桥的长度是(l[i+1]-r[i],r[i+1]-l[i])。按照关键字R排序,R相同L小的在前面

然后set维护桥的使用情况。

当前第i个岛屿需要的桥的范围是[L,R]。那么快速找出set中第一个桥长度>=L的桥,每次贪心都取小的,判断是否<=R

然后每取一座桥就在set中删除


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
struct node{
    ll l,r;
    int id;
}s[200100];
ll x[200100],y[200100];
bool cmp(node a,node b){
    if(a.r==b.r) return a.l<b.l;
    return a.r<b.r;
}
int n,m;
set<pair<ll,int> > S;
int anw[200100],vis[200100];
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) scanf("%I64d%I64d",&x[i],&y[i]);
    for(int i=1;i<n;i++){
        s[i].l=x[i+1]-y[i];
        s[i].r=y[i+1]-x[i];
        s[i].id=i;
    }
    sort(s+1,s+n,cmp);
    for(int i=1;i<=m;i++) {
        ll v;
        scanf("%I64d",&v);
        S.insert(make_pair(v,i));
    }
    set<pair<ll,int> >::iterator it;
    for(int i=1;i<n;i++){
        it=S.lower_bound(make_pair(s[i].l,-1));
        if(it==S.end()) break;
        if(it->first > s[i].r) break;
        anw[s[i].id]=it->second;
        S.erase(it);
    }
    for(int i=1;i<n;i++){
        if(anw[i]==0) {
            cout<<"No"<<endl;
            return 0;
        }
    }
    cout<<"Yes"<<endl;
    for(int i=1;i<n;i++) cout<<anw[i]<<" ";cout<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值