续续 Codeforces 596 C Wilbur and Points
来源:互联网 发布:复旦cpu卡算法 编辑:程序博客网 时间:2024/06/05 04:24
传送门:
http://codeforces.com/contest/596/problem/C
附上那份符合我当初屌丝想法的只排序不需要任何数据结构的写法
其实本质思想都是贪心,即先通过排序找到每个w[i]所对应的点号,然后保存下来
具体找法还是贪心去找,即x小优先,否则y小优先!!!!!
然后再O(n)扫一遍去判断合法性就ok了,其实我这个方法也是蛮优美的,因为它只需要把两边都排次序就可以完成匹配了。。。
不过就是我刚开始想的方法就是错的(虽然大概思想是对的),我的排序方法被样例给带偏了,这题首要想法就应该是贪心!!!
一共对两个数组排两次序,第一次是为了记录位置,第二次是为了恢复原来的序列,然后进行判断,哎,这么裸的想法,我当时怎么就没想到要恢复原序列再O(n)扫一遍判断合法性呢???
注意这种写法容易忽略掉的一点就是在对权值排序的时候如果相等的话要保证id的有序性,因为这道题目的本质就是判断给定的权值序列能否构成,如果不能保证之前的有序性就肯定会出问题的!!!!
(符合我前两份代码中确定解的时候的过程)
这份代码的优美之处就在于,它巧妙地利用第一次排序之后的结果把相应的位置记录了下来,记录下来了每一个w在原先线段序列中的位置!!!!
现在一看其实这题真的是蛮sb的!!!!
就是贪心加判断就ok了!!!!!
#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>#include<math.h>#include<stdlib.h>#include<vector>#include<map>#include<string>using namespace std;#define ll long long#define For(i,n) for(int i=1;i<=n;i++)#define Dor(i,n) for(int i=n;i>=1;i--)#define s(n) scanf("%d",&n)#define p(n) printf("%d\n",n)#define maxn 100000+10#define ms(n) memset(n,0,sizeof(n))#define lson l , m , rt << 1#define rson m + 1 , r , rt << 1 | 1const double eps=1e-8;const int inf=0x3f3f3f3f;int n;struct node{ int x,y,w,id,pre;}a[maxn],b[maxn];bool cmp(node a, node b) { if(a.w!=b.w) return a.w<b.w; if(a.x!=b.x) return a.x<b.x; if(a.y!=b.y) return a.y<b.y;}bool cmp1(node a,node b){ if(a.w==b.w) return a.id<b.id; return a.w<b.w;}bool cmp2(node a,node b){ return a.id<b.id;}int main(){ s(n); for(int i=0;i<n;i++) { s(a[i].x);s(a[i].y); a[i].id=i; a[i].w=a[i].y-a[i].x; } for(int i=0;i<n;i++) { s(b[i].w);b[i].id=i; } sort(a,a+n,cmp); sort(b,b+n,cmp1);int flag=1; for(int i=0;i<n;i++) { if(a[i].w!=b[i].w) { flag=0;break; } else b[i].pre=a[i].id; } if(!flag) { puts("NO");return 0; } sort(a,a+n,cmp2);sort(b,b+n,cmp2); flag=1; for(int i=1;i<n;i++) { if((a[b[i].pre].x>a[b[i-1].pre].x||a[b[i].pre].y>a[b[i-1].pre].y)) { continue; } else { flag=0;break; } } if(flag) { puts("YES"); for(int i=0;i<n;i++) { printf("%d %d\n",a[b[i].pre].x,a[b[i].pre].y); } } else puts("NO"); return 0;}
0 0
- 续续 Codeforces 596 C Wilbur and Points
- 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
- 596 C. Wilbur and Points
- Codeforces 596C Wilbur and Points 【贪心 + sort】
- Codeforces Round #331 (Div. 2) C. Wilbur and Points
- Codeforces Round #331 (Div. 2) C. Wilbur and Points
- Codeforces Round #331 (Div. 2) C. Wilbur and Points
- codeforces#331-C - Wilbur and Points-set/贪心乱搞
- Codeforces Round #331 (Div. 2)-C. Wilbur and Points
- cf C. Wilbur and Points (STL_vector)
- Codeforces Round #331 (Div. 2)C. Wilbur and Points(模拟+STL)
- Codeforces Round #331 (Div. 2)——C. Wilbur and Points
- Codeforces Round #331 (Div. 2) C. Wilbur and Points 贪心+排序
- Codeforces Round #331 (Div. 2)-C-Wilbur and Points(STL容器)
- Codeforces596C. Wilbur and Points
- JQuery不同版本的差异(checkbox)
- JAVA之JDBC连接MYSQL
- springmvc(六)商品修改功能开发
- 11464Even Parity
- mac版mysql 5.6 与5.7的安装与测试
- 续续 Codeforces 596 C Wilbur and Points
- Linux虚拟机启动级别和网络设置
- Xcode和模拟器的清除缓存
- 7、单例设计模式
- 分享周鸿祎的《如何建立一个“铁打的营盘”》
- 继承Binder类绑定服务显示时间
- boost库中 不同版本载入lib的方式
- javaEE之-------Spring中的aspectJ的应用
- 考研后的Java温习之I/O