CodeForces - 556D 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
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
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
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
Input4 4Output
1 4
7 8
9 10
12 14
4 5 3 8
YesInput
2 3 1
2 2Output
11 14
17 18
2 9
NoInput
2 1Output
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; }View Code