题目大意:
  有$n(n\leq10^6)$种颜色,第$i$种颜色有$c_i(\sum c_i\leq10^6)$个,指定第一个颜色为$a$,最后一个颜色为$b$,问对于一个长度为$m=\sum c_i$的序列,是否能构造出一个染色方案满足相邻的颜色不相同。如果能,试构造出一种方案。

思路:
  贪心。如果序列中有多个元素,开头结尾颜色相同而这种颜色只有一个,则显然不存在合法方案。每次选取当前数量最多的不同于前一个颜色的颜色,如果有一样多的,就尽量取和最后一个位置的颜色相同的颜色。如果没有可以取的元素或者倒数第二个元素颜色只能选和最后一个元素相同的颜色就说明不存在合法方案。洛谷和BZOJ随便AC,然而SZKOpuł上怎么卡常都是T。

正常代码:

 #include<queue>
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=1e6+;
int a[N],c[N];
std::priority_queue<std::pair<int,int> > q;
int main() {
const int n=getint();
a[]=getint(),a[]=getint();
int m=;
for(register int i=;i<=n;i++) {
m+=c[i]=getint();
}
std::swap(a[],a[m]);
c[a[]]--;
if(m!=) c[a[m]]--;
if(c[a[]]<) {
putchar('');
return ;
}
for(register int i=;i<=n;i++) {
if(c[i]) q.push(std::make_pair(c[i],i==a[m]?i+n:i));
}
for(register int i=;i<m;i++) {
int x=q.top().second%n;q.pop();
if(!x) x+=n;
if(x==a[i-]) {
if(q.empty()) {
putchar('');
return ;
}
int y=q.top().second%n;q.pop();
if(!y) y+=n;
q.push(std::make_pair(c[x],x==a[m]?x+n:x));
a[i]=y;
if(--c[y]) q.push(std::make_pair(c[y],y==a[m]?y+n:y));
} else {
a[i]=x;
if(--c[x]) q.push(std::make_pair(c[x],x==a[m]?x+n:x));
}
}
if(a[m-]==a[m]) {
putchar('');
return ;
}
for(register int i=;i<=m;i++) {
printf("%d%c",a[i]," \n"[i==m]);
}
return ;
}

卡常代码:

 #pragma GCC optimize(3)
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<cstdio>
#include<climits>
#include<algorithm>
#include<sys/mman.h>
#include<sys/stat.h>
typedef long long int64;
class BufferedInputStream {
private:
char *buf,*p;
int size;
public:
BufferedInputStream() {
register int fd=fileno(stdin);
struct stat sb;
fstat(fd,&sb);
size=sb.st_size;
p=buf=reinterpret_cast<char*>(mmap(,size,PROT_READ,MAP_PRIVATE,fileno(stdin),));
}
char getchar() {
return (p==buf+size||*p==EOF)?EOF:*p++;
}
};
BufferedInputStream in;
inline int getint() {
register char ch;
while(!__builtin_isdigit(ch=in.getchar()));
register int x=ch^'';
while(__builtin_isdigit(ch=in.getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=1e6+;
int a[N],c[N];
class priority_queue {
#define _left <<1
#define _right <<1|1
#define _par >>1
private:
int size;
int64 val[N];
public:
priority_queue() {
val[]=LLONG_MAX;
}
void push(const int64 &x) {
register int p=++size;
for(;val[p _par]<x;p=p _par) {
val[p]=val[p _par];
}
val[p]=x;
}
int64 top() const {
return val[];
}
void pop() {
register int p=;
const int64 x=val[size--];
for(;p _left<=size&&(x<val[p _left]||x<val[p _right]);p=val[p _left]>val[p _right]?p _left:p _right) {
val[p]=std::max(val[p _left],val[p _right]);
}
val[p]=x;
}
bool empty() const {
return !size;
}
#undef _left
#undef _right
#undef _par
};
inline int64 make_pair(const int &x,const int &y) {
return (int64)x<<|y;
}
inline int second(const int &x) {
return x&0x7fffffff;
}
priority_queue q;
int main() {
const int n=getint();
a[]=getint(),a[]=getint();
int m=;
for(register int i=;i<=n;i++) {
m+=c[i]=getint();
}
std::swap(a[],a[m]);
c[a[]]--;
if(__builtin_expect(m!=,)) c[a[m]]--;
if(__builtin_expect(c[a[]]<,)) {
__builtin_puts("");
__builtin_exit();
}
for(register int i=;i<=n;i++) {
if(__builtin_expect(c[i],)) q.push(make_pair(c[i],i==a[m]?i+n:i));
}
for(register int i=;i<m;i++) {
int x=second(q.top());q.pop();
if(__builtin_expect(x>n,)) x-=n;
if(__builtin_expect(x==a[i-],)) {
if(__builtin_expect(q.empty(),)) {
__builtin_puts("");
__builtin_exit();
}
int y=second(q.top());q.pop();
if(__builtin_expect(y>n,)) y-=n;
q.push(make_pair(c[x],x==a[m]?x+n:x));
if(__builtin_expect(--c[a[i]=y],)) q.push(make_pair(c[y],y==a[m]?y+n:y));
} else {
if(__builtin_expect(--c[a[i]=x],)) q.push(make_pair(c[x],x==a[m]?x+n:x));
}
}
if(__builtin_expect(a[m-]==a[m],)) {
__builtin_puts("");
__builtin_exit();
}
for(register int i=;i<=m;i++) {
__builtin_printf("%d%c",a[i]," \n"[i==m]);
}
__builtin_exit();
}

[POI2014]Beads的更多相关文章

  1. BZOJ 3524: [Poi2014]Couriers [主席树]

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1892  Solved: 683[Submit][St ...

  2. HDU 1817Necklace of Beads(置换+Polya计数)

    Necklace of Beads Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  3. BZOJ 3524: [Poi2014]Couriers

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1905  Solved: 691[Submit][St ...

  4. [BZOJ3872][Poi2014]Ant colony

    [BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...

  5. 【USACO】beads

    题目: You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, othe ...

  6. POJ 1286 Necklace of Beads(Polya原理)

    Description Beads of red, blue or green colors are connected together into a circular necklace of n ...

  7. 【BZOJ】【3522】【POI2014】Hotel

    暴力/树形DP 要求在树上找出等距三点,求方案数,那么用类似Free Tour2那样的合并方法,可以写出: f[i][j]表示以 i 为根的子树中,距离 i 为 j 的点有多少个: g[i][j]表示 ...

  8. 【BZOJ】【3831】【POI2014】Little Bird

    DP/单调队列优化 水题水题水题水题 单调队列优化的线性dp…… WA了8次QAQ,就因为我写队列是[l,r),但是实际操作取队尾元素的时候忘记了……不怎么从队尾取元素嘛……平时都是直接往进放的……还 ...

  9. Bzoj 3831 [Poi2014]Little Bird

    3831: [Poi2014]Little Bird Time Limit: 20 Sec Memory Limit: 128 MB Submit: 310 Solved: 186 [Submit][ ...

随机推荐

  1. 2.0 python+appium环境搭建

    Python下载地址:链接:https://pan.baidu.com/s/1Z3H8tw8AiBVwpxdcABC7XQ 密码:z66t Pycharm下载地址: 链接:https://pan.ba ...

  2. Mysql与Oracle之间的数据类型转换

    MySQL Data Type Oracle Data Type BIGINT NUMBER(19, 0) BIT RAW BLOB BLOB, RAW CHAR CHAR DATE DATE DAT ...

  3. java IO小结

    package 字符与字节转换; import java.io.*; public class char_byte { public static void main(String[] args) { ...

  4. 牛客网暑期ACM多校训练营(第一场):D-Two Graphs

    链接:D-Two Graphs 题意:给出图G1和G2,求G2的子图中和G1同构的个数. 题解:只有8个点,暴力枚举G2的点每个排列,让G1映射到G2中,求出同构个数a.同构的G2就是在G1有边的对应 ...

  5. Python全栈 MySQL 数据库 (索引、数据导入、导出)

    ParisGabriel              每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰    开局一张图     表字段重命名(change)   alter table 表名 ...

  6. virsh command

    http://www.cnblogs.com/chenjiahe/category/845519.html resize qemu-img resize win2k8r2.qcow2 40G conv ...

  7. 博客挪窝了 http://my.oschina.net/jrrx/blog

    1. 博客园很不错: 2. 由于各种原因,挪到了 http://my.oschina.net/jrrx/blog

  8. Python下安装protobuf

    1. 下载安装包 2. 解压缩 tar –xzvf protobuf-2.6.1.tar.gz 3. 安装protoc 在python中使用protocbuf需要Protocal Buffer 编译器 ...

  9. Oracle设置用户密码永不过期

    1.查看用户的profile是那个,一般是default: select username, profile from dba_users; 2.查看指定概要文件(如default)的密码有效期设置: ...

  10. poj 2240 Arbitrage (最短路径)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13800   Accepted: 5815 Descri ...