日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

线段树维护区间最大值+第 45 届(ICPC)亚洲区域赛(昆明)L题Simone and Graph Coloring

發布時間:2023/12/4 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线段树维护区间最大值+第 45 届(ICPC)亚洲区域赛(昆明)L题Simone and Graph Coloring 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:

給你n個數的序列,當滿足i<ji<ji<j andandand ai>aja_i>a_jai?>aj?時,這兩個點之間有一條邊,現在對點染色,要求每個點相鄰的點顏色不同,問如何染色使得不同顏色數量最小。

題目:

鏈接:https://ac.nowcoder.com/acm/contest/17137/L
來源:??途W

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(1T106) , 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(1n106), 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 c1ci?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

分析:

這道題,在賽中時,剛開始考慮是直接求每個元素的逆序對(用樹狀數組),然后該點顏色為逆序對數+1,交了wa了第一遍;第二遍我們舉出來了一個數據是 1 5 2 3 4,結果是 1 4 1 1 1,顏色數為4,肯定不對,所以進行了離散化,交上去又wa了,此時走入了瓶頸,舉了很多數據都是對的,耽誤了很多時間,最后舉了一個例子,1 2 3 4 5 8 6 9 7,按著思路應該是1 1 1 1 1 3 1 2 1,但有更優解 1 1 1 1 1 2 1 2 1,故此,我們思路出問題了。討論過后很短的時間,就決定用線段樹維護區間逆序對顏色最大值,每次query得到最大值+1即可。這里面有幾個需要注意的點:

  • 序列從后往前遍歷,當我們對當前區間查找最大值時,就是當前點逆序對的最大值,因為某些雖然比當前點小,但不為逆序對的值,一定在序列的后面,此時該點并沒有賦值,所以不需考慮。
  • 區間更新時,因為少加了seg[u]=max(seg[u<<1],seg[u<<1|1]);,編譯結果出現問題,就像前面說的,我們需要的是區間最大值,直接套用模板就行,不需要考慮逆序對之類的。最后每次更新顏色最大值,輸出即可,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; }

總結

以上是生活随笔為你收集整理的线段树维护区间最大值+第 45 届(ICPC)亚洲区域赛(昆明)L题Simone and Graph Coloring的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。