Codeforces 596C Wilbur and Points 【贪心 + sort】

来源:互联网 发布:php 去除所有html标签 编辑:程序博客网 时间:2024/05/16 13:54

C. 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 (xy) 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 (xy) 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 (00), (01), (10) and (11), there are two aesthetically pleasing numberings. One is 1234 and another one is 1324.

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 w1w2,..., 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 (xy) 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 (20) gets number 3, point (00) gets number one, point (10) gets number 2, point (11) gets number 5 and point (01) 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 is012. Therefore, the answer does not exist.


好晦涩的题意,蒟蒻WA了4次才过。泪奔 ~~o(>_<)o ~~

定义一个二维点集的美观数列a[]:对任意的a[i], 使得集合里面所有满足0 <= x[j] < x[i] && 0 <= y[j] < y[i]的a[j] <= a[i]。 

题意:给定一个二维点集x[], y[]和一个序列w[],问你是否存在一个美观序列a[]使得对任意的i都唯一对应——a[i] = y[i] - x[i] = w[j]。若存在输出任意一组序列,反之输出-1。


好麻烦的题,思路怎么说呢。。。就是贪心思维胡搞吧,总之sort被用的清新脱俗。


思路:首先判断是否存在一个最佳的(最可能是一个美观序列)序列b[]使得,b[i] = y[i] - x[i] = w[j]。再判断序列b[]是否为关于二维点集的一个美观序列。

第一步:直接求出二维点集的y[] - x[]的val值,把该信息与对应的id以及x、y值用num1[]存储起来,然后贪心的排序,策略是先按val升序,再按x升序,最后按y升序(调换x和y的顺序应该是不会错的)。对给出的序列w[],记录元素的在序列中的id值和val,这些可以用num2[]存储,然后贪心排序,不同的是先按val升序,再按id值升序。最后从头到尾扫一遍,并记录序列b[]。

第二步:建立在序列b[]存在的前提下。我们经过上一步求出的b[]是最可能满足美观序列的,所以只需要更新出初始序列num1,num2,然后根据w[]的先后(序列b[]是按w[]的输入顺序求出的),判断num1(最初的二维点集)相邻的b[i]和b[i-1]之间是否满足x[b[i]] > x[b[i-1]] || y[b[i]] > y[b[i-1]]。


AC代码:


#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <vector>#define INF 0x3f3f3f3f#define eps 1e-4#define MAXN (200000+10)#define MAXM (1000000)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 100000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1using namespace std;struct Node{    int x, y, val, id, rec;};Node num1[MAXN], num2[MAXN];bool cmp(Node a, Node b){    if(a.val != b.val)        return a.val < b.val;    else if(a.x != b.x)        return a.x < b.x;    else        return a.y < b.y;}bool cmp1(Node a, Node b){    if(a.val != b.val)        return a.val < b.val;    else        return a.id < b.id;}bool cmp2(Node a, Node b){    return a.id < b.id;}int main(){    int n; Ri(n);    for(int i = 0; i < n; i++)    {        Ri(num1[i].x); Ri(num1[i].y);        num1[i].val = num1[i].y - num1[i].x;        num1[i].id = i;    }    sort(num1, num1+n, cmp);    for(int i = 0; i < n; i++)    {        Ri(num2[i].val);        num2[i].id = i;    }    sort(num2, num2+n, cmp1);    bool flag = true;    for(int i = 0; i < n; i++)    {        if(num1[i].val != num2[i].val)        {            flag = false;            break;        }        else            num2[i].rec = num1[i].id;    }    if(flag)    {        sort(num2, num2+n, cmp2);        sort(num1, num1+n, cmp2);        for(int i = 1; i < n; i++)        {            if(!(num1[num2[i].rec].x > num1[num2[i-1].rec].x || num1[num2[i].rec].y > num1[num2[i-1].rec].y))            {                flag = false;                break;            }        }        if(flag)        {            printf("YES\n");            for(int i = 0; i < n; i++)                printf("%d %d\n", num1[num2[i].rec].x, num1[num2[i].rec].y);        }        else            printf("NO\n");    }    else        printf("NO\n");    return 0;}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 买了球显示未出票中奖了怎么办 点错了允许易企秀获得权限怎么办 淘宝还没发货商家拒绝退款怎么办 买家不补邮费还要你发货怎么办 天猫客服提示获取信息失败怎么办 拼多多下单了商家不发货怎么办 店铺扣2分宝贝降权了怎么办 淘宝店没订单没流量怎么办啊 淘宝id账号登录密码忘记了怎么办 淘宝网店铺授权客服联系不上怎么办 卖家发货买家查不到物流信息怎么办 物流信息到了但东西没到怎么办 淘宝详情已更改对方恶意投诉怎么办 淘宝店写的不给退换怎么办 从饿了么商家借款逾期怎么办 谷歌浏览器网页无法复制文字怎么办 复制粘贴在文件本里找不到了怎么办 淘宝商家给我寄错东西怎么办 美团客户点餐商家没有了怎么办 如果银行卡里的钱突然没有了怎么办 微信绑定的银行卡怎么没有了怎么办 西亚超市商品退货赠品不退怎么办 淘宝发布宝贝推荐橱窗位不够怎么办 抖音和微信软件冲突怎么办 淘宝开过店想给别家做客服怎么办 学网上开店别人不主动教你怎么办 淘宝店铺身份信息复核逾期了怎么办 一件代发进货后如果没卖出去怎么办 国外供货商收了钱不发货怎么办 企业被注销之后淘宝企业店怎么办 在淘宝上买了东西店家关门了怎么办 在淘宝上买东西被店家骂了怎么办 淘宝店家错发了两次货怎么办 我的身份证注销了淘宝支付宝怎么办 买了空调坏了店家不管怎么办 支付宝充话费没到账也不退款怎么办 淘宝上店家关铺无法赔偿怎么办 未发货申请退款卖家拒绝怎么办 淘宝订单未发货买家恶意退款怎么办 淘宝被限制购买还有没退款的怎么办 复制粘贴了打字在复制下页怎么办?