C. Wilbur and Points (단순 한 욕심 과 합 법성 판단)

4730 단어 데이터 구조CF
C. Wilbur and Points
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.
문제 풀이: 욕심 이 먼저 한 조 의 해 를 구성 한 다음 에 해 의 합 법성 을 판단 해 야 한다 고 생각 하기 쉽다. 즉, 편차 (존재 (x, y) 의 번 호 는 i 이 고 모든 (a, b) 의 번호 j, j > i, a > = x, b > = y) 를 만족 시 켜 야 한다. 이것 은 나무 모양 의 배열 을 이용 하여 해 를 판단 할 수 있다.
AC 코드:
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
typedef pair<int,int> P;
map<int,int> mp,tp;
int x[N],y[N],n,c[N];

bool check() {
    for(auto e: tp) {
        if(e.second) return false;
    }
    return true;
}

void update(int x,int val) {
    while(x <= n + 1) {
        if(c[x] > val) c[x] = val;
        x += x & -x;
    }
}

int que(int x) {
    int s = INF;
    while(x > 0) {
        if(c[x] < s) s = c[x];
        x -= x & -x;
    }
    return s;
}

bool solve() {
    for(int i = n - 1; i >= 0; i--) {
        if(que(x[i] + 1) <= y[i]) return false;
        else update(x[i] + 1,y[i]);
    }
    puts("YES");    
    for(int i = 0; i < n; i++) {
        printf("%d %d
",x[i],y[i]); } return true; } int main() { cin>>n; memset(c,0x3f,sizeof(c)); for(int i = 0; i < n; i++) { int x,y; scanf("%d %d",&x,&y); tp[y - x]++; } for(int i = 0; i < n; i++) { int w; scanf("%d",&w); x[i] = abs(w) + mp[w]; y[i] = mp[w]; mp[w]++; tp[w]--; if(w > 0) swap(x[i],y[i]); } if(check() == false) puts("NO"); else if(solve() == false) puts("NO"); return 0; }

좋은 웹페이지 즐겨찾기