您的位置:首页 > 运维架构

sgu 199 Beautiful People

2012-11-08 12:31 337 查看

199. Beautiful People

time limit per test: 0.5 sec.

memory limit per test: 65536 KB
input: 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.

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

最长严格单调递增子序列的变形,我们先按s从小到大排序,再按b从大到小排序。然后对b跑模板,就可以了。

代码的实现花了很多时间,一直WA#17 ,原因不明,后来重打。

贴上代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>

#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)

using namespace std;
int const nMax = 100110;
int const base = 10;
typedef int LL;
typedef pair<LL,LL> pij;

//    std::ios::sync_with_stdio(false);

struct Int {
LL s,b;
int id;
bool operator < (const Int & a) const {
if(s!=a.s) return s<a.s;
return b>a.b;
}
} a[nMax];

int n;
int dp[nMax],f[nMax],fa[nMax],lo[nMax];
int size;

int bsearch(int a) {
int l=1,r=size;
int ans=0;
while(l<=r) {
int mid=(l+r)/2;
if(a>f[mid]) {
if(ans<mid) ans=mid;
l=mid+1;
}else {
r=mid-1;
}
}
return ans+1;
}

int LIS(){
size=0;
f[0]=0;lo[0]=0;
for(int i=1;i<=n;i++) {
int j=bsearch(a[i].b) ;
if(j>size) {
lo[j]=i;
f[j]=a[i].b;
fa[i]=lo[j-1];
size++;
}else {
lo[j]=i;
f[j]=a[i].b;
fa[i]=lo[j-1];
}
}
return size;
}

main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d%d",&a[i].s,&a[i].b);
a[i].id=i;
}
sort(a+1,a+n+1);
//for(int i=1;i<=n;i++) printf("%d %d\n",a[i].s,a[i].b) ;
int ans=LIS();
printf("%d\n",ans);
int u=lo[ans];
while(u) {
printf("%d ",a[u].id);
u=fa[u];
}
printf("\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: