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.pswp.cn/diannao/80442.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/80442.shtml
英文地址,請注明出處:http://en.pswp.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模型流水線。預置算法模版、訓練框架、推理框架、模型任務調度和自動…