题意:

给你n个数的序列,当满足i<ji<ji<j andandand ai>aja_i>a_jai​>aj​时,这两个点之间有一条边,现在对点染色,要求每个点相邻的点颜色不同,问如何染色使得不同颜色数量最小。

题目:

链接:https://ac.nowcoder.com/acm/contest/17137/L
来源:牛客网

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

分析:

这道题,在赛中时,刚开始考虑是直接求每个元素的逆序对(用树状数组),然后该点颜色为逆序对数+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相关推荐

  1. Marvolo Gaunt's Ring CodeForces - 855B+线段树+维护区间最大值和最小值

    题目链接: Marvolo Gaunt's Ring CodeForces - 855B 题目大意: 给定一段序列:a1,a2,a3,--an, 给定三个数:p,q,r(注意数据范围,代码里ans=- ...

  2. 2021 ICPC 昆明(22-4-17) C L E | 第46届ICPC亚洲区域赛(昆明)

    ICPC 2021 昆明 传送门 补题计划 CLE, C - Cup of Water prob : 在0-V内随机取数灌满1升水的期望操作次数 idea1: 首先将题给的"在0-V内随机取 ...

  3. 第46届ICPC亚洲区域赛(沈阳)L-Perfect Matchings【dp,组合数学】

    正题 题目链接:https://ac.nowcoder.com/acm/contest/24346/L 题目大意 有一张2n2n2n个点的完全图,在上面删除一棵生成树,然后求这张图的完全匹配方案数. ...

  4. BZOJ-4811: [Ynoi2017]由乃的OJ (树链剖分 线段树维护区间操作值 好题)

    4811: [Ynoi2017]由乃的OJ Time Limit: 6 Sec  Memory Limit: 256 MB Submit: 366  Solved: 118 [Submit][Stat ...

  5. Codeforces Round #742 (Div. 2) E. Non-Decreasing Dilemma (线段树维护区间连续问题)

    题意: 操作1:把x位置的数字修改成y. 操作2:查询[l,r]之间不下降序列的个数. 题解: 线段树维护区间和问题 (这是套路,想不到只能说做题少别打我) . 用五个变量进行维护. sum区间总个数 ...

  6. Can you answer these queries V SPOJ - GSS5 (分类讨论+线段树维护区间最大子段和)

    recursion有一个整数序列a[n].现在recursion有m次询问,每次她想知道Max { A[i]+A[i+1]+...+A[j] ; x1 <= i <= y1 , x2 &l ...

  7. BZOJ1018 | SHOI2008-堵塞的交通traffic——线段树维护区间连通性+细节

    [题目描述] BZOJ1018 | SHOI2008-堵塞的交通traffic 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可 以被看成是一个2行C列 ...

  8. 2017年第42届ACM-ICPC亚洲区域赛青岛赛区(现场赛)

    比赛:第42届ACM-ICPC 亚洲区域赛青岛站 比赛地点:中国石油大学体育馆一楼 时间:2017-11-4 到 2017-11-5 正式比赛时间:2017-11-5 上午九点到下午两点 AC题目: ...

  9. E. Sign on Fence(整体二分 + 线段树维护区间最大连续 1 的个数)

    E. Sign on Fence 给定一个长度为nnn的数组aaa,1≤ai≤1091 \leq a_i \leq 10 ^ 91≤ai​≤109,有mmm次询问,每次给定l,r,kl, r, kl, ...

最新文章

  1. 如何用hover写出顺畅的动态效果
  2. 2009_01_15_星期三
  3. 认识 linux sysfs文件系统
  4. STM32F103C8T6学习笔记_时钟
  5. 神策数据《2022 中国企业数字化运营成熟度报告》发布
  6. 关于PostMessage后台发送组合键
  7. 百老汇原版音乐剧《摇滚学校》2月开启中国巡演
  8. 使用mockjs模拟数据
  9. 使用Hibernate加载或保存图像-MySQL
  10. PDF签名系列(1):PDF签名机制的漏洞分析
  11. 两个相同矩形脉冲卷积_两个矩形脉冲的卷积
  12. TCP 拥塞控制算法
  13. php磁力链播放源码,Bt种子转磁力链 PHP源码
  14. 传统蒙文字体_关于传统蒙古文网页的国际标准编码及字体处理技术
  15. Retinex算法--低照度图像增强
  16. 证件照缩小为20k大小
  17. linux命令行连接蓝牙键盘
  18. 正态分布的由来及推导
  19. 什么命令能查看服务器的型号,查看服务器型号的命令
  20. 微星GE62 NVIDIA960m 双系统ubuntu16.04 配置caffe-ssd

热门文章

  1. linux之lsof查看端口占用情况
  2. 求数组里面的最大值和最小值
  3. Android之RecyclerView 实现真正的Gallery效果
  4. 搜索引擎(lucene)
  5. python 最简单的实现适配器设计模式
  6. android auto answer,Incoming call auto answer in android 4.0.3
  7. 软件项目组织管理(二、三)项目管理与信息技术环境、项目管理过程组
  8. 北大保送、硕博连读!《西游记》红孩儿扮演者现成中科院博士!
  9. 当女朋友问你会不会出轨的时候,该怎么回答?
  10. 惊了!日本街头出现透明公厕,竟有人排队抢着上!