SGU 199 Beautiful People lis O(nlogn)算法
来源:互联网 发布:js里面加载js 编辑:程序博客网 时间:2024/05/29 15:42
点击打开链接题目链接
199. Beautiful People
time limit per test: 0.25 sec.
memory limit per test: 65536 KB
memory limit per test: 65536 KB
input: standard
output: standard
output: standard
The most prestigious sports club in one city has exactly N members. Each of its members is strong and beautiful. More precisely, i-th member of this club (members being numbered by the time they entered the club) has strength Si and beauty Bi . Since this is a very prestigious club, its members are very rich and therefore extraordinary people, so they often extremely hate each other. Strictly speaking, i-th member of the club Mr X hates j-th member of the club Mr Y if Si ≤ Sj and Bi ≥ Bj or if Si ≥ Sj and Bi ≤ Bj (if both properties of Mr X are greater then corresponding properties of Mr Y, he doesn't even notice him, on the other hand, if both of his properties are less, he respects Mr Y very much).
To celebrate a new 2003 year, the administration of the club is planning to organize a party. However they are afraid that if two people who hate each other would simultaneouly attend the party, after a drink or two they would start a fight. So no two people who hate each other should be invited. On the other hand, to keep the club presti≥ at the apropriate level, administration wants to invite as many people as possible.
Being the only one among administration who is not afraid of touching a computer, you are to write a program which would find out whom to invite to the party.
To celebrate a new 2003 year, the administration of the club is planning to organize a party. However they are afraid that if two people who hate each other would simultaneouly attend the party, after a drink or two they would start a fight. So no two people who hate each other should be invited. On the other hand, to keep the club presti≥ at the apropriate level, administration wants to invite as many people as possible.
Being the only one among administration who is not afraid of touching a computer, you are to write a program which would find out whom to invite to the party.
Input
The first line of the input file contains integer N — the number of members of the club. ( 2 ≤ N ≤ 100,000 ). Next N lines contain two numbers each — Si and Bi respectively ( 1 ≤ Si, Bi ≤ 109 ).
Output
On the first line of the output file print the maximum number of the people that can be invited to the party. On the second line output N integers — numbers of members to be invited in arbitrary order. If several solutions exist, output any one.
Sample test(s)
Input
4 1 1 1 2 2 1 2 2
Output
2
1 4
需要用到O(nlogn)的算法
二分= =
这题的状态需要注意一下
因为需要输出路径
所以不能单纯记录那个值
而是应该记录这个值的位置
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN=100100;int id[MAXN],pre[MAXN];struct Node{ int a,b,id;}node[100100];int n;int cmp(Node x,Node y){ if(x.a!=y.a) return x.a<y.a; return x.b>y.b;}void print(int x){ if(x==-1) return ; print(pre[x]); printf("%d ",node[x].id);}int main(){ int i,left,right,mid; int len; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) { scanf("%d %d",&node[i].a,&node[i].b); node[i].id=i; } sort(node+1,node+1+n,cmp); //for(i=1;i<=n;i++) // printf("%d %d %d\n",node[i].a,node[i].b,node[i].id); len=1; memset(pre,-1,sizeof(pre)); id[0]=1; for(i=2;i<=n;i++) { if(node[i].b>node[id[len-1]].b) { id[len++]=i; pre[i]=id[len-2]; continue; } left=0; right=len-1; while(left<right) { mid=(left+right)/2; if(node[id[mid]].b<node[i].b) { left=mid+1; } else { right=mid; } } id[left]=i; if(left==0) pre[i]=-1; else pre[i]=id[left-1]; } printf("%d\n",len); print(id[len-1]); puts(""); } return 0;}
0 0
- SGU 199 Beautiful People lis O(nlogn)算法
- sgu 199 Beautiful People (DP-LIS O(nlogn)算法 输出所选元素)
- SGU 199. Beautiful People(最长上升子序列nlogn LIS)
- sgu 199 Beautiful People
- SGU--199 beautiful people
- SGU 199 Beautiful People
- SGU 199. Beautiful People 二维LIS
- LIS-O(NlogN)算法
- sgu 199 Beautiful People dp
- ZOJ2319 Beautiful People(贪心,最长递增子序列nlogn,LIS)
- sgu 199 beautiful people(nlog 的LIS并输出路径)
- LIS的O(nLogN)算法
- ACdream 1216 Beautiful People(二维上升子序列 O(nlogn))
- SGU 119 Beautiful People
- O(nlogn)LIS及LCS算法
- LIS的O(nlogn)算法(二分)
- ACdream 1216 Beautiful People(LIS)
- ZOJ Problem Set - 2319 Beautiful People || sgu 199
- 多线程同步执行函数,超时后结束掉线程
- 线程学习资料
- 矩形的个数
- Eclipse修改XML默认打开方式
- 在c语言写的cgi程序中如何获取URL传递的参数
- SGU 199 Beautiful People lis O(nlogn)算法
- alter database create datafile as
- 去掉Form的最大,最小和关闭按钮和边框
- 登陆拦截防止登陆页面加载到iframe里面
- cocos2dx3.2 退格键 删除键 响应
- 表现层、持久层、业务层
- Destructors That Throw and Why They're Evil
- qemu for windows
- 2013年个人微博推荐技术资料汇总-何登成