Codeforces Round #775 (Div. 2, based on Moscow Open Olympiad in Informatics)简训

  • 导语
  • 涉及的知识点
  • 题目
    • A Game
    • B Game of Ball Passing
    • C Weird Sum
    • D Integral Array
  • 参考文献

导语

日常训练,终于多做出来一道题

涉及的知识点

思维,前缀和,树状数组/线段树

链接:Codeforces Round #775 (Div. 2, based on Moscow Open Olympiad in Informatics)

题目

A Game

题目大意:略

思路:统计起点连续1最远能到哪,统计终点连续1最前能到哪,然后获得两个位置的距离即可,如果都是1直接输出0

代码

#include <bits/stdc++.h>
#define int long long
const int inf=0x3f3f3f3f;
const int maxn=1e6+5;
using namespace std;
int n,m,cnt,t,a[121];
signed main() {ios::sync_with_stdio(0);cin.tie(0);cin >>t;while(t--) {cin >>n;int r=n,l=1;for(int i=1; i<=n; i++)cin >>a[i];if(n==2) {//特判cout <<0<<endl;continue;}while(a[r-1])r--;//终点连续1while(a[l+1])l++;//起点连续1cout <<(r-l>0?r-l:0)<<endl;}return 0;
}

B Game of Ball Passing

题目大意:n个人彼此传球,给出最后每个人传球的次数,判断至少几个球能满足最后的传球次数结果

思路:如果次数最大值能够大于其余所有项之和,代表需要额外的球来消耗最大值剩下的值,否则只需要一个球

代码

#include <bits/stdc++.h>
#define int long long
const int inf=0x3f3f3f3f;
const int maxn=1e6+5;
using namespace std;
int n,m,cnt,t,a[maxn];
signed main() {ios::sync_with_stdio(0);cin.tie(0);cin >>t;while(t--) {cin >>n;int sum=0,mx=-1,ans=0;for(int i=1; i<=n; i++) {cin >>a[i],mx=max(mx,a[i]),sum+=a[i];//获得最大值和统计和if(a[i])ans++;}if(ans==0) {//特判全是0cout <<0<<endl;continue;}sum-=mx;if(mx<=sum+1)cout <<1<<endl;else cout <<mx-sum<<endl;}return 0;
}

C Weird Sum

题目大意:给出n×m的矩阵,每个位置填的数字是1到n和1到m,只能填一个,求出每对相同数字的在矩阵上的曼哈顿距离和

思路:对每个数字可以分解成行坐标和列坐标,分别保存,那么分开来处理即可,比如遍历行坐标,增加的距离为当前的行坐标分别减去前面已经出现的行坐标的差值的和,可以使用前缀和来简化过程,对列坐标也是如此,但是需要先排序,最后统计结果即可

代码

#include <bits/stdc++.h>
#define int long long
const int inf=0x3f3f3f3f;
const int maxn=1e5+5;
using namespace std;
int n,m,t;
vector<int>x[maxn],y[maxn];
signed main() {ios::sync_with_stdio(0);cin.tie(0);cin >>n>>m;int sum=0,mx=0;for(int i=1; i<=n; i++)for(int j=1; j<=m; j++) {int v;cin >>v;mx=max(mx,v);x[v].push_back(i);//记录横坐标y[v].push_back(j);//记录纵坐标}for(int i=1; i<=mx; i++) {int len=x[i].size(),prex=0,prey=0;//初始化长度和前缀和if(len==1||len==0)continue;//特判sort(y[i].begin(),y[i].end());//只需要对列坐标排序for(int j=0; j<len; j++) {if(j==0) {//第一个跳过prex+=x[i][j];continue;}sum+=(j)*x[i][j]-prex;//记录这个值与所有已经出现过的值的差值prex+=x[i][j];//统计前缀和}for(int j=0; j<len; j++) {//同上if(j==0) {prey+=y[i][j];continue;}sum+=(j)*y[i][j]-prey;prey+=y[i][j];}}cout <<sum<<endl;return 0;
}

D Integral Array

题目大意:略

思路:假设 x , y ∈ a , r ∉ a x,y\in a,r\notin a x,y∈a,r∈/​a,如果 ⌊ x / y ⌋ = r ⌊x/y⌋=r ⌊x/y⌋=r,那么 y r ≤ x < y ( r + 1 ) yr\le x\lt y(r+1) yr≤x<y(r+1),将思路反过来,如果枚举所有不属于a的r,和所有属于a的y,如果能找到一个x属于给定的区间,那么必然可以判断序列不符合题设条件,将r从1到c枚举,y从1到yr>c位置,利用前缀和判断是否存在这样的一个x,时间复杂度为 O ( C log ⁡ C ) O(C\log C) O(ClogC),证明可以查看参考文献

代码

#include <bits/stdc++.h>
#define int long long
const int inf=0x3f3f3f3f;
const int maxn=2e6+5;
using namespace std;
int n,c,t,pre[maxn];
bool a[maxn];
signed main() {ios::sync_with_stdio(0);cin.tie(0);cin >>t;while(t--) {cin >>n>>c;for(int i=1; i<=c; i++)//初始化a[i]=pre[i]=0;for(int i=1; i<=n; i++) {int x;cin >>x;a[x]=1;//标记存在}bool flag=0;for(int i=1; i<=c; i++)pre[i]=pre[i-1]+a[i];//利用前缀和判断区间内是否存在值for(int r=1; r<=c; r++) {if(flag)break;//找到了if(a[r])continue;//如果在序列中出现,那么必然会作为y,跳过for(int y=1; y*r<=c; y++) {//不超过边界if(!a[y])continue;//如果这个数不存在if(pre[min(y*(r+1)-1,c)]-pre[r*y-1]) {//判断区间内是否存在值flag=1;break;}}}flag?cout <<"NO\n":cout <<"YES\n";}return 0;
}

参考文献

  1. Codeforces Round #775 (Div. 2,based on Moscow Open Olympiad in Informatics) - D. Integral Array - 题解
  2. Codeforces Round #775 (Div. 2) E.Tyler and Strings
  3. Codeforces Round #775 (Div. 2) E. Tyler and Strings

Codeforces Round #775 (Div. 2, based on Moscow Open Olympiad in Informatics)简训相关推荐

  1. C. Tyler and Strings(组合数学,树状数组维护前缀和)(Codeforces Round #775 (Div. 1, based on Moscow Open Olympiad i)

    对我来说比较困难的一题了,尝试着自己写了一下,调不出来遂放弃. Codeforces Round #775 (Div. 1, based on Moscow Open Olympiad in Info ...

  2. Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics)C. Unusual Competitions

    C. Unusual Competitions time limit per test1 second memory limit per test512 megabytes inputstandard ...

  3. Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine))

    Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) 题号 题目 知识点 A Simply Strange Sor ...

  4. Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) A-F全题解

    Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) 文章目录 A. Simply Strange Sort B. ...

  5. Codeforces Round #528 (Div. 2, based on Technocup 2019 Elimination Round 4) C. Connect Three 【模拟】...

    传送门:http://codeforces.com/contest/1087/problem/C C. Connect Three time limit per test 1 second memor ...

  6. Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises)

    A. Fraction 题目链接:http://codeforces.com/contest/854/problem/A 题目意思:给出一个数n,求两个数a+b=n,且a/b不可约分,如果存在多组满足 ...

  7. Codeforces Round #709 (Div. 2, based on Technocup 2021 Final Round) 题解

    文章目录 A. Prison Break B. Restore Modulo C. Basic Diplomacy D. Playlist E. Skyline Photo F. Useful Edg ...

  8. Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2)

    A - Between the Offices 题目链接http://codeforces.com/contest/867/problem/A 解题心得:就是一个水题,读懂题就好,如果从S到F次数大于 ...

  9. Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round)【A、B、C题】

    A. Contest for Robots 签到题,注意特判即可. #include <bits/stdc++.h> using namespace std; const int N=11 ...

最新文章

  1. c语言赋值x为字母,C语言算术、赋值、关系、逻辑运算详细剖析---
  2. 什么样的文献有html阅读,有关html的参考文献
  3. Intel Realsense D435 python wrapper pyrealsense.pipeline类
  4. Pytorch Fashion_MNIST直接离线加载二进制文件到pytorch
  5. MSBuild编译扩展
  6. 单反录像按钮在哪_单反与微单到底哪不同
  7. 修改服务器的共享内存大小,服务器共享内存大小能改么
  8. leetcode练习:292. Nim Game
  9. 百度分享自定义内容和图片
  10. 两道挺有意思的思考题
  11. html input自动获取光标位置,HTML contenteditable 标签里怎样获取光标像素位置?
  12. 百度在美国遭集体起诉;iPhone 11 成苹果最畅销机型;OpenSSL 曝高危漏洞 | 极客头条...
  13. 服务器遭受***后的处理过程
  14. js遍历数组和遍历对象的区别
  15. 计算机高一基础知识大全,计算机基础知识大全
  16. 数据库系统-数据库设计
  17. 电脑热点的连接问题(基于现有IPhone12)
  18. Enterprise Architect安装使用
  19. 基于51单片机十字路口交通灯_只显示绿灯时间+黄灯5s
  20. 反燃油车占位方案:AI识别+EasyCVR解决燃油车占位问题

热门文章

  1. django后台运行runserver
  2. php 执行bin文件,bin文件是干什么的
  3. wordpress汉化技巧_保护WordPress网站安全的10个鲜为人知的技巧
  4. 校园网掉线自动重连小助手--Python
  5. 公共资源交易中心使用美赛思光盘打印刻录机系统自动实现一盘一项目
  6. 【原创】Android-项目实战(附Demo)
  7. 学习笔记之《Android项目实战——手机安全卫士》
  8. 计算机简单故障排除教案,计算机故障检测与排除教案,计算机故障排除方法
  9. 全新C/C++发展路线,看这里
  10. ubuntu 命令行卸载并清理软件