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 ...
随机推荐
- 北京Uber优步司机奖励政策(1月14日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 北京Uber优步司机奖励政策(1月1日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- [python3.x]win 7 下Pyinstaller库的安装与使用---探索之路
一.下载安装包 cmd进入命令行,执行命令:pip install https://github.com/pyinstaller/pyinstaller/archive/develop.tar.gz ...
- Emmet 技巧
1. Lorem 产生一段 dummy text 2. $ 变量的使用 3. 插入img的长度和宽度 使用快捷键ctrl+u插入图片的长度和宽度 注意光标要停留在图片文件名上. 其他在Sublime中 ...
- steam更新出错 应用运行中
游戏程序没有完全关闭,仍在后台运行. 打开任务处理器,选择进程,下面找到TslGame,关闭之.
- 第五模块:WEB开发基础 第3章·BootStrap&JQuery开发
01-JQuery介绍 02-jQuery文件引入和加载的区别 03-jQuery的基础选择器 04-jQuery的层级选择器 05-jQuery的基本过滤选择器 06-jQuery的属性选择器 07 ...
- TPO-12 C2 A problem of the TA's payroll
TPO-12 C2 A problem of the TA's payroll payroll n. 工资单:在册职工人数:工资名单: paycheck n. 付薪水的支票,薪水 paperwork ...
- Unity Lighting - Choosing a Rendering Path 选择渲染路径(三)
Choosing a Rendering Path 选择渲染路径 Unity supports a number of rendering techniques, or ‘paths’. An i ...
- 204. Singleton
Description Singleton is a most widely used design pattern. If a class has and only has one instance ...
- 洪水!(Flooded! ACM/ICPC World Final 1999,UVa815)
题目描述:竞赛入门经典的习题4-10 解题思路:1.把各个网格想象成一个数组 2.排序 3.雨水总体积去铺满 //太懒了只写了求海拔 #include <stdio.h> #define ...