【CQ18阶梯赛第二场】题解
【A-H国的身份证号码I】
用N个for语句可以搞定,但是写起来不方便,所以搜索。
dfs(w,num,p)表示搜索完前w位,前面x组成的数位num,最后以为为p。
如果搜索到第N位,则表示num满足条件。
最后把所有满足条件的a[]排序后输出。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<memory>
using namespace std;
long long a[],n,k,cnt;
void dfs(int w,long long num,int p)
{
if(w==n) {
a[++cnt]=num;return ;
}
for(int i=;i<=k;i++){
if(p*i<=k)
dfs(w+,num*+i,i);
}
}int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=k;i++){
dfs(,i,i);
}
sort(a+,a+cnt+);
for(int i=;i<=cnt;i++)
printf("%d\n",a[i]);
return ;
}
【B-偶像的条件】
三个班级,肯定是每个班级选的要接近才行,所以先排序,然后枚举最矮的,然后找第二,接着找第三。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=;
int a[][maxn];
int ans=,t1,t2,t3,tmp;
void _do(int x,int y){//x最小,y第二
for(int i=;i<=a[x][];i++) {
t1=a[x][i];
int pos=lower_bound(a[y]+,a[y]++a[y][],t1)-a[y];
t2=a[y][pos];
pos=lower_bound(a[-x-y]+,a[-x-y]++a[-x-y][],t2)-a[-x-y];
t3=a[-x-y][pos];
if(t1!=&&t2!=&&t3!=) {
tmp=*(t3-t1);
if(tmp<ans) ans=tmp;
}
}
}
int main()
{
int n,m,q,i,j,L;
scanf("%d%d%d",&n,&m,&L);
a[][]=n;a[][]=m;a[][]=L;
for(i=;i<=n;i++) scanf("%d",&a[][i]);
for(i=;i<=m;i++) scanf("%d",&a[][i]);
for(i=;i<=L;i++) scanf("%d",&a[][i]);
sort(a[]+,a[]++n);
sort(a[]+,a[]++m);
sort(a[]+,a[]++L);
_do(,);
_do(,);
_do(,);
_do(,);
_do(,);
_do(,);
printf("%d\n",ans);
}
【C-数组去重】
从前往后,如果一个数不和前面的一样,则输出。
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
int main()
{
int n,pre,t,num;
while(scanf("%d",&n)){
if(n==-) return ;
num=;
pre=-;
while(n--){
scanf("%d",&t);
if(t!=pre) {
if(num) printf(" ");
printf("%d",t);
num++;
}
pre=t;
}
printf("\n");
}
return ;
}
【D-分数调查】
并查集,我们先设所有人的成绩未知,如果两个人的相对成绩知道,则把二人分到同一个集合。
所以如果两个人在同一个集合,那么就可以他们的相对成绩。
相反,如果两个人不在同一个集合,那么他们的相对成绩是错误的。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<memory>
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],To[maxn],V[maxn],cnt=;
int fa[maxn],scr[maxn];//fa是分组,score代表相对分数。
void _add(int u,int v,int s){
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
V[cnt]=s;
}
void _dfs(int u){
for(int i=Laxt[u];i;i=Next[i]){
if(!fa[To[i]]){
fa[To[i]]=fa[u];
scr[To[i]]=scr[u]-V[i];
_dfs(To[i]);
}
}
}
int main()
{
int n,m,q,i,x,y,s;
scanf("%d%d%d",&n,&m,&q);
for(i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&s);
_add(x,y,s);
_add(y,x,-s);
}
for(i=;i<=n;i++)
if(!fa[i]){
fa[i]=i;
_dfs(i);
}
for(i=;i<=q;i++){
scanf("%d%d",&x,&y);
if(fa[x]!=fa[y]) printf("-1\n");
else printf("%d\n",scr[x]-scr[y]);
}
return ;
}
【CQ18阶梯赛第二场】题解的更多相关文章
- CQ18阶梯赛第二场
H国的身份证号码I HihoCoder - 1558 只要单纯的判断一下前后的乘积就好了, 因为不是很想处理倍数的关系, 所以我这里是用 string去处理. 代码: #include<bits ...
- 【CQ18阶梯赛第一场】题解
[A-风格不统一如何写程序] 输入字符串,得到长度,对于每个字符:如果是大写,则改为:‘_’+小写:如果是‘_’则忽略‘_’,并且把后面的小写改为大写. #include<cstdio> ...
- Contest1592 - 2018-2019赛季多校联合新生训练赛第二场(部分题解)
Contest1592 - 2018-2019赛季多校联合新生训练赛第二场 D 10248 修建高楼(模拟优化) H 10252 组装玩具(贪心+二分) D 传送门 题干 题目描述 C 市有一条东西走 ...
- NOI.AC NOIP模拟赛 第二场 补记
NOI.AC NOIP模拟赛 第二场 补记 palindrome 题目大意: 同[CEOI2017]Palindromic Partitions string 同[TC11326]Impossible ...
- 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)
以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...
- 【CQ18阶梯赛第8场】题解
[A:HDU2032 杨辉三角]: 简单的递推,或者是基础的DP: 但是只有杨润东一个人1A,整体准确率只有8/37,具体原因不详. 经验:提交前一定要试一下比较特殊的数据或者最大的数据.其次,为了保 ...
- SCNU省选校赛第二场B题题解
今晚的校赛又告一段落啦,终于"开斋"了! AC了两题,还算是满意的,英语还是硬伤. 来看题目吧! B. Array time limit per test 2 seconds me ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
- 2019牛客暑期多校第二场题解FH
F.Partition problem 传送门 题意:有2n个人,分两组,每组n个,要求sum(vij)最大值. 题解:n并不大我们可以枚举每个人是在1组还是2组爆搜. 代码: #include &l ...
随机推荐
- ORA-01940: cannot drop a user that is currently connected 问题解析
https://www.linuxidc.com/Linux/2012-12/76448.htm
- CF 2018 Battle of Brains GYM 102062 F
https://codeforces.com/gym/102062/attachments/download/8213/2018-battle-of-brains-en.pdf https://cod ...
- poj - 2186 Popular Cows && poj - 2553 The Bottom of a Graph (强连通)
http://poj.org/problem?id=2186 给定n头牛,m个关系,每个关系a,b表示a认为b是受欢迎的,但是不代表b认为a是受欢迎的,关系之间还有传递性,假如a->b,b-&g ...
- MySQL与MSSQL的一些语法差异(持续更新中)
分号不能少:分号不能少:分号不能少:重要的事情说3遍 Insert或者Update的数据包含反斜杠\的时候需要进行转义\\,例:insert into tablename(id,name) value ...
- nrf51822中app_button 的应用
Button Handler(按键处理程序) 按键处理程序是使用GPIOTE(GPIO Task and Event)的处理机制实现的,为了防止按键的抖动.在GPIOTE event(事件)处理程序中 ...
- Microsoft Dynamics CRM Server 2013软件安装要求
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveV9mMTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- Python——正則表達式(2)
本文译自官方文档:Regular Expression HOWTO 參考文章:Python--正則表達式(1) 全文下载 :Python正則表達式基础 ======================== ...
- Android--向SD卡读写数据
// 向SD卡写入数据 private void writeSDcard(String str) { try { // 推断是否存在SD卡 if (Environment.getExternalSto ...
- run as maven test报错解决办法
eclipse中使用maven插件的时候,运行run as maven build的时候报错 -Dmaven.multiModuleProjectDirectory system propery is ...
- c++学习笔记之基础---类内声明函数后在类外定义的一种方法
在C++的“类”中经常遇到这样的函数, 返回值类型名 类名::函数成员名(参数表){ 函数体.} 双冒号的作用 ::域名解析符!返回值类型名 类名::函数成员名(参数表) { 函数体. } 这个是在类 ...