【Codeforces Round 331 (Div 2)C】【脑洞 SET贪心写法】Wilbur and Points 锯齿图形差值编号,右上角的最大

来源:互联网 发布:北京宽带哪个好 知乎 编辑:程序博客网 时间:2024/06/08 05:53
Wilbur and Points
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Wilbur is playing with a set of n points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (x, y) belongs to the set, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y also belong to this set.

Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to n. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (x, y) gets number i, then all (x',y') from the set, such that x' ≥ x and y' ≥ y must be assigned a number not less than i. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4.

Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as s(x, y) = y - x. Now he gives Wilbur some w1, w2,..., wn, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number i has it's special value equal to wi, that is s(xi, yi) = yi - xi = wi.

Now Wilbur asks you to help him with this challenge.

Input

The first line of the input consists of a single integer n (1 ≤ n ≤ 100 000) — the number of points in the set Wilbur is playing with.

Next follow n lines with points descriptions. Each line contains two integers x and y (0 ≤ x, y ≤ 100 000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (x, y) is present in the input, then all points (x', y'), such that 0 ≤ x' ≤ x and 0 ≤ y' ≤ y, are also present in the input.

The last line of the input contains n integers. The i-th of them is wi ( - 100 000 ≤ wi ≤ 100 000) — the required special value of the point that gets number i in any aesthetically pleasing numbering.

Output

If there exists an aesthetically pleasant numbering of points in the set, such that s(xi, yi) = yi - xi = wi, then print "YES" on the first line of the output. Otherwise, print "NO".

If a solution exists, proceed output with n lines. On the i-th of these lines print the point of the set that gets number i. If there are multiple solutions, print any of them.

Sample test(s)
input
52 00 01 01 10 10 -1 -2 1 0
output
YES0 01 02 00 11 1
input
31 00 02 00 1 2
output
NO
Note

In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and yi - xi = wi.

In the second sample, the special values of the points in the set are 0,  - 1, and  - 2 while the sequence that the friend gives to Wilbur is0, 1, 2. Therefore, the answer does not exist.


#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}const int N=2e5+10,M=0,Z=1e9+7,ms63=1061109567;const int BAS=1e5;int n;int x,y;pair<int,int>ans[N];set<pair<int,int>>sot[N],point;bool check(){for(int i=1;i<=n;++i){int k;scanf("%d",&k);k+=BAS;if(sot[k].size()==0)return 0;int x=sot[k].begin()->first;int y=sot[k].begin()->second;sot[k].erase(sot[k].begin());if(x&&!point.count(MP(x-1,y)))return 0;if(y&&!point.count(MP(x,y-1)))return 0;point.insert(MP(x,y));ans[i]=MP(x,y);}return 1;}int main(){scanf("%d",&n);for(int i=1;i<=n;++i){scanf("%d%d",&x,&y);sot[y-x+BAS].insert(MP(x,y));}if(check()){puts("YES");for(int i=1;i<=n;++i)printf("%d %d\n",ans[i].first,ans[i].second);}else puts("NO");return 0;}/*【trick&&吐槽】这题可谓是,极富思维价值,非常有意义!【题意】给你一个集合,集合中包含n(1<=n<=1e5)个点。每个点的坐标都是[0,1e5]范围的。而且,如果点(x,y)在这个集合中,那对于(xx,yy),0<=xx<=x&&0<=yy<=y,(xx,yy)也会在这个集合中。我们会告诉你,会依次输出,这个集合中的所有点。现在,我们要对集合中的所有点做编号操作,n个点的编号依次为从1到n。如果一个点(x,y)被编号为p,那对于(xx,yy),0<=xx<=x&&0<=yy<=y的所有点(xx,yy),(xx,yy)的编号要严格比p小。现在,我们要你对这n个点染色。定义w(point)=point.y-point.x,也就是这个点的纵坐标-横坐标。编号为1,2,3,4,5,...,n的n个点。需要对应着w()值分为为k1,k2,k3,k4,k5,...,kn。我们会输出这个k[]数列。现在,让你判定我们是否可以成功给所有点编号。如果可以,则输出YES,然后按照k[]数列的顺序依次输出其所对应的所有点。否则,则输出NO。【类型】脑洞【分析】有一种可行的做法是——我们对于每个点,求出这个点的坐标差值,把这个点push_back到这个差值的vector中。然后我们处理询问的时候,在询问中,有(编号,差值)这两个信息。我们从差值的vector中,取出点,如果这个点之间没有编号过。而且比这个点小的点都编号过了。那么我们给这个点编号。会不会有多个点同时满足这个条件,且对应着合法的方案呢?显然,是不会的。因为,题目中给出的点的集合,其实是一个呈阶梯状下降的形状。同样的差值,对应的是y=x+b的一条线,这条线上,肯定最多只有一个点,满足这个点左下角的所有点都被编号过且自己没有编号过的条件。于是我们通过vector和map,就解决了这道题。它的时间复杂度是多少呢?最坏情况,大概是很多点都push_back到同一个vector中。这样,我们的暴力的时间复杂度就可达vector.size()^2级别。然而,因为题目中所说的一个点如果选就会选这个点左下角全部点。于是,vector.size()最多不过是2000,于是这种做法的AC是合理的,时间复杂度取决于map,是O(nlogn)。然而,这个速度还是有些慢的。我们发现,每次我们从vector中取点,也花费了大量时间。不如我们用set来存放每个差值的点。这样,当我们处理到(编号,差值)的时候,在同样的差值的点中,我们一定是选取当前未编号的点中位置最小的一个做染色。【时间复杂度&&优化】O(nlogn)【数据】60 00 10 20 30 40 50 1 2 3 4 5;*/


1 0
原创粉丝点击