zoukankan      html  css  js  c++  java
  • 【26.67%】【codeforces 596C】Wilbur and Points

    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.

    Examples
    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.

    【题目链接】:http://codeforces.com/contest/596/problem/C

    【题解】

    这里写图片描述
    题中所给的y-x这个特殊值;其实就是如上,约束这些点在y-x=wi这条线上;
    而题中所给的条件转换一下可以理解为;
    对于任意(x,y);如果x’<=x && y’<=y;则(x’,y’)的序号要在(x,y)这个点之前;
    最后所给的要求序列;
    相当于我们要在y-x=wi这条线段上选一个点放在序列的相应位置;
    那我们要怎么选呢?
    这里写图片描述
    肯定要从上图上的这些圆圈圈中的点开始选(即从线的左下角开始往右上角依次选);
    现在假设我们选过的点都标为红色;
    然后w=1;
    则我们要选如下这个点
    这里写图片描述
    那我们能选这个这个点吗?
    答案是不行;
    因为在这个蓝点的正下方有一个点(1,1)还没被选;
    如果我们先选了那个蓝点;必然下面这个点会在这个蓝点的序号之后(所有的点都会出现,所以下面那个点是肯定会在后面选的);
    但是下面那个点的横坐标1<=1,且纵坐标1<=2;这个点是肯定要在这个蓝点之前被选的;【还记的我们转换成的条件吗:对于任意(x,y);如果x’<=x && y’<=y;则(x’,y’)的序号要在(x,y)这个点之前;】
    这就说明序列不合法;
    因为我们是按照直线从左下往右上选的;而且每次都只选一个点;所以我们可以在选一个点(x,y)的时候先判断(x-1,y)和(x,y-1)有没有被选(边界就不用判断了);如果其中有一个点没被选;则不满足条件;
    (我们可以在遇到w的时候,看看y-x=w这条线上从左下到右上的第一个没被选的点是啥(记录这条线上被选过的点的最右上那个点的横坐标即可),然后判断一下x-1,y和y,x-1有没有被加入balabalba一下就可以过了);

    【完整代码】

    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <set>
    #include <map>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <stack>
    #include <string>
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    
    using namespace std;
    
    //const int MAXN = x;
    const int dx[5] = {0,1,-1,0,0};
    const int dy[5] = {0,0,0,-1,1};
    const double pi = acos(-1.0);
    map <int,int> dic;
    map <int,int> maxx;
    map <pair <int,int>,int> pre;
    
    int n;
    vector <pair <int,int> > ans;
    
    void rel(LL &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t) && t!='-') t = getchar();
        LL sign = 1;
        if (t == '-')sign = -1;
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
        r = r*sign;
    }
    
    void rei(int &r)
    {
        r = 0;
        char t = getchar();
        while (!isdigit(t)&&t!='-') t = getchar();
        int sign = 1;
        if (t == '-')sign = -1;
        while (!isdigit(t)) t = getchar();
        while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
        r = r*sign;
    }
    
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(n);
        for (int i = 1;i <= n;i++)
        {
            int x,y;
            rei(x);rei(y);
            int temp = y-x;
            dic[temp]++;
        }
        for (int i =-100000;i <= 0;i++)
            maxx[i] = -i;
        for (int i = 1;i <= n;i++)
        {
            int t;
            rei(t);
            int x,y;
            x = maxx[t];y = x+t;
            ans.push_back(make_pair(x,y));
            if (x-1>=0 && !pre[make_pair(x-1,y)])
            {
                puts("NO");
                return 0;
            }
            if (y-1>=0 && !pre[make_pair(x,y-1)])
            {
                puts("NO");
                return 0;
            }
            pre[make_pair(x,y)] = 1;
            dic[t]--;
            maxx[t]++;
        }
        for (int i = -100000;i <= 100000;i++)
            if (dic[i]!=0)
            {
                puts("NO");
                return 0;
            }
        puts("YES");
        for (int i = 0;i <= n-1;i++)
            printf("%d %d
    ",ans[i].first,ans[i].second);
        return 0;
    }
  • 相关阅读:
    Linux下JDK的安装
    Docker 搭建 Maven 私服
    K8s 部署 PostgreSQL
    CentOS7 使用 kubeadm 部署 K8s(单机/集群)
    CentOS7 升级 Vim
    Go 函数详解
    CentOS7 安装 golang
    Redis 集群伸缩原理
    CentOS7 安装 Redis
    CentOS7 搭建 Redis 集群
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7632075.html
Copyright © 2011-2022 走看看