Codeforces Round #748 (Div. 3)
Codeforces Round #748 (Div. 3)
A. Elections
思路分析:
- 令当前值比最大值大即可,如果最大值是它自己,就输出\(0\)
代码
#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[3];
int ans[3];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
for (int i = 0; i < 3; i++)
cin >> a[i].first, a[i].second = i;
sort(a, a + 3);
for (int i = 0; i < 3; i++)
{
if (i == 0)
{
ans[a[i].second] = a[2].first - a[0].first + 1;
}
else if (i == 1)
{
ans[a[i].second] = a[2].first - a[1].first + 1;
}
else
{
if (a[2].first == a[1].first)
{
ans[a[i].second] = 1;
}
else
ans[a[i].second] = 0;
}
}
for (int i = 0; i < 3; i++)
{
cout << ans[i] << ' ';
}
cout << endl;
}
return 0;
}
B. Make it Divisible by 25
思路分析:
- 一开始以为和上次make power of 2一样,然后发现不对劲,想到了结论,25是5的倍数,那么我们剩下的数最后一位必定为5或者0,所以我们一直删到最后一位为5或者0为止。
- 然后如果为25的倍数的话,末尾两位必定是25,50,75,00,所以我们就分情况取最小值即可。
- 其实官方题解的最简单,我就建议不要看我这个做法了。
代码
#include <bits/stdc++.h>
using namespace std;
int a[2] = {2, 7};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
string s;
cin >> s;
string c = s;
int mincnt = 0;
//以5为最后一位
for (int i = s.size() - 1; i >= 0; i--)
{
if (s[i] != '5')
{
s.erase(i);
mincnt++;
}
//找到5
else
break;
}
for (int i = s.size() - 2; i >= 0; i--)
{
if (s[i] != '2' && s[i] != '7')
{
mincnt++;
s.erase(i);
}
else
break;
}
int temp = 0;
for (int i = c.size() - 1; i >= 0; i--)
{
if (c[i] != '0')
{
c.erase(i);
temp++;
}
//找到5
else
break;
}
for (int i = c.size() - 2; i >= 0; i--)
{
if (c[i] != '5' && c[i] != '0')
{
temp++;
c.erase(i);
}
else
break;
}
cout << min(mincnt, temp) << endl;
}
return 0;
}
C. Save More Mice
思路分析:
- 这题其实就是贪心吧,让离洞最近的老鼠先逃即可,因为老鼠和猫的相对速度为\(1\)。
- 注意更新猫的位置即可。
代码
#include <bits/stdc++.h>
using namespace std;
vector<int> a;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
a.clear();
int n, k;
cin >> n >> k;
for (int i = 0; i < k; i++)
{
int x;
cin >> x;
a.push_back(x);
}
sort(a.begin(), a.end());
int ans = 0;
int x = 0;
for (int i = a.size() - 1; i >= 0; i--)
{
if (x < a[i])
{
x += (n - a[i]);
ans++;
}
else
break;
}
cout << ans << endl;
}
return 0;
}
D1. All are Same
思路分析:
- 因为最后要把所有数变为一个数,那么肯定是把所有数变成当前数组的最小值即可。
- 那么如何求这个减去的数的最大值呢?
- 实际上也就是所有数和最小值的GCD了。
代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a.begin(), a.end());
int x = a[0];
int ans = 0;
for (int i = 0; i < n; i++)
{
ans = __gcd(ans, a[i] - x);
}
if (ans != 0)
cout << ans << endl;
else
{
cout << "-1" << endl;
}
}
return 0;
}
D2. Half of Same
思路分析:
- 我们可以知道当且仅当当前数组中已经有\(n / 2\)个数相同输出\(-1\)。
- 我们枚举每一个数,也就是说把其他数变为它需要减去多少值保存一下,然后记录原数组中已经有多少个数与它相同,再因数分解一下这个差值,然后遍历它的差值的因数,取使得因数的个数加上原本和它一样的数的个数大于\(n / 2\)的最大的因数即可。
代码
#include <bits/stdc++.h>
using namespace std;
set<int> divide(int x)
{
set<int> q;
for (int i = 1; i * i <= x; i++)
{
if (x % i == 0)
{
q.insert(i);
q.insert(x / i);
}
}
return q;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
int k = -1;
for (int i = 0; i < a.size(); i++)
{
int minv = a[i];
int same = 0;
vector<int> d;
for (int j = 0; j < a.size(); j++)
{
if (a[j] == minv)
same++;
else if (a[j] > minv)
{
d.push_back(a[j] - minv);
}
}
if (same >= n / 2)
{
k = INT_MAX;
break;
}
map<int, int> mp;
for (auto x : d)
{
for (auto xx : divide(x))
{
mp[xx]++;
}
}
for (auto x : mp)
{
if (x.second + same >= n / 2)
{
k = max(k, x.first);
}
}
}
cout << (k == INT_MAX ? -1 : k) << endl;
}
return 0;
}
E. Gardener and Tree
思路分析:
- 一开始以为暴力能过就写了个暴力然后T了。然后看了下题解,原来是反向的,也就是说我们也是模拟一下删点过程(把这棵树删完),然后统计一下是在第几轮删去的这个点,最后把删去轮数和\(k\)比较即可。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + 10;
int degree[maxn];
vector<int> e[maxn];
int n, k, ans;
int vis[maxn];
int dis[maxn];
int main()
{
int t;
cin >> t;
while (t--)
{
memset(vis, 0, sizeof(vis));
memset(degree, 0, sizeof(degree));
memset(dis, 0, sizeof(dis));
cin >> n >> k;
ans = n;
for (int i = 1; i <= n; i++)
{
e[i].clear();
}
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
degree[u] += 1;
degree[v] += 1;
}
queue<int> q;
for (int i = 1; i <= n; i++)
{
if (degree[i] == 1)
{
q.push(i);
vis[i] = 1;
dis[i] = 1;
}
}
while (!q.empty())
{
int x = q.front();
q.pop();
for (int j = 0; j < e[x].size(); j++)
{
int u = e[x][j];
if (vis[u])
continue;
degree[u]--;
if (degree[u] == 1)
{
q.push(u);
vis[u] = 1;
dis[u] = dis[x] + 1;
}
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
{
if (dis[i] > k)
{
ans++;
}
}
cout << ans << endl;
}
return 0;
}
Codeforces Round #748 (Div. 3)的更多相关文章
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
随机推荐
- .NET 5 支持 Azure Functions OpenAPI 扩展啦
今年5月,在 Build大会上,Azure FunctionsOpenAPI的功能支持(预览版)正式宣布. 当时,它最高支持 v3 运行时--.NET Core 3.1 版本. 最近,它发布了 .NE ...
- ELK数据迁移,ES快照备份迁移
通过curl命令或者kibana快照备份,恢复的方式进行数据迁移 环境介绍 之前创建的ELK 因为VPC环境的问题,需要对ELK从新部署,但是还需要保留现有的数据,于是便有了这篇文档. 10.0.20 ...
- ---Docker学习随笔---基础管理部分---
docker是什么?提供快速.高效.轻量的微服务平台 1. 背景介绍突破虚拟机对资源占用高.启动时间长.镜像存储大.集群规模小等限制,摆脱操作系统级的隔离级别,实现进程级管理.主要专用名词: chro ...
- MySQL版本浅介
一.关于MySQL发行版介绍: 1. MySQL官方发行版 MySQL是最流行的数据库,主要特点: 简单:MySQL使用很简单,可以无师自通地参照文档安装运行和使用MySQL,几乎没有什么门槛. 开源 ...
- Flask(4)- URL 组成部分详解
URL Uniform Resource Locator 的简写,中文名叫统一资源定位符 用于表示服务端的各种资源,例如网页 下面将讲解 Flask 中如何提取组成 URL 的各个部分 URL 组 ...
- 【死磕NIO】— NIO基础详解
Netty 是基于Java NIO 封装的网络通讯框架,只有充分理解了 Java NIO 才能理解好Netty的底层设计.Java NIO 由三个核心组件组件: Buffer Channel Sele ...
- ipsec.conf配置文件多个保护子网解析流程
Author : Email : vip_13031075266@163.com Date : 2021.01.23 Copyright : 未经同意不得 ...
- IIS中配置WCF站点
http://msdn.microsoft.com/zh-cn/library/aa751852.aspx http://blog.csdn.net/hsg77/article/details/389 ...
- Maven专题2——聚合与继承
聚合 聚合模块的<packaging>元素为pom 聚合模块通过<modules>元素标识自己的子模块,每个子模块对应了一个module元素 module元素中指定的是子模块所 ...
- Apache Dolphin Scheduler - Docker Compose 详解
Apache DolphinScheduler 是一个分布式去中心化,易扩展的可视化 DAG 工作流任务调度系统.简称 DS,包括 Web 及若干服务,它依赖 PostgreSQL 和 Zookeep ...