Codeforces Round #331 (Div. 2)C. Wilbur and Points 贪心
2015-11-16 03:18
302 查看
C. Wilbur and Points
Time Limit: 20 SecMemory Limit: 256 MB
题目连接
http://codeforces.com/contest/596/problem/CDescription
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 Input
5 2 0 0 0 1 0 1 1 0 1 0 -1 -2 1 0
Sample Output
YES 0 0 1 0 2 0 0 1 1 1
HINT
题意给你一堆点,要求你找到一个集合,使得他的y[i]-x[i]=w[i]
且如果xj>=xi && yj>=yi,那么id[i]>id[j]
问你能否找到
题解:
我们贪心取最小就好了,然后再check一下就好了
check可以使用线段树,也可以先按照Y排序,然后再按照X排序,再扫一遍来check
代码
#include<iostream> #include<stdio.h> #include<math.h> #include<algorithm> #include<map> #include<vector> using namespace std; struct node { int x,y,z; int id; }; node p[100005]; bool cmp(node a,node b) { if(a.x==b.x&&a.y==b.y)return a.id<b.id; if(a.x==b.x)return a.y<b.y; return a.x<b.x; } int b[100005]; map<int,int> H; vector<int> X; vector<int> Y; int tot = 1; vector<node> Q[100005]; int add[100005]; vector<node> Q2[100005]; int main() { int n;scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d",&p[i].x,&p[i].y); p[i].z = p[i].y-p[i].x; if(H[p[i].z]==0) H[p[i].z]=tot++; Q[H[p[i].z]].push_back(p[i]); } for(int i=1;i<tot;i++) sort(Q[i].begin(),Q[i].end(),cmp); for(int i=1;i<=n;i++) { scanf("%d",&b[i]); if(H[b[i]]==0) return puts("NO"); int t = H[b[i]]; if(add[t]==Q[t].size())return puts("NO"); X.push_back(Q[t][add[t]].x); Y.push_back(Q[t][add[t]].y); add[t]++; } for(int i=0;i<n;i++) { node kkk; kkk.x = X[i],kkk.y = Y[i]; kkk.id = i; Q2[kkk.y].push_back(kkk); } for(int i=0;i<=100000;i++) sort(Q2[i].begin(),Q2[i].end(),cmp); for(int i=0;i<=100000;i++) { if(Q2[i].size()<=1)continue; for(int j=0;j<Q2[i].size()-1;j++) { if(Q2[i][j].id>Q2[i][j+1].id) return puts("NO"); } } puts("YES"); for(int i=0;i<X.size();i++) { printf("%d %d\n",X[i],Y[i]); } }
相关文章推荐
- Codeforces Round #331 (Div. 2) B. Wilbur and Array 水题
- Codeforces Round #331 (Div. 2) A. Wilbur and Swimming Pool 水题
- Educational Codeforces Round 1 D. Igor In the Museum(BFS)
- 周群飞:老板大都是逼上梁山的
- IOS之UI--小实例项目--综合使用
- 关于jsp页面一些bug问题
- Educational Codeforces Round 1 B. Queries on a String(字符串简单操作)
- Educational Codeforces Round 1 A. Tricky Sum(简单模拟求和)
- PHP 计算下个月当天日期,解决跨年和2月份的各种问题
- 交叉验证
- Notification相关和一些关键点收集
- tomcat图形化界面管理及数据源配置
- android基础--Handler,Loop,MessageQueue
- 从下至上按层遍历二叉树
- [置顶] 中文知识图谱研讨会的学习总结 (上) 图谱引入、百度知心、搜狗知立方
- 中文知识图谱研讨会的学习总结 (上) 图谱引入、百度知心、搜狗知立方
- Servlet学习笔记
- Android笔记(五十九)Android总结:四大组件——Service篇
- ViewPager+Fragment实现左右滑动页
- ViewPager+Fragment实现左右滑动页