Portal -->agc001D

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的更多相关文章

  1. 【NOIP2017提高A组模拟9.12】Arrays and Palindrome

    [NOIP2017提高A组模拟9.12]Arrays and Palindrome[SPJ] 题目 Description Input Output Sample Input 1 6 Sample O ...

  2. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  3. 【LeetCode】680. Valid Palindrome II

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...

  4. 【HDOJ6599】I Love Palindrome String(PAM,manacher)

    题意:给出一个由小写字母组成的长为n的字符串S,定义他的子串[L,R]为周驿东串当且仅当[L,R]为回文串且[L,(L+R)/2]为回文串 求i=[1,n] 所有长度为i的周驿东串的个数 n<= ...

  5. 【原】Arrays.binarySearch() 的用法

    Arrays.binarySearch() 的用法 1.binarySearch(Object[] a, Object key) Searches the specified array for th ...

  6. 【LeetCode】125. Valid Palindrome

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  7. 【easy】479. Largest Palindrome Product

    Find the largest palindrome made from the product of two n-digit numbers Since the result could be v ...

  8. 【leetcode】409. Longest Palindrome

    problem 409. Longest Palindrome solution1: class Solution { public: int longestPalindrome(string s) ...

  9. 【LeetCode】214. Shortest Palindrome

    Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...

随机推荐

  1. MyCat安装与测试教程 超详细!

    MyCat安装与测试教程 超详细! MyCat基础知识 一.什么是MYCAT? 1. 一个彻底开源的,面向企业应用开发的大数据库集群 2. 支持事务.ACID.可以替代MySQL的加强版数据库 3. ...

  2. python—启动自带shell时报错(丢失api-ms-win-crt-runtime-l1-1-0.dll)已解决

    备注: 有的伙伴安装完1后重启,问题可以解决,summer儿在安装完1依然未能解决,于是又进行了2的安装再次重启后问题解决!! 1,安装vc-redist.x64,微软官网搜索免费下载,安装后重启. ...

  3. VMware启动Centos时出现错误Cannot open the disk 'xxxxxxx.vmdk' or one of the snapshot disks it depends on. .

    今天拔装虚拟机的硬盘的时候,没有关掉虚拟机,导致虚拟打开的时候出现:Cannot open the disk 'xxxxxxx.vmdk' or one of the snapshot disks i ...

  4. git实践笔记

    title: git实践笔记 date: 2016-10-15 18:40:26 tags: [Git] categories: [Tool,Git] --- 概述 本文记录常用 git 的功能和命令 ...

  5. WinForm中从SQLite数据库获取数据显示到DataGridView

    1.关于Sqlite Sqlite是一款开源的.适合在客户端和嵌入式设备中使用的轻量级数据库,支持标准的SQL. 不像SqlServer或Oracle的引擎是一个独立的进程.通过TCP或命名管道等与程 ...

  6. SecureCRT SSH连接一直提示密码错误

    这是解决方法:  http://www.linuxidc.com/Linux/2016-09/134925.htm

  7. java 事务

    之前的事务介绍基本都是数据库层面的事务,本文来介绍一下J2EE中和事务相关的内容,在阅读本文之前,希望读者对分布式有一定的了解. 关于事务的基础知识这里不再详细介绍,想要了解的同学可以在我的博客中阅读 ...

  8. WPF四则运算《《《《《策略模式

    设计思路:           因为之前没有用过WPF,听说和window窗体语法类似,就想着仿照之前的Window窗体做的,首先用三个textbox存储数据,添加一个comboBox,利用索引选择运 ...

  9. c++课的圆周面积

    又回顾了一下一两个月没动过的类,似乎又有点手生了,不过还好还可以做. 在栋哥的推荐下下载了一个vs2015,表示从dev的白鼠形式的简单操作缓过来还有些不习惯呢,不过有些功能,例如诊断还是挺好用的 这 ...

  10. Android笔记-4-实现登陆页面并跳转和简单的注册页面

    实现登陆页面并跳转和简单的注册页面   首先我们来看看布局的xml代码 login.xml <span style="font-family:Arial;font-size:18px; ...