Codeforces Round 901 (Div
C. Jellyfish and Green Apple
题解
显然\(n \% m =0\),答案一定为\(0\)
如果\(n > m\),我们显然可以将\(n / m\)的苹果分给每个人,然后再处理$n % m $
如果\(n < m\),我们一定会将所有苹果一直对半切直到\(n > m\),所以答案每次对半切一定会加\(n\)
直到\(n \%m = 0\)的时候结束
那么什么情况下无解呢?
我们考虑每次对半切会使得\(n :=n * 2\),也就是说如果一个质因子\(p,p\neq 2\)在\(m\)中存在,但是在\(n\)中不存在,那么\(n\)不管怎么翻倍,永远不会出现\(n \%m = 0\)
那么这个该如何判断呢?
我们可以通过\(\frac{m}{gcd(n,m)}=2^i\)来判断
int n, m;
int lowbit(int x) { return x & -x; }
void solve()
{
cin >> n >> m;
if (n % m == 0)
{
cout << 0 << endl;
return;
}
n = n % m;
int r = m / gcd(n, m);
if (r != lowbit(r))
{
cout << -1 << endl;
return;
}
int ans = 0;
while (true)
{
while (n * 2 < m)
{
ans += n;
n <<= 1;
}
ans += n;
n <<= 1;
if (n % m == 0)
break;
n = n % m;
}
cout << ans << endl;
}
D. Jellyfish and Mex
\(1 \leq n \leq 5000\)
题解:\(DP\)
我们考虑\(dp\)
显然对于\(a_i > mex\)部分我们可以不用管
如果当前数组\(a\)的\(mex\)为\(g\),我们想使得\(x,x < g\)成为\(mex\),那么代价为\(g \times (cnt[x] - 1) + x\)
我们定义状态为:\(dp[i]\):代表通过删除操作使得\(mex = i\)的最小代价
转移方程为:
\[dp[mex] = 0 \\
dp[i] = min \sum_{j = i + 1}^{mex}(dp[j] + j \times (cnt[i] - 1) + i)
\]倒序遍历\(i\)即可
- 显然答案为\(dp[0]\)
int n, a[N];
// dp[i] 代表通过删除操作使得 mex = i 的最小代价
void solve()
{
cin >> n;
map<int, int> mp;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
mp[a[i]]++;
}
int mex = -1;
for (int i = 0;; ++i)
if (!mp.count(i))
{
mex = i;
break;
}
vector<int> dp(mex + 10, INF);
dp[mex] = 0;
for (int i = mex - 1; i >= 0; --i)
for (int j = i + 1; j <= mex; ++j)
dp[i] = min(dp[i], dp[j] + j * (mp[i] - 1) + i);
cout << dp[0] << endl;
}
Codeforces Round 901 (Div的更多相关文章
- 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 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
随机推荐
- tracking调研
常用框架有以下三种: Separate Detection and Embedding (SDE- 物体检测,特征提取与物体关联),JOINT Detection and Embeddin ...
- WebRTC 简单入门与实践
一.前言 WebRTC 技术已经广泛在各个行业及场景中被应用,但对多数开发者来说,实时音视频及相关技术却是比较不常接触到的. 做为一名 Web 开发者,WebRTC 这块的概念着实花了不少时间才搞明白 ...
- 704 二分查找 golang实现
二分查找(Binary Search)是一种高效的查找算法,适用于 有序数组 或 有序列表.它的基本思想是通过将搜索范围逐渐缩小到目标元素所在的一半,从而大大减少查找的次数. 二分查找的基本原理 排序 ...
- CSS & JS Effect – sticky horizontal scrollbar
需求 这个是 Google Ads 里的 table. 那个 horizontal scrollbar 可以 sticky bottom. 我们知道 scrollbar 是游览器原生的,我们能做的 s ...
- Figma 学习笔记 – Image
参考: Figma Tutorial: Images 3 Ways to Insert Image 1. rectangle / frame + fill 画一个 rectangle / frame ...
- 第22天:安全开发-PHP应用&留言板功能&超全局变量&数据库操作&第三方插件引用
#数据库操作-mysqli函数&增删改查 PHP函数:连接,选择,执行,结果,关闭等 参考:https://www.runoob.com/php/php-ref-mysqli.html 常用: ...
- Hive 2.3.2安装
一.安装mysql 安装MySQL服务器端和MySQL客户端: •安装: – yum install mysql – yum install mysql-server •启动: – /etc/init ...
- Linux内核虚拟内存管理之匿名映射缺页异常分析
今天我们就来讨论下这种缺页异常,让大家彻底理解它.注:本文使用linux-5.0内核源代码.文章分为以下几节内容: 匿名映射缺页异常的触发情况 0页是什么?为什么使用0页? 源代码分析 3.1 触发条 ...
- 105份墨天轮“国产化迁移”干货文档汇总(含TiDB、openGauss、上云等)
当前国产数据库产品百花齐放,随着政策的推进.技术的迭代以及市场需求的逐步扩大,数据库国产化正在加速进行中,有越来越多的金融.通信.制造.互联网等企业机构以及政府机关单位将业务系统从Oracle.MyS ...
- 别人可以访问本项目的ip地址
.markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...

