链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
题目描述
Simone, a student of Graph Coloring University, is interested in permutation. Now she is given a permutation of length nnn, and she finds that if she connects each inverse pair, she will get a graph. Formally, for the given permutation, if i<ji<ji<j and ai>aja_i>a_jai>aj, then there will be an undirected edge between node iii and node jjj in the graph.
Then she wants to color this graph. Please achieve poor Simone's dream. To simplify the problem, you just need to find a way of coloring the vertices of the graph such that no two adjacent vertices are of the same color and minimize the number of colors used.
输入描述:
There are multiple test cases. The first line of the input contains an integer T(1≤T≤106)T(1\leq T\leq 10^6)T(1≤T≤106), indicating the number of test cases. For each test case, the first line contains an integer n(1≤n≤106)n(1 \leq n \leq 10^6)n(1≤n≤106), indicating the length of the permutation. The second line contains nnn integers a1,a2,...,ana_1,a_2,...,a_na1,a2,...,an, indicating the permutation. It is guaranteed that the sum of nnn over all test cases does not exceed 10610^6106.
输出描述:
For each test case, the first line contains an integer ccc, the chromatic number(the minimal number of colors been used when coloring) of the graph. The second line contains nnn integers c1,c2,...,cnc_1,c_2,...,c_nc1,c2,...,cn, the color of each node. Notice that cic_ici should satisfy the limit that 1≤ci≤c1 \leq c_i \leq c1≤ci≤c. If there are several answers, it is acceptable to print any of them.
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=1e6+5;
int b[maxn];
int f[maxn];//以i为结尾的最长下降子序列
int main()
{
int t;cin>>t;
while(t--)
{
int n;cin>>n;
int tot=1;
scanf("%d",&b[1]);f[1]=1;
for(int i=2;i<=n;i++)
{
int num;scanf("%d",&num);
if(num<b[tot])
{
b[++tot]=num;
f[i]=tot;
}
else
{
int l=1,r=tot;
int pos;
while(l<=r)
{
int m=(l+r)/2;
if(b[m]<=num)
{
pos=m;
r=m-1;
}
else
{
l=m+1;
}
}
b[pos]=num;
f[i]=pos;
}
}
printf("%d\n",tot);
for(int i=1;i<=n;i++)
{
printf("%d ",f[i]);
}
printf("\n");
}
}