第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明)L (LIS+二分)

第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明)L (LIS+二分)

思路: 有点自闭的一题,总结一下。因为所有逆序对之间必有连线那么假如有: a i 1 > a i 2 > a i 3 > a i 4 . . . > a i 7 ai_1>ai_2>ai_3>ai_4...>ai_7 ai1>ai2>ai3>ai4...>ai7那么至少存在的颜色为 7 7 7种。那么答案即为最长下降子序列,反着写即是最长上升子序列。接下去就是构造,难点在于每一个点应该赋值为什么颜色,有点类似与拦截导弹,如果正着推,那么每次出现一个数 a [ i ] < a [ i + 1 ] a[i]<a[i+1] a[i]<a[i+1]那么我们查找前面网中第一个高度小于它的网然后更新即可。反着推,那么如果出现 a [ i + 1 ] < a [ i ] a[i+1]<a[i] a[i+1]<a[i]那么在已有网中找到第一个高度大于它的网然后更新,数据很大得用 s c a n f scanf scanf

  • 正面LIS+二分
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
int a[N];
int tot;
int b[N];//存拦网
int c[N];//颜色
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        tot=1;
        b[tot]=a[1];
        c[1]=tot;
        for(int i=2;i<=n;i++){
            if(a[i]<b[tot]){
                b[++tot]=a[i];
                c[i]=tot;
            }
            else{
                int l=1,r=tot;
                while(l<r){
                    int mid=l+r>>1;
                    if(b[mid]<a[i])r=mid;
                    else l=mid+1;
                }
                c[i]=l;
                b[l]=a[i];
            }
        }
        printf("%d\n",tot);
        for(int i=1;i<=n;i++)printf("%d ",c[i]);
        printf("\n");
    }
    return 0;
}
  • 反面LIS+二分
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
int a[N],b[N],c[N]; 
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)scanf("%d",&a[i]);
		int tot=1;
		c[tot]=a[n];
		b[n]=1;
		for(int i=n-1; i>=1; i--)
		{
			if(c[tot]<a[i]) 
			{
				c[++tot]=a[i];
				b[i]=tot;
			}
			else
			{
				int p=lower_bound(c+1,c+tot+1,a[i])-c;
				b[i]=p;
				c[p]=a[i];
			}
		}
		printf("%d\n",tot);
		for(int i=1;i<=n;i++)
		{
			printf("%d ",b[i]);
		}
		printf("\n");
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值