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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

題意:

給你n個(gè)數(shù)的序列,當(dāng)滿足i<ji<ji<j andandand ai>aja_i>a_jai?>aj?時(shí),這兩個(gè)點(diǎn)之間有一條邊,現(xiàn)在對(duì)點(diǎn)染色,要求每個(gè)點(diǎn)相鄰的點(diǎ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(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

分析:

這道題,在賽中時(shí),剛開始考慮是直接求每個(gè)元素的逆序?qū)?#xff08;用樹狀數(shù)組),然后該點(diǎn)顏色為逆序?qū)?shù)+1,交了wa了第一遍;第二遍我們舉出來了一個(gè)數(shù)據(jù)是 1 5 2 3 4,結(jié)果是 1 4 1 1 1,顏色數(shù)為4,肯定不對(duì),所以進(jìn)行了離散化,交上去又wa了,此時(shí)走入了瓶頸,舉了很多數(shù)據(jù)都是對(duì)的,耽誤了很多時(shí)間,最后舉了一個(gè)例子,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,故此,我們思路出問題了。討論過后很短的時(shí)間,就決定用線段樹維護(hù)區(qū)間逆序?qū)︻伾畲笾?#xff0c;每次query得到最大值+1即可。這里面有幾個(gè)需要注意的點(diǎn):

  • 序列從后往前遍歷,當(dāng)我們對(duì)當(dāng)前區(qū)間查找最大值時(shí),就是當(dāng)前點(diǎn)逆序?qū)Φ淖畲笾?#xff0c;因?yàn)槟承╇m然比當(dāng)前點(diǎn)小,但不為逆序?qū)Φ闹?#xff0c;一定在序列的后面,此時(shí)該點(diǎn)并沒有賦值,所以不需考慮。
  • 區(qū)間更新時(shí),因?yàn)樯偌恿藄eg[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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。