标签:图片 \n tno end pos required let integer include
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 adjacentislands. 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?+?1and 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?-?1numbers 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
4 4
1 4
7 8
9 10
12 14
4 5 3 8
Yes
2 3 1
2 2
11 14
17 18
2 9
No
2 1
1 1
1000000000000000000 1000000000000000000
999999999999999999
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-1个区间和m个点。问你能否从m个点中找出n-1个点,分别包含于n-1个区间中。如果能,输出Yes,并输出任何一种匹配结果;否则输出No。
贪心。
先将区间按其左端点排序,再将点按其坐标排序。
依次考察每个点,直至考察完所有点(1..m):
1、将所有左端点不大于该点的区间送入优先队列。区间右端点越小,优先级越高。
2、若队列非空,则pop。若区间右端点比该点小,则跳出循环(实际上已经可以输出No了,因为后面没有更小的点与该区间对应);若区间右端点不小于该点,则建立区间到这点的对应关系(贪心,因为右端点较大的区间更有可能匹配到较大的点)。
最后考察是否所有的区间都建立了到点的对应关系,是则输出Yes及对应关系;否则输出No。
排序(谈几点自己的理解,C++学完对运算符重载啥的了解后,再回来想一想)。
//结构体&数
//优先队列用结构体:struct cmp & greater/less<int>
//排序用函数:int cmp() & int cmp()
//如果只涉及一种偏序关系,不必太在意,也可以在结构体内部写,如果涉及偏序关系较多,最好拿出来写清楚
#include<cstdio> #include<queue> #include<cstring> #include<algorithm> typedef long long lol; using namespace std; const int maxn=200000; struct tnode { lol mmin; lol mmax; int seq; }; tnode node[maxn+5];//1..n-1的区间 struct tlen { lol lenth; int seq; }; tlen len[maxn+5];//1..m的桥长 int cmp1(tnode a,tnode b) { return a.mmin<b.mmin; } int cmp2(tlen a,tlen b) { return a.lenth<b.lenth; } struct cmp3 { bool operator()(const int a,const int b) { return node[a].mmax>node[b].mmax; } }; int bridge[maxn+5];//i区间对应的桥 int main() { int n,m; scanf("%d%d",&n,&m); lol l0,r0,l,r; for(int i=1;i<=n;i++) { scanf("%lld%lld",&l,&r); if(i>=2) { node[i-1].mmin=l-r0; node[i-1].mmax=r-l0; node[i-1].seq=i-1; } l0=l; r0=r; } for(int i=1;i<=m;i++) { scanf("%lld",len+i); len[i].seq=i; } sort(node+1,node+n,cmp1); sort(len+1,len+m+1,cmp2); priority_queue<int,vector<int>,cmp3> q; memset(bridge,0,sizeof(bridge)); for(int i=1,j=1;i<=m;i++) { for(;j<=n-1&&node[j].mmin<=len[i].lenth;j++) q.push(j); bool flag=true; if(!q.empty()) { int tmp=q.top();q.pop(); if(node[tmp].mmax<len[i].lenth) flag=false; else bridge[node[tmp].seq]=len[i].seq; } if(!flag) break; } bool flag=true; for(int i=1;i<=n-1;i++) if(bridge[i]==0) { flag=false; break; } if(flag) { printf("Yes\n"); for(int i=1;i<=n-1;i++) { if(i<n-1) printf("%d ",bridge[i]); else printf("%d\n",bridge[i]); } } else printf("No\n"); return 0; }
CodeForces - 556D Case of Fugitive (贪心+排序)
标签:图片 \n tno end pos required let integer include
原文地址:https://www.cnblogs.com/acboyty/p/9792159.html