题目:
来源:https://vjudge.net/contest/293466#problem/I
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 2005 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 prestige 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.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
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 Input
1
4
1 1
1 2
2 1
2 2
Sample Output
2
1 4
题意:
给出m个人,每个人有两个属性:强壮度(s),美丽度(b),如果a的s,b中任意一个属性大于等于y的对应属性,另一个属性小于y,两个人就会打架。现在要办一个party,要求邀请尽可能多的人,并且他们不能打架。输出人数和每个人的编号(编号从1开始)
思路:
先贪心排序,按照从前到后强壮度递增,强壮度相同时美貌度递增的方法排序,然后求出最长严格递增子序列,输出序列长度和里面元素的编号。
我用了一个数组去记录子序列中当前数的前一个数的位置,输出的时候根据数组倒序找就可以了
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#define N 100100
using namespace std;
struct node
{
int x,y,pos;
} g[N];
int b[N];
int pre[N],pb[N];//pre[i]存储的是当前位置的前一个位置,pb[i]存储b数组的对应位置的位置
bool cmp(node a,node b)//x按照从小到大牌,按照从大到小排
{
if(a.x==b.x)
return a.y>b.y;
return a.x<b.x;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(b,0,sizeof(b));
memset(pre,0,sizeof(pre));
memset(pb,0,sizeof(pb));
int len=0;
for(int i=0; i<n; i++)
{
scanf("%d%d",&g[i].x,&g[i].y);
g[i].pos=i;
}
sort(g,g+n,cmp);
int sb;
for(int i=0; i<n; i++)
{
int p=lower_bound(b,b+len,g[i].y)-b;
if(p==0)
pre[i]=-1;
else
pre[i]=pb[p-1];//存储
pb[p]=i;
b[p]=g[i].y;
if(p==len)
{
len++;
sb=i;//记录lis的最小末尾的位置
}
}
printf("%d\n",len);//输出递增子序列长度
int pp=0;//用来控制空格输出
while(sb!=-1)
{
if(pp)printf(" ");
pp++;
printf("%d",g[sb].pos+1);//因为是从0开始的,所以要+1
sb=pre[sb];
}
puts("");
}
return 0;
}