CodeForces 555B 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 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个桥,每个岛屿是一个区间[li,ri],每个桥有一个长度,问能否用这些桥将这些岛屿连通。

思路:岛屿与岛屿之间可放桥的长度为【li-r(i-1),ri-l(i-1)】,题目也就转化成了在这些区间中能否都可以放一个桥。

贪心思想,我们尽量先用小的桥去放入较小的区间;
1.我们先对桥的长度进行从小到大排序
2.我们处理出来岛屿之间可放桥的长度区间
3.我们对这些区间进行左端点从小到大的排序
4.我们利用优先队列,我们将满足l<=a[i].len<=r的区间入队,因为a[i].len随着i的增大a[i].len会越来越大,因此队内所有的l<=a[i].len,因此只需要比较r与a[i].len的关系,因此我们设区间右端点小的优先级高,弹出队首元素,如果r>=a[i].len,那么在该区间内就放这个桥,否则的话直接输出no(因为a[i].len是越来越大的,最小的a[i].len放不进去的话,更何况那些大的)。

#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node1{
	ll len;
	ll id;
}a[200009];
struct node2{
	ll l;
	ll r;
	friend bool operator < (node2 r,node2 s)
	{
		return r.r>s.r;
	}
	int id;
}b[200009];
ll x[200009],y[200009],ans[200009];
bool cmp1(node1 r,node1 s)
{
	return r.len<s.len;
}
bool cmp2(node2 u,node2 o)
{
	return u.l<o.l;
}
int main()
{
	int n,m,cnt=0,flag=0,j=1;
	cin>>n>>m;
	for(int i=0;i<n;i++)
		cin>>x[i]>>y[i];
	for(int i=0;i<m;i++)
	{
		cin>>a[i].len;
		a[i].id=i;
	}
	for(int i=1;i<n;i++)
	{
		b[i].l=x[i]-y[i-1];
		b[i].r=y[i]-x[i-1];
		b[i].id=i;
	}
	sort(a,a+m,cmp1);
	sort(b+1,b+n,cmp2);
	priority_queue<node2> q;
	for(int i=0;i<m;i++)
	{
		while(j<n&&b[j].l<=a[i].len&&a[i].len<=b[j].r)
		{
			q.push(b[j]);
			j++;
		}
		if(q.empty()) continue;
		node2 tmp=q.top();
		q.pop();
		if(tmp.r<a[i].len)
		{
			flag=1;break;
		}
		cnt++;
		ans[tmp.id]=a[i].id+1;
 	}
 	if(flag||cnt<n-1)
 	{
 		cout<<"No"<<endl;
	}
	else
	{
		cout<<"Yes"<<endl;
		for(int i=1;i<n-1;i++)
			cout<<ans[i]<<" ";
		cout<<ans[n-1]<<endl;
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/why932346109/article/details/88085214