codefoces B - Phoenix and Beauty
原题链接:https://codeforc.es/problemset/problem/1348/B
题意:告诉我们一个数组及其长度和k,判断是否可以构造一个新数组使得每K段长度和都相等。
思路:首先需要判断原数组不同元素的个数与k的大小,如果看k小于原数组的不同元素的个数那么无法构造这样的数组;如果k大于等于那我们就可以构造出一个新数组。我们定义一个vis[101]数组作为标记数组,记录不同元素的个数,来判断是否存在这样的数组。然后我们定义一个数组b[]构造新数组的一个周期数组。最后只要输出n个这样的周期数组即可。
坑点:一开始觉得很难 是以为新数组的长度要最短,然后反复看了几遍才发现(英语不好的菜鸡加上读题不仔细)。然后别忘记对标记数组的初始化,因为这个也wr了一次。
Ac代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod1=1e9+7;
const ll mod2=0x3f3f3f3f;
const ll maxn=1e4+5;
int a[maxn];//原数组;
int b[maxn];//新数组;
int vis[101];//标记数组;
int main(){
int t,n,k;
cin>>t;
while(t--){
cin>>n>>k;
for(int i=0;i<101;i++) vis[i]=0;//标记数组初始化;
for(int i=0;i<n;i++){cin>>a[i];vis[a[i]]++;}
int cnt=0;
for(int i=1;i<101;++i) {if(vis[i]) cnt++;}//求不同元素的个数;
if(cnt>k) {puts("-1");continue;}
else if(cnt<k){
for(int i=1;i<=100;i++){
if(!vis[i]){
vis[i]=1;cnt++;
if(cnt==k) break;//以k为周期;
}
}
}
int x=0;
for(int i=1;i<=100;i++){
if(vis[i]) b[++x]=i;//构造以k为周期的新数组;
}
cout<<n*k<<endl;//输出新数组的长度;
for(int i=0;i<n;i++){
for(int j=1;j<=k;j++){
cout<<b[j]<<" "; //输出新数组的元素;
}
}
cout<<endl;
}
return 0;
}
codefoces B - Phoenix and Beauty的更多相关文章
- B. Phoenix and Beauty(贪心构造)
\(给定序列长n的数组和k.完美数组的定义是数组中每一个连续k项的子段和为定值\) \(现在要求插入一些数使得数组满足条件,输出你构造的新数列.\) \(\color{Red}{----------- ...
- codefoces D. Phoenix and Science
原题链接:https://codeforc.es/problemset/problem/1348/D 题意:给你一个体重为一克的细菌(它可以每天进行一次二分裂即一分为二体重均分:晚上体重增加1克)求最 ...
- Phoenix综述(史上最全Phoenix中文文档)
个人主页:http://www.linbingdong.com 简书地址:http://www.jianshu.com/users/6cb45a00b49c/latest_articles 网上关于P ...
- 在DBeaver中phoenix查询报错:org.apache.phoenix.exception.PhoenixIOException: The system cannot find the path specified
环境:Phoenix:4.4,win7系统 问题:Phoenix在查询hbase时,报"系统找不到指定路径". 解决: 请参见 https://distcp.quora.com/C ...
- HBase+Phoenix整合入门--集群搭建
环境:CentOS 6.6 64位 hbase 1.1.15 phoenix-4.7.0-HBase-1.1 一.前置环境: 已经安装配置好Hadoop 2.6和jdk 1.7 二.安装hba ...
- SQL Server恢复软件 Stellar Phoenix sql recovery
SQL Server恢复软件 Stellar Phoenix sql recovery http://www.stellarinfo.com/ http://www.stellarinfo.com/ ...
- Hbase+ Phoenix搭建教程
Hbase+ Phoenix搭建教程 一.Hbase简介 HBase是基于列存储.构建在HDFS上的分布式存储系统,其主要功能是存储海量结构化数据. HBase构建在HDFS之上,因此HBase也是通 ...
- CDH5.4.5运行Phoenix导入CSV文件
1.安装phoenix 在界面上设置Phoenix的parcel包: http://52.11.56.155:7180/cmf/settings?groupKey=config.scm.parcel. ...
- Phoenix -修复表索引
索引的修复可以通过2种方式,(关于pehoenix的索引的生命周期可以参考 https://community.hortonworks.com/articles/58818/phoenix-inde ...
随机推荐
- vuepress & package.json lock version
vuepress & package.json lock version npm 锁版 bug npm lock version holy shit { "name": & ...
- MBP 2018
MBP 2018 touch pad MacBook Pro 如何调节键盘背光 https://support.apple.com/zh-cn/HT202310 F6 & F5 如何清洁 Ma ...
- jest all in one
jest all in one ES Modules & TypeScript & React https://github.com/xgqfrms/FEAT/tree/master/ ...
- GreenSock & SVG Animation
GreenSock & SVG Animation refs https://greensock.com/ https://greensock.com/learning/ GSAP https ...
- Learning CSS with Chrome DevTools
Learning CSS with Chrome DevTools CSS 复合属性展开 border background box-shadow flex-flow flex HTML5 custo ...
- 密码 & 安全
密码 & 安全 拖库/脱库 如何在数据库中存储密码更安全? https://time.geekbang.org/dailylesson/detail/100044031 拖库和撞库 https ...
- svg rect to polygon points & points order bug
svg rect to polygon points & points order bug https://codepen.io/xgqfrms/pen/vYOWjYr?editors=100 ...
- c++ 设置桌面壁纸(win)
#include <iostream> #include <Windows.h> int main() { const char* path = "C:\\Users ...
- 解决ROS及Fast-RTPS安装和使用中raw.githubusercontent.com无法连接的问题
资料参考: https://blog.csdn.net/weixin_44692299/article/details/105869229
- SpringBoot整合Mybatis 使用generator自动生成实体类代码、Mapper代码、dao层代码
1.新建一个SpringBoot项目,并引入Mybatis和mybatis-generator相关的依赖. <dependency> <groupId>org.springfr ...