Codeforces Round #725 (Div. 3) 题解
文章目录
- A. Stone Game
- B. Friends and Candies
- C. Number of Pairs
- D. Another Problem About Dividing Numbers
- E. Funny Substrings
- F. Interesting Function
- G. Gift Set
#725-Div.3
A. Stone Game
先找到最大值最小值的位置,然后有三种选取(两边中走一边/两边都走一点)
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define maxn 105
int T, n;
int a[maxn];
bool vis[maxn];int main() {scanf( "%d", &T );while( T -- ) {scanf( "%d", &n );for( int i = 1;i <= n;i ++ )scanf( "%d", &a[i] );int minn = maxn, maxx = -maxn, pos_min, pos_max;for( int i = 1;i <= n;i ++ ) {if( a[i] < minn ) minn = a[i], pos_min = i; if( a[i] > maxx ) maxx = a[i], pos_max = i;}int l = min( pos_min, pos_max ), r = max( pos_min, pos_max );printf( "%d\n", min( r, min( n - l + 1, l + n - r + 1 ) ) );}return 0;
}
B. Friends and Candies
首先糖果要能被均分,然后肯定是糖果多的人分给别人
#include <cstdio>
#define maxn 200005
int T, n;
int a[maxn];int main() {scanf( "%d", &T );while( T -- ) {scanf( "%d", &n );int tot = 0;for( int i = 1;i <= n;i ++ ) {scanf( "%d", &a[i] );tot += a[i];}if( tot % n ) printf( "-1\n" );else {tot /= n;int ans = 0;for( int i = 1;i <= n;i ++ )if( a[i] > tot ) ans ++;printf( "%d\n", ans );}}return 0;
}
C. Number of Pairs
先排个序,用lower_bound/upper_bound
查询要求值域中的值
#include <cstdio>
#include <algorithm>
using namespace std;
#define maxn 200005
#define int long long
int T, n, l, r;
int a[maxn];signed main() {scanf( "%lld", &T );while( T -- ) {scanf( "%lld %lld %lld", &n, &l, &r );int ans = 0;for( int i = 1;i <= n;i ++ )scanf( "%lld", &a[i] );sort( a + 1, a + n + 1 );for( int i = 1;i < n;i ++ ) {if( a[i] > r ) continue;int L, R;R = upper_bound( a + i + 1, a + n + 1, r - a[i] ) - a - 1 - 1;L = lower_bound( a + i + 1, a + n + 1, l - a[i] ) - a - 1 - 1;ans += R - L;}printf( "%lld\n", ans );}return 0;
}
D. Another Problem About Dividing Numbers
特判k=1
的情况
最小的操作次数肯定是222,直接除成111,所以只需要考虑最大操作次数,在这之间的操作次数肯定都是可以达到的,显然
最大操作次数就是唯一质因数分解的幂次和
刚开始还担心根号带TTT跑不过(#.#)
#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
int T, a, b, k;int gcd( int x, int y ) {if( x < y ) swap( x, y );if( ! y ) return x;else return gcd( y, x % y );
}int main() {scanf( "%d", &T );while( T -- ) {scanf( "%d %d %d", &a, &b, &k );if( k == 1 ) {if( a == b ) printf( "No\n" );else if( a % b == 0 || b % a == 0 ) printf( "Yes\n" );else printf( "No\n" );continue;}int cnta = 0, Sqr = sqrt( a );for( int i = 2;i <= Sqr;i ++ )if( a % i == 0 )while( a % i == 0 ) a /= i, cnta ++;if( a != 1 ) cnta ++;int cntb = 0; Sqr = sqrt( b );for( int i = 2;i <= Sqr;i ++ )if( b % i == 0 )while( b % i == 0 ) b /= i, cntb ++;if( b != 1 ) cntb ++;if( k <= cnta + cntb ) printf( "Yes\n" );else printf( "No\n" );}return 0;
}
E. Funny Substrings
过分原始的暴力模拟不可取,疯狂自叠加字符串长度可以达到2502^{50}250级别,望而却步
事实上对于每一个字符串,我们只需要存下haha
的个数,长度,以及前后各三个字符即可
因为只有这特殊的六个字符才可能与新的字符串相加时产生新的haha
#include <map>
#include <cstdio>
#include <iostream>
using namespace std;
#define int long long
struct word {int len, cnt;string s;
};
map < string, word > mp;string GetHead( string s ) {if( s.size() < 3 ) return s;else return s.substr( 0, 3 );
}string GetTail( string s ) {if( s.size() < 3 ) return s;else return s.substr( s.size() - 3, 3 );
}int calc( string s, string p ) {int cnt = 0;for( int i = 0;i + p.size() <= s.size();i ++ )if( s.substr( i, p.size() ) == p ) cnt ++;return cnt;
}word merge( word a, word b ) {word New;New.len = a.len + b.len;New.cnt = a.cnt + b.cnt + calc( GetTail( a.s ) + GetHead( b.s ), "haha" );New.s = a.s + b.s;if( New.s.size() > 7 ) New.s = GetHead( a.s ) + "#" + GetTail( b.s );return New;
}signed main() {int T, n;string a, b, c, op, plus;scanf( "%lld", &T );while( T -- ) {mp.clear();scanf( "%lld", &n );int ans = 0;while( n -- ) {cin >> a >> op >> b;if( op == ":=" ) {word New;New.cnt = calc( b, "haha" );New.len = b.length();New.s = b;mp[a] = New;}else {cin >> plus >> c;mp[a] = merge( mp[b], mp[c] );}ans = mp[a].cnt;}printf( "%lld\n", ans );}return 0;
}
F. Interesting Function
差分一下
发现对于任何数位上的值,完整向前一位进111,变换次数都是101010
所以枚举数位,看前面进位次数乘个101010,再加上该数位的值即可
#include <cstdio>
#define int long long
int cnt[20];int solve( int x ) {int ans = 0;while( x ) {int r = x % 10; x /= 10;ans += x * 10 + r;}return ans;
}signed main() {int T, l, r;scanf( "%lld", &T );while( T -- ) {scanf( "%lld %lld", &l, &r );printf( "%lld\n", solve( r ) - solve( l ) );}return 0;
}
G. Gift Set
能分成nnn个组合,就蕴含n−1n-1n−1
所以考虑二分组合个数ttt,假设x>y,a>bx>y,a>bx>y,a>b
x,yx,yx,y都至少会减去t×bt\times bt×b个,剩下了t×(a−b)t\times(a-b)t×(a−b)就随机让x−t×b,y−t×bx-t\times b, y-t\times bx−t×b,y−t×b补上
用向下取整判断即可
#include <cstdio>
#include <iostream>
using namespace std;
int T, x, y, a, b;bool check( int t ) {int X = x - t * b, Y = y - t * b;if( X / ( a - b ) + Y / ( a - b ) >= t ) return 1;else return 0;
}int main() {scanf( "%d", &T );while( T -- ) {scanf( "%d %d %d %d", &x, &y, &a, &b );if( a == b ) {printf( "%d\n", min( x / a, y / b ) );continue;}if( x < y ) swap( x, y );if( a < b ) swap( a, b );int l = 0, r = y / b, ans;while( l <= r ) {int mid = ( l + r ) >> 1;if( check( mid ) ) ans = mid, l = mid + 1;else r = mid - 1;}printf( "%d\n", ans );}return 0;
}
Codeforces Round #725 (Div. 3) 题解相关推荐
- Codeforces Round #514 (Div. 2)题解
Codeforces Round #514 (Div. 2)题解 A 喵,直接模拟. B 枚举所有盖章时的,合法的,左上角的位置.能盖的话就盖一下.最后check一下图案是否相等即可 C 一轮一轮的扔 ...
- Codeforces Round #725 (Div. 3) G. Gift Set 二分
传送门 文章目录 题意: 思路: 题意: 有两种物品分别有x,yx,yx,y个,每次可以从一个拿出aaa个,另一个拿出bbb个分成一组,问最多能分成多少组. 思路: 这个题有一个显然的单调性,所以二分 ...
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- 【算法题解】Codeforces Round #817 (Div. 4)题解
文章目录 Codeforces Round #817 (Div. 4)题解 A. Spell Check B. Colourblindness C. Word Game D. Line E. Coun ...
- Codeforces Round #747 (Div. 2)题解
Codeforces Round #747 (Div. 2)题解 (本博客将持续更新以后每场CF div2的题解,喜欢ACM.OI的小伙伴记得点个关注哟) 昨天夜晚刷网络流刷入迷了,渐渐就忘记了我还要 ...
- Codeforces Round #789 (Div. 2)题解
Codeforces Round #789 (Div. 2)题解 A. Tokitsukaze and All Zero Sequence 原题链接 算法标签 贪心 排序 思路 情况一:数组存在零 → ...
- Codeforces Round #748 (Div. 3) 题解 完整A~G
Codeforces Round #748 (Div. 3) 题解 A. Elections 题意 已知竞选中三个候选人的当前得票数 a , b , c a,b,c a,b,c,现在可以增加任何一个人 ...
- Codeforces Round #533 (Div. 2)题解
link orz olinr AK Codeforces Round #533 (Div. 2) 中文水平和英文水平都太渣..翻译不准确见谅 T1.给定n<=1000个整数,你需要钦定一个值t, ...
- Codeforces Round #734 (Div. 3) 题解
Hello大家好,今天给大家带来的是 Codeforces Round #734 (Div. 3) 的全题目讲解. 本文链接:https://www.lanqiao.cn/questions/2040 ...
最新文章
- Nginx负载均衡集群介绍
- 【前端】纯前端的一个‘喜欢我吗?’
- 硅谷顶级VC:“S曲线”看四大风口,创企成功机会巨大
- ffmpeg-URL(转)
- 学习笔记(12):Python网络编程并发编程-解决粘包问题-简单版本
- 淘宝上线了新功能,有点元宇宙的意思了
- Spring Cloud Spring Boot mybatis分布式微服务云架构(一)快速入门
- EMNLP2020文档级关系抽取模型GAIN 论文Double Graph Based Reasoning for Document-level Relation Extraction
- 宝塔下php安装mongodb扩展_PHP安装MongoDB扩展
- Atitit 计算机系统结构 计算机系统结构 Cpu 存储 cache 指令系统 目录 Line 56: 第2章指令系统设计 指令格式 寻址方式	1 Line 64: 第3章CPU及其实现
- 【已解决】ws2_32.dll丢失怎么办?ws2_32.dll调用失败如何修复
- 阿里巴巴程序员常用的 15 款开发者工具!你知道几个?
- 电脑桌面计算机找不到,电脑桌面没有了怎么恢复?电脑桌面没有了的恢复方法...
- 时间与日期——瞬时(Instant)
- MFC CStdioFile简单用法
- RewriteRule参数
- DBeaver SQL format 第三方插件方案
- php symfony 安装,安装和设置Symfony框架
- 计算机识别不了usb设备,电脑识别不了USB设备怎么办
- CALCULATE的筛选器参数多种写法
热门文章
- 你们要的印度布线,这是一种极端的牛X
- 造作吧,Python快速入门!
- 应对程序员面试,你必须知道的8大数据结构
- php用switch编写车费的输出,PHP Switch语句在实际代码中的应用
- linux phpunit 安装,PHPUnit安装教程
- jenkins java反序列化_Jenkins “Java 反序列化”过程远程命令执行漏洞
- sql java 创建数据库_java动态创建数据库(sql server)
- java当中用UDP实现相互交流
- learnpython3thehardway视频_LearnPython3theHardWay__Excercise 13 Parameters, Unpacking, Variables
- [Java基础]线程安全的类