Codeforces Round #310 (Div. 1) B. Case of Fugitive 贪心

B. Case of Fugitive
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.

Sample test(s)
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
1 

  用一些桥把岛连接起来,岛和桥都看成线段,设桥的两个端点是x,y,要满足li ≤ x ≤ ri, li + 1 ≤ y ≤ ri + 1y - x = a且。给出M个桥,问相邻的岛之间是否都能建上桥。如果能的话按建立顺序输出一组满足条件的桥的编号。

  设两个岛之间最短间隔为l,最长间隔为r。把间隔按l从小到大排序,桥的长度也按从小到大排序,然后按从小到大的顺序对桥扫一遍,对于一个桥,找出l比它小,r比它大并且r尽量小的间隔进行匹配。在扫的过程中,用优先队列保存间隔l不小于当前桥长度的,然后每次取出r最小的那个,如果r不小于桥的长度就匹配,否则这个间隔肯定是匹配不了桥的,因为后面的桥的长度更大。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;

const LL MAXN=200010;
const LL SIZE=4096;
const LL INF=0x3f3f3f3f;

LL N,M;
LL l[MAXN],r[MAXN],ans[MAXN];

struct St{
    LL l,r,id;
    bool operator < (const St& rhs) const{
        return r>rhs.r;
    }
}a[MAXN];

struct St2{
    LL len,id;
    bool operator < (const St2& rhs) const{
        return len<rhs.len;
    }
}b[MAXN];

bool cmp(St a,St b){
    return a.l<b.l;
}

priority_queue<St> q;

int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%I64d%I64d",&N,&M)!=EOF){
        for(LL i=1;i<=N;i++) scanf("%I64d%I64d",&l[i],&r[i]);
        LL k=0;
        for(LL i=2;i<=N;i++){
            a[k].r=r[i]-l[i-1];
            a[k].l=l[i]-r[i-1];
            a[k].id=i-2;
            k++;
        }
        sort(a,a+k,cmp);
        for(LL i=0;i<M;i++){
            scanf("%I64d",&b[i].len);
            b[i].id=i+1;
        }
        sort(b,b+M);
        while(!q.empty()) q.pop();
        LL j=0,cnt=0;
        for(LL i=0;i<M;i++){
            for(;j<k;j++){
                if(a[j].l<=b[i].len) q.push(a[j]);
                else break;
            }
            if(!q.empty()){
                St tmp=q.top();
                q.pop();
                if(tmp.r>=b[i].len){
                    ans[tmp.id]=b[i].id;
                    cnt++;
                }
                else break;
            }
        }
        if(cnt<k) printf("No\n");
        else{
            printf("Yes\n");
            for(LL i=0;i<k-1;i++) printf("%I64d ",ans[i]);
            printf("%I64d\n",ans[k-1]);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值