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 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
随机推荐
- noip模拟题7
目录 T1:匹配 T2:回家 思路 上代码: T3:寿司 基本思路: 上代码: T1:匹配 ##思路: 首先,这道题既可以用KMP,也可以用hash 先说KMP,首先要注意的一点是:KMP的n ...
- C# Equals方法和==有什么区别
开发工具:VS2019 一.关于这两个比较,需要从值类型和引用类型两方面来说 (A)先说值类型 上图: 因为在对值类型进行比较时候,不管 .Equals() 方法还是 == 方法,都是对值类型变量(图 ...
- GoLang设计模式06 - 对象池模式
这次介绍最后一个创建型模式--对象池模式.顾名思义,对象池模式就是预先初始化创建好多个对象,并将之保存在一个池子里.当需要的时候,客户端就可以从池子里申请一个对象使用,使用完以后再将之放回到池子里.池 ...
- 口护万亿市场杀出的实力派 Oclean欧可林
撰文 |懂懂 编辑 | 秦言 来源:懂懂笔记 在"青年必去的电影节"上,发现了一个跟他们打成一片的智能护齿"新星". 25日,备受关注的第15届FIRST青年电 ...
- 一文搞懂Python Unittest测试方法执行顺序
大家好~我是米洛! 欢迎关注我的公众号测试开发坑货,一起交流!点赞收藏关注,不迷路. Unittest unittest大家应该都不陌生.它作为一款博主在5-6年前最常用的单元测试框架,现在正被pyt ...
- 网站URL如何SEO优化
前言 本文讲解网站的URL如何进行SEO优化,并在自己的WordPress博客echeverra中优化URL. 起因 对于SEO我了解的并不多,只知道SEO做的好,那么各大搜索网站搜索你网站相关内容时 ...
- scrum项目冲刺_day11 第一阶段总结
"智能垃圾分类APP"第一阶段总结 总任务: 一.appUI页面(已完成) 二.首页功能: 1.图像识别功能(已完成) 2.语音识别功能(已完成) 3.垃圾搜索功能(基本完成) 4 ...
- OpenGL渲染管道,Shader,VAO&VBO&EBO
OpenGL渲染管线 (也就是)OpenGL渲染一帧图形的流程 以下列举最简单的,渲染一个三角形的流程,你可以将它视为 精简版OpenGL渲染管线 更复杂的流程也仅仅就是:在此基础上的各个流程中 添加 ...
- Dapr实战(一) 基础概念与环境搭建
什么是Dapr Dapr 是一个可移植的.事件驱动的运行时,可运行在云平台或边缘计算中.支持多种编程语言和开发框架. 上面是官方对Dapr的介绍.有点难以理解,大白话可以理解为:Dapr是一个运行时, ...
- Java基础系列(10)- 类型转换
类型转换 由于Java是强类型语言,所以要进行有些运算的时候,需要用到类型转换.运算中,不同类型的数据先转换为同一类型,然后进行运算. 低 ------------------------------ ...