【agc001d】Arrays and Palindrome
Description
给你一个\(m\)个数的排列\(A\),这个\(A\)中元素的顺序可以随便调换,\(A\)中的元素的和为\(n\),现在要你构造一个数组\(B\)(长度为\(m1\)),满足:如果一个字符串同时满足:
1、头\(A_1\)个字符,接下来的\(A_2\)个字符,接下来的\(A_3\)个字符...接下来的\(A_m\)个字符分别构成回文串
2、头\(B_1\)个字符,接下来的\(B_2\)个字符,接下来的\(B_3\)个字符...接下来的\(B_{m1}\)个字符分别构成回文串
那么这个字符串中每一个位置上的字符都一样
Solution
日常不会构造题。。。qwq
有一个比较好的想法就是。。画蚊香。。大概是这样:

这个图对应的是\(A=\{3,2\}\)\(B=\{4,1\}\)
然后我们要做的就是。。使得连完线之后可以一笔画
然后发现如果说出现一个长度为奇数的回文串,最中间的那个点就会没有线连,然后为了让它和其他的点连上,这个点的度数必须是\(1\),然后为了保证一笔画,这样的点必须至多出现两个,所以奇数长度的回文串至多只能有两个,否则就无解了,然后多画几组会发现。。如果出现奇数长度的回文串它们还必须出现在一头一尾qwq
那么剩下的就是构造啦
当全部都是偶数的时候,我们在最开头先放一个\(1\),这样后面就错开了,然后第\(1\)到\(m-1\)都可以直接复制下来,至于最后一个回文串,我们可以放一堆\(2\)中间夹一个\(1\)这样(具体自己画一下就知道了)
当有一个奇数的时候,我们把它放在\(A\)的最后,\(B\)的前面部分的构造方式同上,最后一个长度为奇数的回文串就简单一些,直接全部上\(2\)就好了
当有两个奇数的时候,我们将其放在一头一尾,然后\(B\)的第一个元素设成\(A_1+1\),这样后面的情况就和只有一个奇数、并且已经放了一个\(1\)的情况一样了,剩下的构造同上
代码大概长这个样子
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1e5+10;
int a[N],b[N*2],lis[3];
int n,m,cnt;
bool firstcheck(){
int cnt=0;
for (int i=1;i<=n;++i)
cnt+=a[i]&1;
return cnt<=2;
}
void get_b(){
int sum;
if (cnt==0){
b[++b[0]]=1; sum=m-1;
for (int i=1;i<n;++i) b[++b[0]]=a[i],sum-=a[i];
if (sum==0) return;
sum-=1; sum/=2;
for (int i=1;i<=sum/2;++i) b[++b[0]]=2;
b[++b[0]]=1;
for (int i=sum/2+1;i<=sum;++i) b[++b[0]]=2;
}
else if (cnt==1){
b[++b[0]]=1;
for (int i=1;i<n;++i) b[++b[0]]=a[i];
for (int i=1;i<=(a[n]-1)/2;++i) b[++b[0]]=2;
}
else{
b[++b[0]]=a[1]+1;
for (int i=2;i<n;++i) b[++b[0]]=a[i];
for (int i=1;i<=(a[n]-1)/2;++i) b[++b[0]]=2;
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
#endif
scanf("%d%d",&m,&n);
for (int i=1;i<=n;++i) scanf("%d",a+i);
if (!firstcheck()){printf("Impossible\n"); return 0;}
cnt=0;
for (int i=1;i<=n;++i){
if ((a[i]&1))
lis[++cnt]=i;
}
if (lis[1]) swap(a[n],a[lis[1]]);
if (lis[2]) swap(a[1],a[lis[2]]);
for (int i=1;i<=n;++i) printf("%d ",a[i]); printf("\n");
get_b();
printf("%d\n",b[0]);
for (int i=1;i<=b[0];++i) printf("%d ",b[i]); printf("\n");
}
【agc001d】Arrays and Palindrome的更多相关文章
- 【NOIP2017提高A组模拟9.12】Arrays and Palindrome
[NOIP2017提高A组模拟9.12]Arrays and Palindrome[SPJ] 题目 Description Input Output Sample Input 1 6 Sample O ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【LeetCode】680. Valid Palindrome II
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...
- 【HDOJ6599】I Love Palindrome String(PAM,manacher)
题意:给出一个由小写字母组成的长为n的字符串S,定义他的子串[L,R]为周驿东串当且仅当[L,R]为回文串且[L,(L+R)/2]为回文串 求i=[1,n] 所有长度为i的周驿东串的个数 n<= ...
- 【原】Arrays.binarySearch() 的用法
Arrays.binarySearch() 的用法 1.binarySearch(Object[] a, Object key) Searches the specified array for th ...
- 【LeetCode】125. Valid Palindrome
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- 【easy】479. Largest Palindrome Product
Find the largest palindrome made from the product of two n-digit numbers Since the result could be v ...
- 【leetcode】409. Longest Palindrome
problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...
- 【LeetCode】214. Shortest Palindrome
Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...
随机推荐
- [shell] 一次性赋值多个变量
管道符是fork子进程,子进程的变量无法传回父进程 [root@XM-v106 ~]# echo "1 2 3" | read a b c;echo $a [root@XM-v10 ...
- IDEA2017.3.4破解方式及lombok图文配置详解
下载jetbrainsCrack-2.7-release-str.jar包 下载地址: https://files.cnblogs.com/files/xifenglou/JetBrains.zip ...
- spring study
Dependency Injection The Inversion of Control(IoC) is a general concept, and it can be expressed in ...
- IntelliJ IDEA 自动编译功能无法使用,On 'update' action:选项里面没有update classes and resources这项
https://zhidao.baidu.com/question/1381265197230335740.html
- java 第一次实验报告
北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1353 姓名:黎静 学号:20135338 成绩: 指导教师:娄嘉鹏 ...
- 20162328蔡文琛 2017week03
20162328 2017-2018-1 <程序设计与数据结构>第3周学习总结 教材学习内容总结 查找是在一组项内找到指定目标或是确定目标不存在的过程. 搞笑的查找使得比较的次数最少. C ...
- Java 面试 --- 3
上一篇,我们给出了大概35个题目,都是基础知识,有童鞋反映题目过时了,其实不然,这些是基础中的基础,但是也是必不可少的,面试题目中还是有一些基础题目的,我们本着先易后难的原则,逐渐给出不同级别的题目, ...
- ASP.NET中实现封装与策略模式
首先把运算方法封装起来,这样在网页界面中直接就可以调用了,不过是换张脸而已! using System; using System.Collections.Generic; using System. ...
- HDU 4126 Genghis Khan the Conqueror 最小生成树+树形dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4126 Genghis Khan the Conqueror Time Limit: 10000/50 ...
- BNUOJ 52318 Be Friends prim+Trie
题目链接: https://acm.bnu.edu.cn/v3/problem_show.php?pid=52318 B. Be Friends Case Time Limit: 2500msMemo ...