Codeforces 556D Case of Fugitive【排序+贪心】

186 篇文章 0 订阅

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.

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
1 
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个岛屿,每个岛屿的左端点和右端点已知,此时有m种桥,其长度已知,问能否用这些桥连接起来所有的岛屿。


思路:


1、首先我们先将问题转化一下:

①预处理出每相邻两个岛屿之间的最近距离和最远距离,存入a【i】.minn,和a【i】.maxn中。

②我们再将所有桥的长度存入b【i】中。

那么此时问题就转化为:有n-1个区间,其中需要在m个桥的长度中,给每一个区间安置一个桥,桥的长度必须满足:a[i].minn<=len<=a[i].maxn。


2、那么我们考虑解决这个问题:

①模拟训练的过程中满脑子的二分匹配...........很显然,二分图最大匹配的做法是会TLE的。

②那么考虑先将敲的长度从小到大排序。

③再将每一个区间按照minn从小到大排序。

④那么接下来我们考虑用桥来选择区间。

⑤我们将a【j】.minn<=b【i】<=a【j】.maxn的合法的j都丢进一个优先队列中。那么对应此时优先队列中的所有元素都肯定满足这样一个条件:
这里所有的区间的minn.都小于等于此时的b【i】.那么此时我们就不需要对minn进行考虑,只考虑maxn这个属性即可。

⑥那么我们优先队列中优先的属性就是maxn的值,我们按照从小到大的顺序来维护这个优先队列。

那么对应如果此时优先队列不为空,那么我们贪心的取队头即可。

因为队头是maxn最小的元素,随着b【i】一点一点增大,maxn越小的就越没有桥能与之匹配,所以我们每一次取队头。

如果此时队头区间的maxn已经小于了b【i】,那么显然无解,因为b【i+1】是一定大于b【i】的,所以b【i+1】也一定是大于此时队头区间的maxn的.


3、数据范围比较大,注意需要使用Long Long.


Ac代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define ll __int64
struct node
{
    ll minn,maxn;
    friend bool operator <(node a,node b)
    {
        return a.maxn>b.maxn;
    }
    int pos;
}a[200600];
struct node2
{
    ll bi;int pos;
}b[200600];
int vis[200600];
int ans[200600];
ll x[200600];
ll y[200600];
int cmp(node a,node b)
{
    return a.minn<b.minn;
}
int cmp2(node2 a,node2 b)
{
    return a.bi<b.bi;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        priority_queue<node >s;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            scanf("%I64d%I64d",&x[i],&y[i]);
        }
        for(int i=0;i<m;i++)
        {
            scanf("%I64d",&b[i].bi);
            b[i].pos=i;
        }
        for(int i=1;i<n;i++)
        {
            a[i].pos=i;
            a[i].minn=x[i]-y[i-1];
            a[i].maxn=y[i]-x[i-1];
        }
        int cont=0;
        int flag=0;
        int j=1;
        sort(a+1,a+n,cmp);
        sort(b,b+m,cmp2);
        for(int i=0;i<m;i++)
        {
            while(j<n&&a[j].minn<=b[i].bi&&b[i].bi<=a[j].maxn)
            {
                s.push(a[j]);
                j++;
            }
            if(s.size()==0)continue;
            node now=s.top();
            s.pop();
            if(now.maxn<b[i].bi)
            {
                flag=1;break;
            }
            cont++;
            ans[now.pos]=b[i].pos;
        }
        if(cont<n-1)
        {
            printf("No\n");
            continue;
        }
        if(flag==1)printf("No\n");
        else
        {
            printf("Yes\n");
            for(int i=1;i<n;i++)
            {
                printf("%d ",ans[i]+1);
            }
            printf("\n");
        }
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值