codeforces 1328D(思维)
题意描述
The round carousel consists of n figures of animals. Figures are numbered from 1 to n in order of the carousel moving. Thus, after the n-th figure the figure with the number 1 follows. Each figure has its own type — the type of the animal corresponding to this figure (the horse, the tiger and so on). The type of animal of the i-th figure equals ti. You want to color each figure in one of the colors. You think that it’s boring if the carousel contains two different figures (with the distinct types of animals) going one right after another and colored in the same color.
Your task is to color the figures in such a way that the number of distinct colors used is the minimum possible and there are no figures of the different types going one right after another and colored in the same color. If you use exactly k distinct colors, then the colors of figures should be denoted with integers from 1 to k.
有n个动物组成一个环,要求给每个动物染色,要求相邻的不同种类的动物不能有相同的颜色,求最小用到的颜色数量
思路
如果只有一种动物的话,那么只需要一种颜色即可。 如果有两种动物,那么需要两种颜色。 如果有三种及以上的动物的话,就需要分类讨论:因为是环形,如果首尾动物种类不同但是颜色相同,那么首尾颜色需要改变,但是首尾颜色的改变可能会使答案序列不满足题意,所以我们可以找到种类相同的动物的位置,然后把左侧或者右侧的1全部变为2,2全部变成1,这样的话是不会影响相互关系的。如果不满足,那么只需要将尾部颜色变为3即可。
AC代码
#include<bits/stdc++.h>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define sz(x) x.size()
#define ins(x) inserter(x,x.begin())
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=4*1e5+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
int a[N],ans[N];
void solve(){
int n;cin>>n;
rep(i,0,n) cin>>a[i];
int flag=0,p=1,res=1;
ans[0]=1;
rep(i,1,n){
if(a[i]!=a[i-1]){
res=2;
p=3-p;
ans[i]=p;
}else{
flag=i;
ans[i]=p;
}
}
if(n>=3 && a[0]!=a[n-1] && ans[0]==ans[n-1]){
if(flag){
rep(i,flag,n) ans[i]=3-ans[i];
}else{
res=3;
ans[n-1]=3;
}
}
cout<<res<<endl;
rep(i,0,n) cout<<ans[i]<<' ';
cout<<endl;
}
int main(){
IOS;
int t;cin>>t;
while(t--){
solve();
}
return 0;
}
- Rafy 领域实体框架演示(4) - 使用本地文件型数据库 SQLCE 绿色部署
- spring 的OpenSessionInViewFilter简介
- Android TextView中文字通过SpannableString来设置超链接、颜色、字体等属性
- 【Java SE】Java NIO系列教程(三) Buffer
- android混淆
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](三)
- 两个activity或者activity和fragment传值
- 【强烈推荐】Java工程师如何从一名普通的码农成长为一位大神
- Remoting: Server encountered an internal error
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](四)
- 在mono 3.0 下运行ASP.NET 4网站的主意事项
- 《干货系列》SQL语句-知无不言言无不尽
- OutOfMemoryError异常系列之方法区溢出和运行时常量溢出池溢出
- 代码转换工具 Code Converter 2013
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- RTSP流媒体协议视频平台EasyNVR和EasyNTS智能云组网同一浏览器运行为什么会导致EasyNTS无法登陆?
- Java:手写线程安全LRU缓存X探究影响命中率的因素
- 视频上云/网络穿透/网络映射服务EasyNTS设备管理为什么会出现无法搜索到设备的情况?
- 快速打造属于你的接口自动化测试框架
- 大数据下的质量体系建设
- PostgreSQL 日志系统 及 设置错误导致磁盘塞满案例
- 六、乘胜追击,将剩下的Git知识点搞定
- 树莓派基础实验39:解析无线电接收机PWM、SBUS信号
- nodejs源码分析第十九章 -- udp模块
- Spark Extracting,transforming,selecting features
- 逆向so文件调试工具ida基础知识点
- 二叉搜索树中的众数
- 了解递归:普通函数递归和非递归栈式实现之间的区别
- 字节真题 ZJ26-异或:使用字典树代替暴力破解降低时间复杂度
- curl命令半天没响应,有可能返回内容导致session挂了