[堆+贪心] CF596C. Wilbur and Points
来源:互联网 发布:java预览excel 编辑:程序博客网 时间:2024/06/09 18:49
CF596C. Wilbur and Points
题意:
太难懂了。有一个平面点集,对集合中任意一点(x,y),满足以原点和(x,y)构成的矩形边上和内部的所有点也都在集合里。
现在要给点标号,为了美观,当给(x,y)标号为i时,要求以(x,y)为左下角的矩形边上和内部所有点(如果在集合内)的标号都不小于i。
然后还有一个条件,标号为i的点(x,y)必须满足 y-x == wi。
输入:
一个数n,然后是n个点,最后是n个数wi。
输出:
是否存在满足条件的标号方法,输入YES or NO,YES的话还要按从小到大标的号输出点的坐标。
题解:
先以y-x为关键字把点存进堆里,维护一个最小堆,每次要wi的时候就从wi堆里把根节点拿出来。当wi堆为空的时候显然就无解,否则因为美观的条件,显然每次贪心标x最小的(y也是最小的)是最优的。
然后还要维护以y为下标的一个数组out,表示坐标为y的点已经往右标到的x坐标,维护x坐标严格按1递增,同时y较大的out值不能超过y较小的out值,否则会违反美观原则。
#include<bits/stdc++.h>using namespace std;typedef pair<int,int> pii;typedef priority_queue<pii, vector<pii>, greater<pii> > pair_heap;map<int, pair_heap> heap;map<int, int> out; // 因为y-x有负数,所以开map做成伪数组vector<pii>ans;int main(){ int n; scanf("%d", &n); for(int i = 1; i <= n; ++i){ int a, b; scanf("%d%d", &a, &b); heap[b-a].push(make_pair(a,b)); } int flag = 1; for(int i = 1; i <= n && flag; ++i){ int tmp; scanf("%d", &tmp); pair_heap &now = heap[tmp]; if(now.empty()){ flag = 0; break; } // 堆为空就无解 ans.push_back(now.top()); int ansy = now.top().second, ansx = now.top().first; now.pop(); int &z = out[ansy]; if(z == ansx) { z += 1; // 维护x依次递增 if(ansy > 0 && z > out[ansy-1]) flag = 0; // 较大的y不能有大的out值 } else flag = 0; } if(!flag){ puts("NO"); return 0; } puts("YES"); for(auto&i : ans) printf("%d %d\n", i.first, i.second);}
0 0
- [堆+贪心] CF596C. Wilbur and Points
- Codeforces596C Wilbur and Points(贪心)
- Codeforces 596 C Wilbur and Points【贪心】
- Codeforces596C. Wilbur and Points
- C. Wilbur and Points(简单贪心加合法性判断)
- Codeforces 596C Wilbur and Points 【贪心 + sort】
- codeforces#331-C - Wilbur and Points-set/贪心乱搞
- 596 C. Wilbur and Points
- Codeforces Round #331 (Div. 2) C. Wilbur and Points 贪心+排序
- cf C. Wilbur and Points (STL_vector)
- Codeforces 596 C Wilbur and Points
- 续 Codeforces 596 C Wilbur and Points
- 续续 Codeforces 596 C Wilbur and Points
- 【26.67%】【codeforces 596C】Wilbur and Points
- 巧妙地利用STL map set pair 贪心+排序 Codeforces Round #331 (Div. 2)C. Wilbur and Points
- 【Codeforces Round 331 (Div 2)C】【脑洞 SET贪心写法】Wilbur and Points 锯齿图形差值编号,右上角的最大
- Codeforces 596B Wilbur and Array 【贪心】
- CodeForces 596B Wilbur and Array 贪心
- ListView常用小技巧
- 寻找考场
- 1004 看节目问题 ACID 00732018
- 有符号数与无符号数
- ffmpeg /x264视频流编解码末尾丢帧问题分析和解决
- [堆+贪心] CF596C. Wilbur and Points
- Win7 x64 VC6无法调试程序的bug解决方法
- Collection
- android开发之SQLite详解
- 模板函数需要将声明和实现放到同一个文件中
- 简单的网页处理工具-HtmlParser
- hdu 1864 最大报销额01背包dp
- Unity小技巧
- poj 2506 Tiling 《大数加法+递推》