Codeforces Round 1020 (Div. 3)(题解ABCDEF)

A. Dr. TC

有n次翻转,从1到n,0->1,1->0,每次统计1的数量,设cnt1是字符串1的数量,n次就是n*cnt1,

但每个1都会被翻转一次减去一个cnt1,再统计cnt0,每个被翻转一次,答案就是(n-1)*cnt1+cnt0

#include<iostream>
#include<vector>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<queue>
#include<cstring>
#include<stack>
#include<array>
#include<cmath>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<iomanip>
using namespace std;
using ll = long long;
using llu = unsigned long long;
const ll inf = 0x3f3f3f3f3f3f3f3fll;
const ll MIN = -9187201950435737472ll;
ll mod = 1e9 + 7;
ll base = 131;
const int N = 1e4 + 10;
void solve()
{int n;cin>>n;string s;cin>>s;int cnt1=0,cnt0=0;for(int i=0;i<n;i++){if(s[i]=='1')cnt1++;else cnt0++;}cout<<(n-1)*cnt1+cnt0<<endl;
}
int main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t = 1;cin>>t;while (t--){solve();}return 0;
}

B. St. Chroma 

给一个排列,从1到n依次做mex操作,让x出现次数最多 ,要出现x,要先排0~x-1,再放x后面的数字,最后再放x

#include<iostream>
#include<vector>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<queue>
#include<cstring>
#include<stack>
#include<array>
#include<cmath>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<iomanip>
using namespace std;
using ll = long long;
using llu = unsigned long long;
const ll inf = 0x3f3f3f3f3f3f3f3fll;
const ll MIN = -9187201950435737472ll;
ll mod = 1e9 + 7;
ll base = 131;
const int N = 1e4 + 10;
void solve()
{int n,x;cin>>n>>x;vector<int>ans(n);for(int i=0;i<x;i++)ans[i]=i;for(int i=x;i<n-1;i++)ans[i]=i+1;ans[n-1]=x==n?x-1:x;for(int i=0;i<n;i++)cout<<ans[i]<<" ";cout<<endl;
}
int main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t = 1;cin>>t;while (t--){solve();}return 0;
}

C. Cherry Bomb 

给出a和b数组,当对应和都相等,就是互补数组,b中有缺失,求方案数

先找无解,那就是给出的对应和有多个 ,或凭借b的范围无法凑出对应和

有解的话就是1个,或b全是-1,有多个,找a的最小值与最大值,即可求

#include<iostream>
#include<vector>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<queue>
#include<cstring>
#include<stack>
#include<array>
#include<cmath>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<iomanip>
using namespace std;
using ll = long long;
using llu = unsigned long long;
const ll inf = 0x3f3f3f3f3f3f3f3fll;
const ll MIN = -9187201950435737472ll;
ll mod = 1e9 + 7;
ll base = 131;
const int N = 1e4 + 10;
void solve()
{int n,k;cin>>n>>k;ll maxx=-2,minn=1e18;ll sum=-1;vector<ll>a(n+1),b(n+1);for(int i=1;i<=n;i++){cin>>a[i];maxx=max(maxx,a[i]);minn=min(minn,a[i]);}bool tag=true;for(int i=1;i<=n;i++){cin>>b[i];if(b[i]!=-1){if(sum==-1)sum=a[i]+b[i];else{if(a[i]+b[i]!=sum)tag=false;}}}if(!tag){cout<<0<<endl;}else if(sum!=-1){bool flag=true;for(int i=1;i<=n&&flag;i++){if(b[i]==-1){if(a[i]+k<sum||a[i]>sum)flag=false;}}if(flag)cout<<1<<endl;else cout<<0<<endl;}else{ll up=minn+k;if(up<maxx)cout<<0<<endl;else cout<<up-maxx+1<<endl;}
}
int main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t = 1;cin>>t;while (t--){solve();}return 0;
}

D. Flower Boy 

在a中找出一个长为m的序列,让对应ai都大于bi,也可删去b中一个再找

不操作有解直接输出

操做的话,考虑枚举删去的b,对a做前缀和与后缀和,pre[i]表示前i个元素可匹配b中前多少个,suf[i]同理

枚举b的过程中,到i,表示要找i-1个先匹配,再找n-i个在后面匹配,二分pre数组,看suf是否合法

#include<iostream>
#include<vector>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<queue>
#include<cstring>
#include<stack>
#include<array>
#include<cmath>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<iomanip>
using namespace std;
using ll = long long;
using llu = unsigned long long;
const ll inf = 0x3f3f3f3f3f3f3f3fll;
const ll MIN = -9187201950435737472ll;
ll mod = 1e9 + 7;
ll base = 131;
const int N = 1e4 + 10;
void solve()
{int n,m;cin>>n>>m;vector<ll>a(n+1),b(m+1);for(int i=1;i<=n;i++)cin>>a[i];for(int i=1;i<=m;i++)cin>>b[i];int pos=1;for(int i=1;i<=n;i++){if(pos!=m+1&&a[i]>=b[pos])pos++;}if(pos==m+1){cout<<0<<endl;return;}vector<int>pre(n+1,0),suf(n+3,0);int t=1;for(int i=1;i<=n;i++){int k=0;if(a[i]>=b[t])t++,k=1;pre[i]=pre[i-1]+k;}//for(int i=1;i<=n;i++)cout<<pre[i]<<" ";//cout<<endl;t=m;for(int i=n;i>=0;i--){int k=0;if(a[i]>=b[t])t--,k=1;suf[i]=suf[i+1]+k;}ll ans=1e18;for(int i=1;i<=m;i++){int pos=lower_bound(pre.begin(),pre.end(),i-1)-pre.begin();//cout<<pos<<endl;if(pos>n)continue;if(pre[pos]==i-1&&suf[pos+1]>=m-i)ans=min(ans,b[i]);}if(ans==1e18)cout<<-1<<endl;else cout<<ans<<endl;
}
int main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t = 1;cin>>t;while (t--){solve();}return 0;
}
/*
2
5 5
7 7 6 7 7
7 7 7 7 7
*/

 E. Wolf

给出一个排列,有q次询问,询问l到r中能否二分到x,不可输出-1,可的话找最小操作数

可做的操作是对数组除x以外的数任意调换顺序,找调换顺序的最小个数

可用st数组记录每个元素的位置

#1.当mid<st[x],且p[mid]>x需要操作,将p[mid]换成小于x的

#2.当mid>st[x],且p[mid]<x需要操作,将p[mid]换成大于x的

注意到1与2,之间可以直接交换使其都合法

模拟二分过程,记录mid>x的次数,和mid>x并且合法次数

记录mid<x次数,和mid<x合法次数

于是就得到了mid<x与>x的不合法次数,抵消到有剩余,判断剩余的有没有对应剩余的可抵消

 

#include<iostream>
#include<vector>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<queue>
#include<cstring>
#include<stack>
#include<array>
#include<cmath>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<iomanip>
using namespace std;
using ll = long long;
using llu = unsigned long long;
const ll inf = 0x3f3f3f3f3f3f3f3fll;
const ll MIN = -9187201950435737472ll;
ll mod = 1e9 + 7;
ll base = 131;
const int N = 1e4 + 10;
void solve()
{int n,q;scanf("%d%d",&n,&q);vector<int>p(n+1),st(n+1);for(int i=1;i<=n;i++){cin>>p[i];st[p[i]]=i;}vector<int>ans;while(q--){int l,r,x;scanf("%d%d%d",&l,&r,&x);if(st[x]<l||st[x]>r){ans.push_back(-1);continue;}if(l==r){if(p[l]==x)ans.push_back(0);else ans.push_back(-1);continue;}int L=0,R=0,LL=0,RR=0;while(l<r){int mid=(l+r)/2;if(p[mid]==x)break;if(mid<st[x]){ L++;if(p[mid]<x)LL++;l=mid+1;}else{R++;if(p[mid]>x)RR++;r=mid-1;}//cout<<l<<endl;}//cout<<l<<" "<<r<<endl;//cout<<cnt<<" "<<ok<<endl;if(L>x-1||R>n-x)ans.push_back(-1);else{L-=LL;R-=RR;if(L>=R){ll tmp=L-R;if(x-1>=tmp+LL+R)ans.push_back(2ll*R+2ll*(L-R));else ans.push_back(-1);}else{ll tmp=R-L;if(n-x>=tmp+RR+L)ans.push_back(2ll*L+2ll*(R-L));else ans.push_back(-1);}}}for(auto y:ans)printf("%d ",y);printf("\n");
}
int main()
{int t = 1;scanf("%d",&t);while (t--){solve();}return 0;
}
/*
3
13 1
12 13 10 9 8 4 11 5 7 6 2 1 3
1 13 2
*/

F. Goblin 

与a题共享题面,但问的不同,n次操作后,我们需要找出这n*n的方格中 最大连通0的数量

考虑dp

注意到每次操作的数形成了主对角线

对于每个字符i,它在aii处翻转,其余保持不变

我们一列一列的添加,发现出现了四种状态转移

0->1,0->0,1->0,1->1

并且在一列一列添加的过程中,场上0的联通块数量不会大于2

因为如果s[i]是0,中间有1,其余为0,将其隔开有两个连通块,如s[i+1]是0,它的上连通块和下联通块会继承s[i]的

如果s[i+1]是1,一个0隔开上列1和下列1,它会将上连通块截止,继承上一个的下连通块

设置状态dp[i][0]表示到第i列,上连通块0的数量

dp[i][1]表示到第i列,下连通块0数量

 

#include<iostream>
#include<vector>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<queue>
#include<cstring>
#include<stack>
#include<array>
#include<cmath>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<iomanip>
using namespace std;
using ll = long long;
using llu = unsigned long long;
const ll inf = 0x3f3f3f3f3f3f3f3fll;
const ll MIN = -9187201950435737472ll;
ll mod = 1e9 + 7;
ll base = 131;
const int N = 1e4 + 10;
void solve()
{int n;cin>>n;string s;cin>>s;s="#"+s;vector<vector<ll>>dp(n+1,vector<ll>(2,0));if(s[1]=='0')dp[1][1]=n-1;else dp[1][1]=1;ll ans=0;for(int i=2;i<=n;i++){if(s[i-1]=='0'&&s[i]=='1'){ans=max(ans,dp[i-1][0]);dp[i][1]=dp[i-1][1]+1;}else if(s[i-1]=='0'&&s[i]=='0'){dp[i][0]=dp[i-1][0]+i-1;dp[i][1]=dp[i-1][1]+n-i;}else if(s[i-1]=='1'&&s[i]=='0'){dp[i][0]=dp[i-1][1]+i-1;dp[i][1]=n-i;}else{ans=max(ans,dp[i-1][1]);dp[i][1]=1;}}ans=max(ans,dp[n][0]);ans=max(ans,dp[n][1]);cout<<ans<<endl;
}
int main()
{ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);int t = 1;cin>>t;while (t--){solve();}return 0;
}

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/80442.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

HTML字符实体和转义字符串

HTML字符实体和转义字符串用于处理特殊字符&#xff0c;确保它们在不同上下文中正确显示或解析。以下是详细总结&#xff1a; HTML字符实体&#xff08;Character Entities&#xff09; ‌定义‌&#xff1a;用于在HTML中表示保留字符或不可见字符&#xff0c;避免与HTML语法…

FreeRTOS菜鸟入门(六)·移植FreeRTOS到STM32

目录 1. 获取裸机工程模版 2. 下载 FreeRTOS V9.0.0 源码 3. FreeRTOS文件夹内容简介 3.1 FreeRTOS文件夹 3.1.1 Demo文件夹 3.1.2 License 文件夹 3.1.3 Source 文件夹 3.2 FreeRTOS-Plus 文件夹 4. 往裸机工程添加 FreeRTOS 源码 5. 拷贝 FreeRTOSConfig…

通过 Tailwind CSS 自定义样式 实现深色模式切换

创建vite项目或者vue-cli配置大同小异 1、当前环境 Vue.js 3.5nuxtjs/tailwindcss 6.13.1nuxt3.15.4node18 这里主要依赖是tailwindcss 因为当前项目是使用nuxt开发。 2、配置颜色模式 在assets/css下创建main.css * {padding: 0;margin: 0;box-sizing: border-box; }[dat…

PWNOS:2.0(vulnhub靶机)

文章目录 靶机地址主机发现、端口扫描web渗透目录探测漏洞利用权限提升 解密工具地址总结 靶机地址 https://download.vulnhub.com/pwnos/pWnOS_v2.0.7z 这里如果是windows系统直接使用vmware或者virtubox打开可以使用,如果是mac系统需再去做一个配置&#xff0c;比较麻烦 这里…

Gartner魔力象限(Gartner Magic Quadrant)

Gartner魔力象限&#xff08;Gartner Magic Quadrant&#xff09;是由全球领先的研究和咨询公司Gartner发布的市场研究报告&#xff0c;广泛应用于IT行业&#xff0c;尤其是在技术供应商评估中。它以图形化的方式展示了不同技术领域中各个供应商的市场表现&#xff0c;帮助企业…

信创时代开发工具选择指南:国产替代背景下的技术生态与实践路径

&#x1f9d1; 博主简介&#xff1a;CSDN博客专家、CSDN平台优质创作者&#xff0c;高级开发工程师&#xff0c;数学专业&#xff0c;10年以上C/C, C#, Java等多种编程语言开发经验&#xff0c;拥有高级工程师证书&#xff1b;擅长C/C、C#等开发语言&#xff0c;熟悉Java常用开…

人口老龄化丨AI健康小屋如何实现防病于未然​

随着全球老龄化加剧&#xff0c;“银发浪潮” 对医疗资源、养老护理和健康管理提出了严峻挑战。 由此智绅科技应运而生&#xff0c;七彩喜智慧养老系统构筑居家养老安全网。 AI 健康小屋作为银发科技的创新载体&#xff0c;通过智能化健康监测、精准化风险预警、便捷化医疗衔…

【金仓数据库征文】金仓数据库:开启未来技术脑洞,探索数据库无限可能

我的个人主页 我的专栏&#xff1a; 人工智能领域、java-数据结构、Javase、C语言&#xff0c;希望能帮助到大家&#xff01;&#xff01;&#xff01; 点赞&#x1f44d;收藏❤ 目录 引言&#xff1a;数据库进化的下一站 —— 未来科技的无限可能金仓数据库简介&#xff1a;国…

#什么是爬虫?——从技术原理到现实应用的全面解析 VI

什么是爬虫?——从技术原理到现实应用的全面解析 V 二十六、异构数据采集技术突破 26.1 PDF文本与表格提取 import pdfplumber import pandas as pddef extract_pdf_data(pdf_path):"""从PDF中提取文本和表格数据:param pdf_path: PDF文件路径:return: 包含…

关于Spring Boot构建项目的相关知识

一 前端框架 1 VUE框架 1.1 简介 Vue是一款流行的JavaScript框架&#xff0c;用于构建用户界面和单页面应用程序。它的设计初衷是为了简化Web开发过程&#xff0c;使开发者能够快速构建交互性强、响应速度快的Web应用。 1.2 优点 简单易用&am…

PPO 强化学习机械臂 IK 训练过程可视化利器 Tensorboard

视频讲解&#xff1a; PPO 强化学习机械臂 IK 训练过程可视化利器 Tensorboard PPO 强化学习过程中&#xff0c;设置了verbose会显示数据&#xff0c;但还是不够直观&#xff0c;这里上一个可视化利器&#xff0c;Tensorboard&#xff0c;实际上stable baselines3中已经有了这部…

UE5的 Modify Curve 蓝图节点

In Unreal Engine’s Animation Blueprints, the Modify Curve node lets you drive and alter any named Animation Curve on your character at runtime. The Apply Mode setting on that node controls how the “new” value you feed in (via the added curve‐input pin)…

【Hive入门】Hive分区与分区表完全指南:从原理到企业级实践

引言 在大数据时代&#xff0c;高效管理海量数据成为企业面临的核心挑战。Hive作为Hadoop生态系统中最受欢迎的数据仓库解决方案&#xff0c;其分区技术是优化数据查询和管理的关键手段。本文将全面解析Hive分区技术的原理、实现方式及企业级最佳实践&#xff0c;帮助您构建高性…

jdk-8u202-linux-x64.tar.gz官方下载地址

https://www.oracle.com/java/technologies/javase/javase8-archive-downloads.html 点击下载&#xff0c;需要先注册oracle账号&#xff0c;很好注册随便写&#xff0c;注册完登录就可以下载了。目前就Oracle JDK 8u201/202 是最后两个可免费用于商业用途的公开版本

OpenCv高阶(十)——光流估计

文章目录 前言一、光流估计二、使用步骤1、导库读取视频、随机初始化颜色2、初始化光流跟踪3、视频帧处理循环4、光流计算与可视化5、循环控制与资源释放完整代码 总结 前言 在计算机视觉领域&#xff0c;光流估计是捕捉图像序列中像素点运动信息的核心技术。它描述了图像中每…

AIGC实战之如何构建出更好的大模型RAG系统

一、RAG 系统核心架构解析 1. 检索模块深度优化 1.1 混合检索技术实现 技术原理&#xff1a;结合稀疏检索&#xff08;BM25&#xff09;与密集检索&#xff08;DPR&#xff09;&#xff0c;通过动态权重分配提升检索精度。例如&#xff0c;在医疗领域&#xff0c;BM25 负责精…

Rust 学习笔记:函数和控制流

Rust 学习笔记&#xff1a;函数和控制流 Rust 学习笔记&#xff1a;函数和控制流函数&#xff08;Function&#xff09;语句和表达式带返回值的函数注释控制流if 表达式使用 else if 处理多个条件在 let 语句中使用 if循环loop从循环中返回值循环标签消除多个循环之间的歧义带 …

c#加密证件号的中间部分,改为*号

前言 使用场景&#xff1a;在我项目中&#xff0c;我需要给前端提供接口&#xff0c;所以我要吧证件号进行加密。例如&#xff1a;411421199510225612&#xff0c;这是一个身份证号&#xff0c;18为的&#xff0c;那么我加密完成之后就会是 411421********5612&#xff0c;类似…

存储新势力:助力DeepSeek一体机

宝子们&#xff0c;今天要给大家分享一个超酷的科技话题——各大厂商陆续推出的DeepSeek训推一体机方案。 【集成人工智能训推平台】 它就像是一个超级智能的大脑中枢&#xff0c;为各种复杂的AI任务搭建AI模型流水线。预置算法模版、训练框架、推理框架、模型任务调度和自动…