AtCoder Beginner Contest 120 题解
题目链接:https://atcoder.jp/contests/abc120
A Favorite Sound
分析:答案为
。
代码:
#include <iostream> using namespace std; int main()
{
int a, b, c;
cin>>a>>b>>c;
cout<<min(b / a, c)<<endl;
return ;
}
B K-th Common Divisor
分析:A、B范围很小,枚举即可。注意要判断A、B的大小,从大到小枚举。
代码:
#include <cstdio>
#include <iostream> using namespace std; int main()
{
int a, b, k;
scanf("%d %d %d", &a, &b, &k);
int imax = max(a, b);
int cnt = ;
for(int i = imax; i >= ; --i)
{
if(a % i == && b % i == )
cnt++;
if(cnt == k)
{
printf("%d\n", i);
break;
}
}
return ;
}
C Unification
分析:因为S串里只有‘0’和‘1’,所以无论如何只要还有‘0’和‘1’同时存在,就一定可以继续该操作。答案就是‘0’和‘1’的数量取最小值的两倍(一次操作两个cube)。
代码:
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int main()
{
char s[];
scanf("%s", s);
int slen = strlen(s);
int zero = , one = ;
for(int i = ; i < slen; ++i)
{
if(s[i] == '')
zero++;
else if(s[i] == '')
one++;
}
printf("%d\n", min(zero, one)*);
return ;
}
D Decayed Bridges
分析:建边后删边判断连通比较麻烦。我们可以考虑倒着建边。当所有边都删完后,
。然后倒序加入每一条边,inconvenience减去加入一条边后连通的点对。判断是否在同一个连通集里可以用并查集维护。这里需要开一个数组s记录每一个集合内的点个数,初始所有点为1.之后更新的时候只要更新并查集里的根节点个数即可。每加一条边
。
代码:
#include <iostream>
#include <cstdio> using namespace std; typedef long long ll; struct bridge
{
int a;
int b;
}dict[]; int parent[], trank[];
ll sum;
ll ans[] = {};
ll s[]; void init()
{
for(int i = ; i < ; ++i)
{
parent[i] = -;
trank[i] = ;
}
} int find_root(int x)
{
int x_root = x;
while(parent[x_root] != -)
x_root = parent[x_root];
return x_root;
} int union_set(int x, int y)
{
int x_root = find_root(x);
int y_root = find_root(y);
if(x_root == y_root)
return ;
else
{
if(trank[x_root] > trank[y_root])
{
parent[y_root] = x_root;
sum -= s[x_root]*s[y_root];
s[x_root] += s[y_root];
}
else if(trank[y_root] > trank[x_root])
{
parent[x_root] = y_root;
sum -= s[x_root]*s[y_root];
s[y_root] += s[x_root];
}
else
{
parent[y_root] = x_root;
trank[x_root]++;
sum -= s[x_root]*s[y_root];
s[x_root] += s[y_root];
}
}
return ;
} int main()
{
init();
int n, m;
scanf("%d %d", &n, &m);
for(int i = ; i < m; ++i)
scanf("%d %d", &dict[i].a, &dict[i].b);
for(int i = ; i <= n; ++i)
s[i] = ;
sum = 1ll*n*(n-)/;
for(int i = m - ; i >= ; --i)
{
ans[i] = sum;
int x = dict[i].a;
int y = dict[i].b;
union_set(x, y);
}
for(int i = ; i < m; ++i)
cout<<ans[i]<<endl;
return ;
}
AtCoder Beginner Contest 120 题解的更多相关文章
- AtCoder Beginner Contest 154 题解
人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...
- AtCoder Beginner Contest 153 题解
目录 AtCoder Beginner Contest 153 题解 A - Serval vs Monster 题意 做法 程序 B - Common Raccoon vs Monster 题意 做 ...
- AtCoder Beginner Contest 177 题解
AtCoder Beginner Contest 177 题解 目录 AtCoder Beginner Contest 177 题解 A - Don't be late B - Substring C ...
- AtCoder Beginner Contest 184 题解
AtCoder Beginner Contest 184 题解 目录 AtCoder Beginner Contest 184 题解 A - Determinant B - Quizzes C - S ...
- AtCoder Beginner Contest 173 题解
AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...
- AtCoder Beginner Contest 172 题解
AtCoder Beginner Contest 172 题解 目录 AtCoder Beginner Contest 172 题解 A - Calc B - Minor Change C - Tsu ...
- AtCoder Beginner Contest 169 题解
AtCoder Beginner Contest 169 题解 这场比赛比较简单,证明我没有咕咕咕的时候到了! A - Multiplication 1 没什么好说的,直接读入两个数输出乘积就好了. ...
- AtCoder Beginner Contest 148 题解
目录 AtCoder Beginner Contest 148 题解 前言 A - Round One 题意 做法 程序 B - Strings with the Same Length 题意 做法 ...
- AtCoder Beginner Contest 151 题解报告
总的来说,这次的题目比较水,然而菜菜的我并没有把所有题目都做完,话不多说,直接来干货: A:Next Alphabet 题目链接:https://atcoder.jp/contests/abc151/ ...
随机推荐
- findwindow\sendmessage向第三方软件发送消息演示
这里仅仅是以putty作为演示消息发送机制和控件搜索机制 程序一:代填IP和端口,并建立远程连接 #include"stdafx.h"#include <windows.h& ...
- JavaScript组合模式---引入
首先:使用一个例子来引入组合模式,需求为(1)有一个学校有2个班(一班,二班)(2)每个班级分2个小组(一班一组,一班二组,二班一组,二班二组)(3)学校计算机教室有限,每一个小组分着来上课 然后:根 ...
- python 三元表达式、列表推导式、生成器表达式
一 三元表达式.列表推导式.生成器表达式 一 三元表达式 name=input('姓名>>: ') res='mm' if name == 'hahah' else 'NB' print( ...
- 【264】◀▶ Windows 批处理(CMD)
参考:Windows Commands 微软官方帮助 参考:DOS命令自学小窍门:巧用help命令 参考:bat批处理的注释语句 打开文件夹: start D:\abc 打开D盘abc文件夹 打开ex ...
- struts-hibernate整合(1)配置环境
①加载jar包 创建类库: 在myeclipse中点击windows---Preference---Java---Build Path---User Libraries---new 输入创建类库名字s ...
- Android中同一个ImageView中根据状态显示不同图片
一般: if(条件1) { image.setBackground(R.id.xxx1); } else if (条件2) { image.setBackground(R.id.xxx2); } 实际 ...
- @SuppressWarnings("unused")注解的作用
JDK5.0后的新特性,你在使用IDE如eclipse的时候,当你定义了一个变量如int a=0;但是你后面根本就没有使用到这个变量,这一行的前面会有一个黄色的警告标志,你将鼠标移动到上面会提示“这个 ...
- session详解&和cookie的区别
session简介 1. 定义 session用来保存会话数据, 将数据保存到服务器中. 2. 作用 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),一个浏览器独占一 ...
- IDEA创建MAVEN 无骨架WEB 项目
Idea创建maven带有骨架的web项目的时候,会缺少必要文件夹,而且会多出来一些我们不需要的东西 详见:IDEA创建Maven Web项目 所以我们也可以创建无骨架项目: 创建maven项目 不选 ...
- 提示crontab command not found的解决方法
操作步骤 1. 确认crontab是否安装: 执行 crontab 命令如果报 command not found,就表明没有安装 2. 安装 crontab 执行 yum insta ...