线段树维护区间最大值+第 45 届(ICPC)亚洲区域赛(昆明)L题Simone and Graph Coloring
題意:
給你n個數(shù)的序列,當(dāng)滿足i<ji<ji<j andandand ai>aja_i>a_jai?>aj?時,這兩個點之間有一條邊,現(xiàn)在對點染色,要求每個點相鄰的點顏色不同,問如何染色使得不同顏色數(shù)量最小。
題目:
鏈接:https://ac.nowcoder.com/acm/contest/17137/L
來源:牛客網(wǎng)
Simone, a student of Graph Coloring University, is interested in permutation. Now she is given a permutation of length nn, 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 andandand ai>aja_i>a_jai?>aj?, then there will be an undirected edge between node i and node j 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 nn integers a1,a2,...,ana_1,a_2,...,a_na1?,a2?,...,an? , indicating the permutation.
It is guaranteed that the sum of nn over all test cases does not exceed 10610^6106 .
輸出描述:
For each test case, the first line contains an integer cc, the chromatic number(the minimal number of colors been used when coloring) of the graph.
The second line contains nn 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.
示例1
輸入
2
4
1 3 4 2
2
1 2
輸出
2
1 1 1 2
1
1 1
分析:
這道題,在賽中時,剛開始考慮是直接求每個元素的逆序?qū)?#xff08;用樹狀數(shù)組),然后該點顏色為逆序?qū)?shù)+1,交了wa了第一遍;第二遍我們舉出來了一個數(shù)據(jù)是 1 5 2 3 4,結(jié)果是 1 4 1 1 1,顏色數(shù)為4,肯定不對,所以進(jìn)行了離散化,交上去又wa了,此時走入了瓶頸,舉了很多數(shù)據(jù)都是對的,耽誤了很多時間,最后舉了一個例子,1 2 3 4 5 8 6 9 7,按著思路應(yīng)該是1 1 1 1 1 3 1 2 1,但有更優(yōu)解 1 1 1 1 1 2 1 2 1,故此,我們思路出問題了。討論過后很短的時間,就決定用線段樹維護(hù)區(qū)間逆序?qū)︻伾畲笾?#xff0c;每次query得到最大值+1即可。這里面有幾個需要注意的點:
- 序列從后往前遍歷,當(dāng)我們對當(dāng)前區(qū)間查找最大值時,就是當(dāng)前點逆序?qū)Φ淖畲笾?#xff0c;因為某些雖然比當(dāng)前點小,但不為逆序?qū)Φ闹?#xff0c;一定在序列的后面,此時該點并沒有賦值,所以不需考慮。
- 區(qū)間更新時,因為少加了seg[u]=max(seg[u<<1],seg[u<<1|1]);,編譯結(jié)果出現(xiàn)問題,就像前面說的,我們需要的是區(qū)間最大值,直接套用模板就行,不需要考慮逆序?qū)χ惖摹W詈竺看胃骂伾畲笾?#xff0c;輸出即可,orz%%%%%%一道簽到題搞芥末久,果然還是菜哈。
AC代碼:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e6+10; int n; int a[N],b[N]; int seg[N<<2]; void upd(int l,int t,int u,int L,int R){if(L==l && R==l){seg[u]=t;return;}int md=(L+R)>>1;if(l<=md) upd(l,t,u<<1,L,md);if(l>md) upd(l,t,u<<1|1,md+1,R);seg[u]=max(seg[u<<1],seg[u<<1|1]); } int quy(int l,int r,int u,int L,int R){if(l<=L && r>=R){return seg[u];}int md=(L+R)>>1;int tp=0;if(l<=md) tp=max(tp,quy(l,r,u<<1,L,md));if(r>md) tp=max(tp,quy(l,r,u<<1|1,md+1,R));return tp; }int main() {int T;scanf("%d",&T);while(T--){for(int i=1; i<4*n+100; ++i) seg[i]=0;scanf("%d",&n);for(int i=1; i<=n; ++i){scanf("%d",&a[i]);}int ma=0;for(int i=n; i>=1; --i){b[i]=quy(1,a[i],1,1,n)+1;upd(a[i],b[i],1,1,n);ma=max(ma,b[i]);}printf("%d\n",ma);for(int i=1; i<=n; ++i){printf("%d%c",b[i],(i==n?'\n':' '));}}return 0; }總結(jié)
以上是生活随笔為你收集整理的线段树维护区间最大值+第 45 届(ICPC)亚洲区域赛(昆明)L题Simone and Graph Coloring的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 减肥能吃油炸花生米吗
- 下一篇: 字符串相关