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)的更多相关文章

  1. 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 ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  9. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

随机推荐

  1. MySQL——MySQL多实例

    一台服务器上运行多个MySQL服务,不同的业务 多实例思路: 1. 多套配置文件 2. 多套数据 3. 多个socket 4. 多个端口 5. 多个日志文件 6. 多个启动程序

  2. Python__Xpath模块

    import requests from lxml import etree # 导入xpath headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 6 ...

  3. 前端路由原理之 hash 模式和 history 模式

    什么是路由? 个人理解路由就是浏览器 URL 和页面内容的一种映射关系. 比如你看到我这篇博客,博客的链接是一个 URL,而 URL 对应的就是我这篇博客的网页内容,这二者之间的映射关系就是路由. 其 ...

  4. 20210716考试-NOIP19

    u,v,w. 这场考过. T1 u 差分裸题 #include<bits/stdc++.h> using namespace std; const int N=5000; int n,m; ...

  5. Flask(3)- Flask 中的 HTTP 方法

    查看 app.route() 源代码 def route(self, rule: str, **options: t.Any) -> t.Callable: """ ...

  6. Docker Compose 实践及梳理

    Docker Compose 可以实现 Docker 容器集群的编排,可以通过 docker-compose.yml 文件,定义我们的服务及其需要的依赖,轻松地运行在测试.生产等环境 文档 Produ ...

  7. Math.round真的是四舍五入吗?我不这么认为

    public static long round(double a) 返回最接近参数的 long.结果将舍入为整数:加上 1/2,对结果调用 floor 并将所得结果强制转换为 long 类型.换句话 ...

  8. Jmeter系类(32) - JSR223(2) | Groovy常见内置函数及调用

    常见内置函数及调用 获取相关函数 获取返回数据并转换为String字符串 prev.getResponseDataAsString() 例子 String Responsedata = prev.ge ...

  9. django错误处理

    1.django.db.utils.OperationalError: no such table 意思:没有这个app应用对应的数据表的,可以用 python manage.py makemigra ...

  10. LR11自带网站

    LR自带的飞机订票系统 启动服务:安装路径\HP\LoadRunner\WebTours下的StartServer.bat 打开网页:地址  http://127.0.0.1:1080/WebTour ...