Necklace - CF613C
Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic sequence of beads of different colors. Ivan says that necklace is beautiful relative to the cut point between two adjacent beads, if the chain of beads remaining after this cut is a palindrome (reads the same forward and backward).
Ivan has beads of n colors. He wants to make a necklace, such that it's beautiful relative to as many cuts as possible. He certainly wants to use all the beads. Help him to make the most beautiful necklace.
The first line of the input contains a single number n (1 ≤ n ≤ 26) — the number of colors of beads. The second line contains after n positive integers ai — the quantity of beads of i-th color. It is guaranteed that the sum of ai is at least 2 and does not exceed 100 000.
In the first line print a single number — the maximum number of beautiful cuts that a necklace composed from given beads may have. In the second line print any example of such necklace.
Each color of the beads should be represented by the corresponding lowercase English letter (starting with a). As the necklace is cyclic, print it starting from any point.
3
4 2 1
1
abacaba
1
4
4
aaaa
2
1 1
0
ab
In the first sample a necklace can have at most one beautiful cut. The example of such a necklace is shown on the picture.
In the second sample there is only one way to compose a necklace.
简单题意
给你很多个珠子(第i种颜色有Ai种,颜色最多有26种,用小写字母表示),让你串成一条项链,然后项链有一个优美值
优美值=优美的cut的个数
一个优美的cut表示从这个地方剪断项链,使得其变成一个回文串
然后你要求出最大的优美值,然后给出一个方案
胡说题解
分情况讨论
1.如果个数中没有奇数,那么答案就是所有数字的gcd,然后构造答案就是输出gcd/2个回文串
2.如果个数中只有一个奇数,那么答案也是所有数字的gcd,然后构造答案就是输出gcd个回文串,个数为奇数的颜色放在回文串的中间
3.如果个数中有两个或以上的奇数,那么答案就是0,因为两个奇数就已经构造不出有优美cut的环来了
#include<cstdio>
using namespace std; int n,c,x,a[]; int gcd(int a,int b){
if(b==)return a;
return gcd(b,a % b);
} int main(){
scanf("%d",&n);
int i,j,k;
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=n;i++)
if((a[i]&)==)c++,x=i;
if(c>){
printf("0\n");
for(i=;i<=n;i++){
while(a[i]>){
--a[i];
printf("%c",'a'-+i);
}
}
}
else
if(c==){
c=a[];
for(i=;i<=n;i++)c=gcd(c,a[i]);
printf("%d\n",c);
for(i=;i<=c;i++){
for(j=;j<=n;j++){
if(j!=x)
for(k=;k<=a[j]/c/;k++)printf("%c",'a'-+j);
}
for(j=;j<=a[x]/c;j++)printf("%c",'a'-+x);
for(j=n;j>;j--){
if(j!=x)
for(k=;k<=a[j]/c/;k++)printf("%c",'a'-+j);
}
}
}
else{
c=a[];
for(i=;i<=n;i++)c=gcd(c,a[i]);
printf("%d\n",c);
for(i=;i<=c/;i++){
for(j=;j<=n;j++){
for(k=;k<=a[j]/c;k++)printf("%c",'a'-+j);
}
for(j=n;j>;j--){
for(k=;k<=a[j]/c;k++)printf("%c",'a'-+j);
}
}
}
return ;
}
AC代码
Necklace - CF613C的更多相关文章
- HDU5730 Shell Necklace(DP + CDQ分治 + FFT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...
- 2016 Multi-University Training Contest 1 H.Shell Necklace
Shell Necklace Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- USACO section1.1 Broken Necklace
/* ID: vincent63 LANG: C TASK: beads */ #include <stdio.h> #include<stdlib.h> #include&l ...
- [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链
[BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链 试题描述 欢乐岛上众多新奇的游乐项目让小可可他们玩的非常开心.现在他们正在玩比赛串项链的游戏,谁串的最快就能得到 ...
- POJ 1286 Necklace of Beads(Polya原理)
Description Beads of red, blue or green colors are connected together into a circular necklace of n ...
- Accepted Necklace
Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 2660 Accepted Necklace
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...
随机推荐
- java操作HDFS
package com.lei.hadoop; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Fil ...
- ES2015学习笔记
ECMA6学习笔记 参考资料 ECMAScript6入门:http://es6.ruanyifeng.com/ 官方文档:https://babeljs.io/learn-es2015/ 开发软件:W ...
- Windows运行机理——创建窗口
Windows运行机理这系列文章都是来至于<零基础学Qt4编程>——吴迪,个人觉得写得很好,所以进行了搬运和个人加工 Windows 窗口在创建之前,其属性必须设定好,所谓属性包括类的名字 ...
- Linearize an sRGB texture in Photoshop
From:https://forum.unity.com/threads/bug-with-bypass-srgb-sampling.282469/
- SpriteKit在复制节点时留了一个巨坑给开发者,需要开发者手动把复制节点的isPaused设置为false
根据When an overlay node with actions is copied there is currently a SpriteKit bug where the node’s is ...
- vim python自动补全插件:pydiction
vim python自动补全插件:pydiction 可以实现下面python代码的自动补全: 1.简单python关键词补全 2.python 函数补全带括号 3.python 模块补全 4.pyt ...
- JVM--内存模型与线程
一.硬件与效率的一致性 计算机的存储设备与处理器的运算速度存在几个数量级的差距,现在计算机系统不得不在内存和处理器之间增加一层高速缓存(cache)来作为缓冲.将运算需要的数据复制到缓存中,让运算能够 ...
- [Clr via C#读书笔记]Cp16数组
Cp16数组 一维数组,多维数组,交错数组:引用类型:P338的图非常的清楚地描述了值类型和引用类型在托管堆中的关系:越界检查: 数组初始化 数组初始化器: 四种写法 string[] names = ...
- mvc中actionresult的返回值类型
以前一直没注意actionresult都能返回哪些类型的类型值(一直用的公司的内部工具类初始化进行返回的),今天跟大家分享一下(也是转载的别人的日志qaq). 首先我们了解一下对action的要求: ...
- 幸运的袋子(深度优先遍历(Depth First Search,DFS))
题目描述 一个袋子里面有n个球,每个球上面都有一个号码(拥有相同号码的球是无区别的).如果一个袋子是幸运的当且仅当所有球的号码的和大于所有球的号码的积. 例如:如果袋子里面的球的号码是{1, 1, 2 ...