Codeforces Round #331 (Div. 2) C. Wilbur and Points

原创 2015年11月21日 17:24:47

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
5
2 0
0 0
1 0
1 1
0 1
0 -1 -2 1 0
output
YES
0 0
1 0
2 0
0 1
1 1
input
3
1 0
0 0
2 0
0 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 is 0, 1, 2. Therefore, the answer does not exist.

这道题的题意真的好难搞懂,意思就是说,给你n个点,每个点都有一个美值,美值是指那个点的y坐标减去x坐标得出来的值,最后再输入一个长为n的数组,这个数组对应每个点的美值。如果有个点的美值没有对应点,输出NO,另外,输出要按照最后给出的那个数组的顺序来输出点的坐标,同时还有一个条件,那就是,如果有 x<=x 并且 y<=y,那么点(x,y)必须要在点(x,y)的前面,如果不符合这个条件,输出NO,如果以上条件都符合,输出YES,并且按照最后的那个数组的顺序输出对应的点。

首先,要把每个值的美值求出来,然后按照美值的大小排序,如果美值相同,就按照x和y的大小排序,最后的数组也要进行排序,如果相同,就按照位置大小排序,然后将他们一一对应,如果有一个点没有对应的值,输出NO,反之,记录下点的位置和美值的关系,还有位置关系。再按照坐标大小排序,检查有没有不符合最后一个条件的点,如果有,输出NO,反之,输出YES,再按照最后一个数组的顺序输出其对应的点。

P.S. 这道题,不能用两个嵌套式的for循环,否则超时。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
struct node
{
    int x;
    int y;
    int z;
    int f;
    int s;
}point[100010];
struct tnode
{
    int x;
    int y;
    int z;
}p[100010];
bool cmp(tnode a,tnode b)
{
    if(a.x==b.x)  return a.y<b.y;
    else  return a.x<b.x;
}
bool cmp1(node a,node b)
{
    if(a.x==b.x)  return a.y<b.y;
    else  return a.x<b.x;
}
bool cmp2(node a,node b)
{
    return a.s<b.s;
}
bool cmp3(node a,node b)
{
    if(a.z==b.z)  return a.s<b.s;
    else  return a.z<b.z;
}
bool cmp4(tnode a,tnode b)
{
    if(a.z==b.z)
    {
        if(a.x==b.x)  return a.y<b.y;
        else  return a.x<b.x;
    }
    else  return a.z<b.z;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)  
    {
        scanf("%d %d",&p[i].x,&p[i].y);
        p[i].z=p[i].y-p[i].x;
    }
    for(int i=0;i<n;i++)  
    {
        scanf("%d",&point[i].z);
        point[i].s=i;
    }
    sort(p,p+n,cmp4);
    sort(point,point+n,cmp3);
    int flag=0; 
    for(int i=0;i<n;i++)
    {
        if(point[i].z==p[i].z&&!point[i].f)
        {
            point[i].x=p[i].x;
            point[i].y=p[i].y;
            point[i].f=1;
        }
        else
        {
            flag=1;
            break;
        }
    }
    int kk=0;
    sort(point,point+n,cmp1);
    int ans=point[0].x;
    int nut=point[0].y;
    int seat=point[0].s;
    for(int i=1;i<n;i++)
    {
        if(ans<=point[i].x&&nut<=point[i].y)
        {
            if(seat>point[i].s)
            {
                kk=1;
                break;
            }
            else
            {
                ans=point[i].x;
                nut=point[i].y;
                seat=point[i].s;
            }
        }
    }
    sort(point,point+n,cmp2);
    if(flag||kk)  printf("NO\n");
    else
    {
        printf("YES\n");
        for(int i=0;i<n;i++)  printf("%d %d\n",point[i].x,point[i].y);
    } 

    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

数学推公式——较难——Codeforces Round #187 (Div. 2)

题目链接: http://codeforces.com/contest/315/problem/C C. Sereja and Contest time limi...
  • CS_liuqing
  • CS_liuqing
  • 2013年06月16日 15:34
  • 1985

【Codeforces Round 340 (Div 2)E】【莫队算法 真实区间思想】XOR and Favorite Number m组区间询问 问区间中多少连续段异或值为k

E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megab...
  • snowy_smile
  • snowy_smile
  • 2016年02月28日 13:06
  • 2838

Codeforces 题目合集+分类+蒟蒻的代码 【Updating...】【232 in total】

Codeforces 题目合集+分类+蒟蒻的代码 【Updating...】
  • my_sunshine26
  • my_sunshine26
  • 2017年07月29日 23:42
  • 1384

Codeforces Round #331 (Div. 2) C. Wilbur and Points 贪心+排序

题目~~ 题意: 有n个坐标,wi为(yi-xi),再给出n个wj(j=0~n), wi==wj 则第i个坐标标号为j,而满足xi...
  • u014445884
  • u014445884
  • 2015年11月18日 10:39
  • 324

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

Wilbur and Points time limit per test 2 seconds memory limit per test 256 megabytes input stan...
  • snowy_smile
  • snowy_smile
  • 2015年12月04日 09:32
  • 581

Codeforces Round #331 (Div. 2)——C. Wilbur and Points

MD,又因为一个傻逼错误搞了一个下午+一个晚上。。忧桑= = 题意: 现在有n个点,然后给出n个点的x,y坐标。然后这些坐标必须满足条件:比如说分配给点(x,y)的标号是i,那么点(x',y')的标号...
  • ACMer_hades
  • ACMer_hades
  • 2015年11月17日 21:31
  • 365

Codeforces Round #331 (Div. 2) _A. Wilbur and Swimming Pool

A. Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megab...
  • xiaotan1314
  • xiaotan1314
  • 2015年11月16日 13:20
  • 272

【Codeforces Round 331 (Div 2)B】【水题 贪心】Wilbur and Array 修改对p位置后的所有位置生效 最少修改次数使得数列变成b

Wilbur and Array time limit per test 2 seconds memory limit per test 256 megabytes ...
  • snowy_smile
  • snowy_smile
  • 2015年12月03日 21:54
  • 536

【Codeforces Round 331 (Div 2)A】【水题】Wilbur and Swimming Pool 给出矩形1~4个顶点让你还原矩形

Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megabyte...
  • snowy_smile
  • snowy_smile
  • 2015年12月03日 21:53
  • 413

Codeforces Round #331 (Div. 2)-Wilbur and Swimming Pool(模拟)

A. Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megab...
  • zsc2014030403015
  • zsc2014030403015
  • 2015年11月16日 09:16
  • 278
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #331 (Div. 2) C. Wilbur and Points
举报原因:
原因补充:

(最多只允许输入30个字)