第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明) L-Simone and graph coloring

题目链接

思路

这道题,理解后就是如果存在一个从左往右看是单减的序列,那么他们的颜色一定不相同,那我们可以从右往左求LIS,对于第i个元素,以它结尾的最长上升子序列长度是多少就可以定为它的颜色。

代码

#include<bits/stdc++.h>

using namespace std;
const int N = 1e6+9;
int a[N],ans[N],low[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=0;
		low[++tot]=a[n];
		ans[n]=1;
		for(int i=n-1; i>=1; i--)
		{
			if(low[tot]<a[i]) 
			{
				low[++tot]=a[i];
				ans[i]=tot;
			}
			else
			{
				int p=lower_bound(low+1,low+tot+1,a[i])-low;
				ans[i]=p;
				low[p]=a[i];
			}
		}
		printf("%d\n",tot);
		for(int i=1; i<=n; i++)
		{
			printf("%d ",ans[i]);
		}
		puts("");
	}
	return 0;
}
posted @ 2022-08-28 08:43  翔村亲亲鸟  阅读(15)  评论(0编辑  收藏  举报