最长递增子序列 O(NlogN)算法,mark数组。ZOJ Problem Set - 2319— Beautiful People

              ZOJ Problem Set - 2319   题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1319

Beautiful People


Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge

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

题意:题目让求n对数中,最多有多少对满足题意的数字,并输出他们的编号(如果一对数的两个数字都分别大于另一对数的两个数,那么这两个就是符合题意的,求最多能有多少对(他们中的任意两个都要符合题意))

思路:把其中一个元素排序,另一个元素找最长递增子序列(两种方法:一种是n^2,一种是nlogn)。n非常大所以肯定用nlogn。(以前认为学会n^2就够了,现在觉得还是很有必要学一下nlogn的,毕竟题目会越来越难的)

用结构体存数据,有一个是数组b【i】是存  能组成长度为i的最小的数字,还有一个mark【i】存第i位能组成的长度的最大值(注意不是前i位,是第i位)。

举个栗子:

例如:5 8 4 6 7 3 9 14 12 13

第1个数字:b【1】=5,mark【1】=1;

第2个数字:b【2】=8,mark【2】=2;

第3个数字:b【1】=4,mark【3】=1;          1(有1的为选上的,下面有解释)

(现在第三个数字是4,在当前b【】中找一个大于等于它的数字(最小的数),更新它的值,若找不到则长度+1)

第4个数字:b【2】=6,mark【4】=2;          1

第5个数字:b【3】=7,mark【5】=3;          1

第6个数字:b【1】=3,mark【6】=1;        

第7个数字:b【4】=9,mark【7】=4;          1

第8个数字:b【5】=14,mark【8】=5;      

第9个数字:b【5】=12,mark【9】=5;        1

第10个数字:b【6】=13,mark【10】=6;    1

注意b【】中存的不一定是最长子序列的数字,但是长度一定是最长的6;b【i】是存  能组成长度为i的最小的数字。

用mark【】找组成最长子序列的数字(倒序找)先找mark【】=6的,再找mark【】=5的,依次类推:

mark【】后面带1的是找到的;

      k代表最大的长度。

         for(int i=n;i>=1;i--)
            {
                if(mark[i]==k)
                {
                    printf("%d ",q[i].z);
                    k--;
                }
            }

输出:13,12,9,7,6,4.

这道题还有一点不好想:怎么处理相等的问题,我把第一个排序(有相等的),后面的数字要最长递增子序列,怎么保证前面的数字不相等?如果前面的数字相同,那我就在排序的时候,(第一个数字相等的情况下)不让后面的数字大于前面的数字,这样就保证(第一个数字相等)第二个数字最多只能选一个(因为它是递减的)。具体看cmp函数


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
using namespace std;
typedef long long int ll;
const int mod = 1e7+7;
const int INF = 0x3f3f3f3f;
const int maxn = 5e4 +10;
int n;
struct node
{
    int x,y,z;
}q[100050];
int cmp(node s,node ss)
{
    if(s.x==ss.x)
        return s.y>ss.y;
    return s.x<ss.x;
}
int mark[100050],b[100050];
int main()
{
    int t;
    scanf("%d",&t);//t的输入没有用,反正题上要求的输入,
    int hh=0;
    while(~scanf("%d",&n))//多组输入
    {
        if(hh++)
        printf("\n");//控制格式
        memset(mark,0,sizeof(mark));
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&q[i].x,&q[i].y);
               q[i].z=i;
        }
            sort(q+1,q+n+1,cmp);
//            for(int i=1;i<=n;i++)
//                printf("%d %d\n",q[i].x,q[i].y);
              int k=0;
            for(int i=1;i<=n;i++)
            {
                int j=lower_bound(b+1,b+k+1,q[i].y)-b;//在当前b【】中找到第一个大于等于它的数字的编号
                b[j]=q[i].y;
                mark[i]=j;
                k=k>j?k:j;//k是最大长度
            }
            int flag=0;
            printf("%d\n",k);
            for(int i=n;i>=1;i--)
            {
                if(mark[i]==k)
                {
                    if(flag++)//控制格式
                        printf(" ");
                    printf("%d",q[i].z);
                    k--;
                }
            }
            printf("\n");
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值