AtCoder Beginner Contest 417

文章目錄

    • A A Substring
    • B Search and Delete
    • C Distance Indicators
    • D Takahashi's Expectation
    • E A Path in A Dictionary
    • F Random Gathering
    • G Binary Cat

AtCoder Beginner Contest 417

A A Substring

You are given an N-character string S consisting of lowercase English letters.
Output the string of N?A?B characters obtained by removing the first A characters and the last B characters from S.

翻譯:

給定一個由 N 個小寫英文字母組成的字符串 S。
輸出 S 字符串移除前 A個字符,后 B 個字符后的字符串。

分析:使用string中的 s.erase(pos, k) 刪除 s中的從 pos 下標開始的連續 k個元素。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, inf = 0x3f3f3f3f;void solve() {int n, a, b;string s;cin >> n >> a >> b >> s;s.erase(0, a);s.erase(s.size()-b, b); cout << s;
}int main() {int T = 1; while (T--) solve();return 0;
}

B Search and Delete

Takahashi has an integer sequence A=(A1,A2,…,AN)A=(A_1,A_2,…,A_N)A=(A1?,A2?,,AN?) of length N.
It is guaranteed that A is non-decreasing.

Takahashi performs M operations on this integer sequence.
In the i-th operation (1≤i≤M)(1\le i\le M)(1iM), he performs the following operation:

If the sequence A contains BiB_iBi? as an element, select one such element and delete it. If no such element exists, do nothing.
Note that since A is non-decreasing, the sequence after the operation is uniquely determined regardless of the choice of element, and remains non-decreasing.

Find A after performing M operations.

翻譯:

高橋有一個長度為 N 的整數序列 A=(A1,A2,…,AN)A=(A_1,A_2,…,A_N)A=(A1?,A2?,,AN?)
保證 A 是非遞減的。

高橋對這個整數序列執行 M 次操作。在第 i (1≤i≤M)(1\le i\le M)(1iM) 次操作中,他執行以下操作:

如果序列 A 包含 BiB_iBi? 作為元素,選擇其中一個這樣的元素并刪除它。如果不存在這樣的元素,則不執行任何操作。
注意,由于 A 是非遞減的,操作后的序列無論選擇哪個元素都是唯一確定的,并且仍然保持非遞減。

執行 M 次操作后找到 A 。

分析:數據范圍很小,直接暴力模擬即可。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, inf = 0x3f3f3f3f;void solve() {int n, m;cin >> n >> m;vector<int> 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];for (int i = 1; i <= m; i++) {for (int j = 1; j <= n; j++) {if (b[i] == a[j]) {a[j] = -1;break;}}}for (int i = 1; i <= n; i++) {if (a[i] != -1)cout << a[i] << ' ';}
}int main() {int T = 1; while (T--) solve();return 0;
}

C Distance Indicators

You are given an integer sequence A=(A1,A2,...AN)A=(A_1,A_2,...A_N)A=(A1?,A2?,...AN?) of length N.

Find how many pairs of integers (i,j) (1≤i<j≤N1\le i<j \le N1i<jN) satisfy j?i=Ai?Ajj-i=A_i-A_jj?i=Ai??Aj?.

Constraints:1≤N≤2×105,1≤Ai≤2×105(1≤i≤N)1\le N\le 2\times 10^5,1 \le A_i \le 2\times 10^5(1\le i\le N)1N2×105,1Ai?2×105(1iN)

翻譯:

你被給定一個長度為 N 的整數序列 A=(A1,A2,...AN)A=(A_1,A_2,...A_N)A=(A1?,A2?,...AN?)

找出有多少對整數 (i,j) (1≤i<j≤N1\le i<j \le N1i<jN) 滿足 j?i=Ai?Ajj-i=A_i-A_jj?i=Ai??Aj?

約束:1≤N≤2×105,1≤Ai≤2×105(1≤i≤N)1\le N\le 2\times 10^5,1\le A_i \le 2\times 10^5(1\le i\le N)1N2×105,1Ai?2×105(1iN)

分析:數據范圍較大,枚舉復雜度為 O(n2)O(n^2)O(n2),考慮將原式變形:移項得 j?aj=i+ai≥2j-a_j =i+a_i \ge 2j?aj?=i+ai?2,可以發現左右兩邊均是與當前位置有關的信息。

使用標記數組 st[i+a[i]]++st[i + a[i]] ++st[i+a[i]]++ 表示 i+a[i]i+a[i]i+a[i] 出現的次數加一。

由于答案的更新在 st 更新前,所以保證了 j<ij<ij<i

答案需要開 long long。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5, inf = 0x3f3f3f3f;void solve() {ll n, ans = 0;cin >> n;vector<int> a(n + 1), st(N << 1, 0);for (int i = 1; i <= n; i++) {cin >> a[i];if (i - a[i] >= 2)ans += st[i - a[i]];st[i + a[i]]++;}cout << ans << "\n";
}
int main() {int T = 1; while (T--) solve();return 0;
}

D Takahashi’s Expectation

翻譯:

分析:

 

E A Path in A Dictionary

You are given a simple connected undirected graph G with N vertices and M edges.
The vertices of G are numbered vertex 1, vertex 2, …, vertex N, and the i-th (1≤i≤M)(1\le i \le M)(1iM) edge connects vertices UiU_iUi? and ViV_iVi?.

Find the lexicographically smallest simple path from vertex X to vertex Y in G.
That is, find the lexicographically smallest among the integer sequences P=(P1,P2,…,P∣P∣)P=(P1 ,P2 ,…,P_{∣P∣})P=(P1,P2,,PP?) that satisfy the following conditions:

  • 1≤Pi≤N1 \le P_i \le N1Pi?N
  • If i≠ji\neq ji=j, then Pi≠PjP_i\neq P_jPi?=Pj?.
  • P1=XP_1=XP1?=X and P∣P∣=YP_{∣P∣}=YPP?=Y.
  • For 1≤i≤∣P∣?11\le i\le ∣P∣?11i≤∣P?1, there exists an edge connecting vertices PiP_iPi? and Pi+1P_{i+1}Pi+1?.

One can prove that such a path always exists under the constraints of this problem.

You are given T test cases, so find the answer for each.

翻譯:
給定一個簡單的連通無向圖 G ,它有 N 個頂點和 M 條邊。
G 的頂點編號為頂點 1、2 、… 、N ,第 i 條 (1≤i≤M)(1\le i \le M)(1iM) 邊連接頂點 UiU_iUi?ViV_iVi?
?在 G 中,找到從頂點 X 到頂點 Y 的字典序最小的簡單路徑。
也就是說,找到滿足以下條件的整數序列 P=(P1,P2,…,P∣P∣)P=(P1 ,P2 ,…,P_{∣P∣})P=(P1,P2,,PP?) 中字典序最小的序列:

  • 1≤Pi≤N1 \le P_i \le N1Pi?N
  • 如果 i≠ji\neq ji=j, 那么 Pi≠PjP_i\neq P_jPi?=Pj?.
  • P1=XP_1=XP1?=XP∣P∣=YP_{∣P∣}=YPP?=Y.
  • 對于 1≤i≤∣P∣?11\le i\le ∣P∣?11i≤∣P?1, 存在一條邊連接頂點 PiP_iPi?Pi+1P_{i+1}Pi+1?.

可以證明,在問題的約束條件下,這樣的路徑總是存在。
給出了 T 個測試用例,所以找出每個的答案。

分析:建圖后按節點升序排序,dfs 遍歷即可。

#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, inf = 0x3f3f3f3f, inf2 = 1e18;
vector<int> G[N];
int a[N], n, m, x, y, p;
bool st[N], flag;void dfs(int u) {if (flag) return;st[u] = 1, a[++p] = u;if (u == y) {for (int i = 1; i <= p; i++)cout << a[i] << " \n"[i == p];flag = 1; return;}for (auto& v : G[u]) {if (st[v]) continue;dfs(v);}--p;
}
void solve() {cin >> n >> m >> x >> y;p = 0, flag = 0;memset(st, 0x00, sizeof st);for (int i = 1; i <= n; i++) G[i].clear();for (int i = 1, u, v; i <= m; i++) {cin >> u >> v;G[u].push_back(v), G[v].push_back(u);}for (int i = 1; i <= n; i++) sort(G[i].begin(), G[i].end());dfs(x);
}
signed main() {int T = 1;cin >> T;while (T--) solve();return 0;
}

F Random Gathering

G Binary Cat

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/94180.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/94180.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/94180.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

C++23 Concepts:用類型約束重構泛型編程的終極方案

一、開篇:模板元編程的"類型檢查困局" 某金融量化團隊曾遇到詭異bug: template<typename T> void process(T data) {static_assert(std::is_arithmetic<T>::value, "需要數值類型");// 業務邏輯... } 當調用process("hello")時…

【RK3568 看門狗驅動開發詳解】

RK3568 看門狗驅動開發詳解一、Linux 看門狗子系統架構?二、設備樹配置?三、 看門狗驅動實現四、驗證看門狗定時器&#xff08;Watchdog Timer&#xff09;是保障嵌入式系統可靠性的關鍵硬件&#xff0c;它通過定期接收 “喂狗” 信號監控系統運行狀態&#xff0c;當系統故障…

探索 Vue 3.6 新特性:Vapor Mode 與高性能 Web 應用開發

Vue 3.6 簡介 Vue.js 是一個廣受歡迎的漸進式 JavaScript 框架&#xff0c;以其簡潔的 API、靈活的組件系統和高性能著稱。Vue 3.6 是 Vue 3 系列的一個重要版本&#xff0c;引入了多項性能優化和新特性&#xff0c;尤其是備受關注的 Vapor Mode&#xff0c;這是一個無需虛擬 D…

初識prometheus

Prometheus&#xff1a;云原生時代的監控利器 在當今快速發展的云原生和微服務架構時代&#xff0c;傳統的監控系統面臨著巨大的挑戰&#xff1a;如何高效地收集海量、動態變化的指標&#xff1f;如何實時告警并快速定位問題&#xff1f;如何實現靈活的可視化和強大的數據查詢…

從源碼角度分析導致 JVM 內存泄露的 ThreadLocal

文章目錄1. 為什么需要ThreadLocal2. ThreadLocal的實現解析1.1 實現分析1.2 具體實現1.3 ThreadLocalMap中Hash沖突的解決1.3.1 Hash沖突解決的幾種方法1.3.1.1 開放定值法1.3.1.2 鏈地址法1.3.1.3再哈希法&#xff1a;1.3.1.4 建立公共溢出區1.3.2 ThreadLocal解決Hash沖突的…

React組件化的封裝

1. 組件化封裝的結構 1.1. 定義一個類(組件名必須是大寫&#xff0c;小寫會被認為是html元素), 繼續自React.Component1.2. 實現當前組件的render函數 render當中返回的jsx內容&#xff0c;就是之后React會幫助我們渲染的內容 1.3. 結構圖如下&#xff1a; data 方法render()…

嵌入式仿真教學的革新力量:深圳航天科技創新研究院引領高效學習新時代

嵌入式系統作為現代信息技術的核心基石&#xff0c;已深度融入工業控制、物聯網、智能終端等關鍵領域。高校肩負著培養嵌入式技術人才的重任&#xff0c;但傳統教學方式正面臨嚴峻挑戰&#xff1a;硬件實驗設備投入巨大、更新滯后、維護繁瑣、時空限制嚴格&#xff0c;難以滿足…

六、Linux核心服務與包管理

作者&#xff1a;IvanCodes 日期&#xff1a;2025年8月3日 專欄&#xff1a;Linux教程 要保證一個Linux系統穩定、安全、功能完備&#xff0c;有效管理其后臺服務和軟件包是至關重要的。本文將深入介紹現代Linux系統中四個核心的管理工具&#xff1a;systemctl (服務管理)&…

【數據結構】哈希表實現

目錄 1. 哈希概念 2 哈希沖突和哈希函數 3. 負載因子 4. 將關鍵字轉為整數 5. 哈希函數 5.1直接定址法 5.2 除法散列法/除留余數法 5.3 乘法散列法&#xff08;了解&#xff09; 5.4 全域散列法&#xff08;了解&#xff09; 5.5 其他方法&#xff08;了解&#xff09…

PostgreSQL面試題及詳細答案120道(21-40)

《前后端面試題》專欄集合了前后端各個知識模塊的面試題&#xff0c;包括html&#xff0c;javascript&#xff0c;css&#xff0c;vue&#xff0c;react&#xff0c;java&#xff0c;Openlayers&#xff0c;leaflet&#xff0c;cesium&#xff0c;mapboxGL&#xff0c;threejs&…

數據建模及基本數據分析

目錄 &#xff08;一&#xff09;數據建模 1.以數據預測為核心的建模 2.以數據聚類為核心的建模 &#xff08;二&#xff09;基本數據分析 1.Numpy 2. Pandas 3.實例 4.Matplotlib 資料自取&#xff1a; 鏈接: https://pan.baidu.com/s/1PROmz-2hR3VCTd6Eei6lFQ?pwdy8…

電動汽車DCDC轉換器的用途及工作原理

在電動汽車的電氣架構中&#xff0c;DCDC轉換器&#xff08;直流-直流轉換器&#xff09;是一個至關重要的部件&#xff0c;負責協調高壓動力電池&#xff08;通常300V~800V&#xff09;與低壓電氣系統&#xff08;12V/24V&#xff09;之間的能量流動。它的性能直接影響整車的能…

PyTorch 應用于3D 點云數據處理匯總和點云配準示例演示

PyTorch 已廣泛應用于 3D 點云數據處理&#xff0c;特別是在深度學習驅動的任務中如&#xff1a; 分類、分割、配準、重建、姿態估計、SLAM、目標檢測 等。 傳統 3D 點云處理以 PCL、Open3D 為主&#xff0c;深度學習方法中&#xff0c;PyTorch 是構建神經網絡處理點云的核心框…

ABP VNext + Quartz.NET vs Hangfire:靈活調度與任務管理

ABP VNext Quartz.NET vs Hangfire&#xff1a;靈活調度與任務管理 &#x1f680; &#x1f4da; 目錄ABP VNext Quartz.NET vs Hangfire&#xff1a;靈活調度與任務管理 &#x1f680;? TL;DR&#x1f6e0; 環境與依賴&#x1f527; Quartz.NET 在 ABP 中接入1. 安裝與模塊…

[硬件電路-148]:數字電路 - 什么是CMOS電平、TTL電平?還有哪些其他電平標準?發展歷史?

1. CMOS電平定義&#xff1a; CMOS&#xff08;Complementary Metal-Oxide-Semiconductor&#xff09;電平基于互補金屬氧化物半導體工藝&#xff0c;由PMOS和NMOS晶體管組成。其核心特點是低功耗、高抗干擾性和寬電源電壓范圍&#xff08;通常為3V~18V&#xff09;。關鍵參數&…

0基礎網站開發技術教學(二) --(前端篇 2)--

書接上回說到的前端3種主語言以及其用法&#xff0c;這期我們再來探討一下javascript的一些編碼技術。 一) 自定義函數 假如你要使用一個功能&#xff0c;正常來說直接敲出來便可。可如果這個功能你要用不止一次呢?難道你每次都敲出來嗎?這個時侯&#xff0c;就要用到我們的自…

前端 拼多多4399筆試題目

拼多多 3 選擇題 opacity|visibity|display區別 在CSS中&#xff0c;opacity: 0 和 visibility: hidden 都可以讓元素不可見&#xff0c;但它們的行為不同&#xff1a; ? opacity: 0&#xff08;透明度為0&#xff09; 元素仍然占據空間&#xff08;不移除文檔流&#xff0…

數琨創享:全球汽車高端制造企業 QMS質量管理平臺案例

01.行業領軍者的質量升級使命在全球汽車產業鏈加速升級的浪潮中&#xff0c;質量管控能力已成為企業核心競爭力的關鍵。作為工信部認證的制造業單項冠軍示范企業&#xff0c;萬向集團始終以“全球制造、全球市場、做行業領跑者”為戰略愿景。面對奔馳、寶馬、大眾等“9N”高端客…

GaussDB 約束的使用舉例

1 not null 約束not null 約束強制列不接受 null 值。not null 約束強制字段始終包含值。這意味著&#xff0c;如果不向字段添加值&#xff0c;就無法插入新記錄或者更新記錄。GaussDB使用pg_get_tabledef()函數獲取customers表結構&#xff0c;如&#xff1a;csdn> set sea…

自動駕駛中的傳感器技術13——Camera(4)

1、自駕Camera開發的方案是否歸一化對于OEM&#xff0c;或者自駕方案商如Mobileye如果進行Camera的開發&#xff0c;一般建議采用Tesla的系統化最優方案&#xff0c;所有Camera統一某個或者某兩個MP設計&#xff08;增加CIS議價權&#xff0c;減少Camera PCBA的設計維護數量&am…