HDOJ1025隐藏着的LIS

24 篇文章 0 订阅
//题目连接http://acm.hdu.edu.cn/showproblem.php?pid=1025
//这里题目要特别注意了,会做LIS的,但是如何想到这个题目就是LIS很重要.
//这是需要在ACM的过程中培养的一种很重要的能力.举一反三...能够很快归类.
//如何将本题与LIS联系起来:
//若3->8,那么4-7的任何一个城市都不可以连接.即LIS中1,2,8,3,4,5,6,7...;若要8的话后面的比8小的都不可以.
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<algorithm>


const int inf = 0x3f3f3f;
using namespace std;
int r,c[500001],a[500001],n;
int dich(int x , int y)
{
    int left = 1,right = y;
    while(right >= left)
    {
        int mid = (right+left)/2;
        if(x > c[mid])left = mid+1;
        else right = mid-1;
    }
    r = max(r,left);
    return left;
}
void LIS(int a[])
{
     r = 1 ; c[1] = a[0];
    for(int i = 1 ; i < n ; i++)
    {
        c[dich(a[i],r)] = a[i];
    }
}
int main()
{
    int t = 0 ;
    while(scanf("%d",&n) != EOF)
    {
        t++;
        memset(c,0,sizeof(c));
        int mm,nn;
        for(int i = 0 ; i < n ; i++)
        {
            scanf("%d%d",&mm,&nn);
            a[mm-1] = nn-1;//还有这么一个思想.最开始的时候是想的是调用快排.为嘛会超时还不知道.
        }                  //这样搞例如1->6 那么a[1]=6就相当于那个意思.
        LIS(a);
        printf("Case %d:\n",t);
        if(r != 1)
        printf("My king, at most %d roads can be built.\n\n",r);//最后还有一个单复数问题.
        else printf("My king, at most %d road can be built.\n\n",r);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值