5.30 學習總

刷題記錄(Codeforces Round 947 (Div. 1 + Div. 2)B,C題)和Codeforces Round 948 (Div. 2)B題

一.B. 378QAQ and Mocha's Array
B. 378QAQ and Mocha's Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mocha likes arrays, so before her departure, 378QAQ gave her an array?a𝑎?consisting of?n𝑛?positive integers as a gift.

Mocha thinks that?a𝑎?is?beautiful?if there exist two numbers?i𝑖?and?j𝑗?(1≤i,j≤n1≤𝑖,𝑗≤𝑛,?i≠j𝑖≠𝑗) such that for all?k𝑘?(1≤k≤n1≤𝑘≤𝑛),?ak𝑎𝑘?is divisible???by either?ai𝑎𝑖?or?aj𝑎𝑗.

Determine whether?a𝑎?is beautiful.

???x𝑥?is divisible by?y𝑦?if there exists an integer?z𝑧?such that?x=y?z𝑥=𝑦?𝑧.

Input

Each test contains multiple test cases. The first line contains the number of test cases?t𝑡?(1≤t≤5001≤𝑡≤500). The description of the test cases follows.

The first line of each test case contains a single integer?n𝑛?(3≤n≤1053≤𝑛≤105)?— the length of the array?a𝑎.

The second line of each test case contains?n𝑛?integers?a1,a2,…,an𝑎1,𝑎2,…,𝑎𝑛?(1≤ai≤1091≤𝑎𝑖≤109)?— the elements of the array?a𝑎.

It is guaranteed that the sum of?n𝑛?over all test cases does not exceed?105105.

Output

For each test case, output "Yes" if array?a𝑎?is beautiful, and output "No" otherwise.

You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

Example
input
Copy
 
4
3
7 3 8
5
7 1 9 3 5
5
4 12 2 6 3
5
7 49 9 3 1000000000
output
Copy
No
Yes
Yes
No
Note

In the first test case, any two numbers in the array are coprime, so the answer is "No".

In the second test case, we can pick?i=2𝑖=2?and?j=1𝑗=1. Since every number in the array is divisible by?ai=1𝑎𝑖=1, the answer is "Yes".

In the third test case, we can pick?i=3𝑖=3?and?j=5𝑗=5.?22?and?44?is divisible by?ai=2𝑎𝑖=2?while?33,?66?and?1212?is divisible by?aj=3𝑎𝑗=3, so the answer is "Yes".

題意:題目要求我們找到兩個數,使得整個數組的每個數都可以被兩個數至少一個整除。

思路:因為是整除關系,小數一定不能被大數整除,所以從最小數下手。

找到的第一個數一定是數組的最小數,第二個數為能被第一個數整除的最小數。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
void solve()
{ll n;cin>>n;vector<ll>a(n);for(ll &i:a){cin>>i;}if(n<=2){cout<<"Yes\n";return;}bool ans=true;sort(a.begin(),a.end());ll b,c;b=a[0];int f=0,f1=0;for(int i=1;i<n;i++){if(a[i]!=a[i-1]) {f=1;}if(a[i]%b!=0&&f==1){f1=1;c=a[i];break;}}if(f==0||f1==0){cout<<"Yes\n";return ;}for(ll i=2;i<n;i++){if(a[i]%b!=0&&a[i]%c!=0) {ans=false;break;}}cout<<(ans?"Yes\n":"No\n");
}
int main()
{int t;cin>>t;while(t--){solve();}return 0;
}

二.C. Chamo and Mocha's Array

C. Chamo and Mocha's Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mocha likes arrays, so before her departure, Chamo gave her an array?a𝑎?consisting of?n𝑛?positive integers as a gift.

Mocha doesn't like arrays containing different numbers, so Mocha decides to use magic to change the array. Mocha can perform the following three-step operation some (possibly, zero) times:

  1. Choose indices?l𝑙?and?r𝑟?(1≤l<r≤n1≤𝑙<𝑟≤𝑛)
  2. Let?x𝑥?be the median???of the subarray?[al,al+1,…,ar][𝑎𝑙,𝑎𝑙+1,…,𝑎𝑟]
  3. Set all values?al,al+1,…,ar𝑎𝑙,𝑎𝑙+1,…,𝑎𝑟?to?x𝑥

Suppose?a=[1,2,3,4,5]𝑎=[1,2,3,4,5]?initially:

  • If Mocha chooses?(l,r)=(3,4)(𝑙,𝑟)=(3,4)?in the first operation, then?x=3𝑥=3, the array will be changed into?a=[1,2,3,3,5]𝑎=[1,2,3,3,5].
  • If Mocha chooses?(l,r)=(1,3)(𝑙,𝑟)=(1,3)?in the first operation, then?x=2𝑥=2, the array will be changed into?a=[2,2,2,4,5]𝑎=[2,2,2,4,5].

Mocha will perform the operation until the array contains only the same number. Mocha wants to know what is the maximum possible value of this number.

???The median in an array?b𝑏?of length?m𝑚?is an element that occupies position number??m+12??𝑚+12??after we sort the elements in non-decreasing order. For example, the median of?[3,1,4,1,5][3,1,4,1,5]?is?33?and the median of?[5,25,20,24][5,25,20,24]?is?2020.

Input

Each test contains multiple test cases. The first line contains the number of test cases?t𝑡?(1≤t≤5001≤𝑡≤500). The description of the test cases follows.

The first line of each test case contains a single integer?n𝑛?(2≤n≤1052≤𝑛≤105)?— the length of the array?a𝑎.

The second line of each test case contains?n𝑛?integers?a1,a2,…,an𝑎1,𝑎2,…,𝑎𝑛?(1≤ai≤1091≤𝑎𝑖≤109)?— the elements of the array?a𝑎.

It is guaranteed that the sum of?n𝑛?over all test cases does not exceed?105105.

Output

For each test case, output the maximum value of the number.

Example
input
Copy
 
2
2
1 2
5
1 2 3 4 5
output
Copy
1
4
Note

In the first test case,?a=[1,2]𝑎=[1,2]. Mocha can only choose the interval?(l,r)=(1,2)(𝑙,𝑟)=(1,2). The array will be changed to?a=[1,1]𝑎=[1,1]. Therefore, the answer is?11.

In the second test case, Mocha can perform the following operations:

  • Choose the interval?(l,r)=(4,5)(𝑙,𝑟)=(4,5), then?a=[1,2,3,4,4]𝑎=[1,2,3,4,4].
  • Choose the interval?(l,r)=(3,5)(𝑙,𝑟)=(3,5), then?a=[1,2,4,4,4]𝑎=[1,2,4,4,4].
  • Choose the interval?(l,r)=(1,5)(𝑙,𝑟)=(1,5), then?a=[4,4,4,4,4]𝑎=[4,4,4,4,4].

The array contains only the same number, which is?44. It can be proven that the maximum value of the final number cannot be greater than?44.

題意:對于整個數組,我們可以選擇一個范圍(l,r),將這個范圍內的數全部變為這個范圍內的中位數,可以進行多次操作或者不做操作,求最后的數組的全部元素的最大值。

思路:因為當選擇的范圍長度是2時,就可以將這個范圍的兩個數全部變為這兩個數的較小值(x),那么一定存在一個長度為3的范圍內的中位數<=x。

如果整個數組的長度為2,輸出較小值即可,反之,遍歷數組全部長度為3的子數組,找到最大的中位數,然后輸出。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
void solve()
{int n;cin>>n;vector<int>a(n);for(int &i:a)cin>>i;if(n==2){cout<<min(a[0],a[1])<<"\n";return;	}ll ans=-1;for(int i=0;i<n-2;i++){int p[3];p[0]=a[i];p[1]=a[i+1];p[2]=a[i+2];sort(p,p+3);ans=max(ans,(ll)p[1]);}cout<<ans<<"\n";
}
int main()
{int t;cin>>t;while(t--){solve();}return 0;
}

三.B. Symmetric Encoding

B. 對稱編碼
每次測試的時間限制
為 2 秒
每次測試的內存限制
256 兆字節
輸入
標準輸入
輸出
標準輸出

Polycarp 有一根繩子s𝑠,由小寫的拉丁字母組成。他使用以下算法對此字符串進行編碼:

  • 首先,他構造了一個新的輔助弦r𝑟,它由字符串的所有不同字母組成s𝑠,按字母順序書寫;
  • 然后編碼如下:字符串中的每個字符s𝑠替換為字符串中的對稱字符r𝑟(字符串的第一個字符r𝑟將被最后一個替換,第二個被倒數第二個替換,依此類推)。

例如,對字符串進行編碼s𝑠="CodeForces“的發生方式如下:

  • 字符串r𝑟以“cdefors";
  • 第一個字符s1𝑠1='c' 替換為 的';
  • 第二個角色s2𝑠2='o' 替換為 'e';
  • 第三個角色s3𝑠3='d' 替換為 'r';
  • ...
  • 最后一個字符s10𝑠10='s“替換為”c“。

字符串r𝑟和替代品s𝑠="CodeForces“。

因此,對字符串進行編碼的結果s𝑠="codeforces“是字符串”serofedsoc“。

編寫一個執行解碼的程序,即恢復原始字符串s𝑠從編碼結果。

輸入

第一行包含單個整數t𝑡?(1≤噸≤1041≤𝑡≤104) — 測試用例的數量。

每個測試用例的第一行包含一個整數n𝑛?(1≤N≤2?1051≤𝑛≤2?105) — 字符串的長度b𝑏.

每個測試用例的第二行都包含一個字符串b𝑏長度n𝑛,由小寫拉丁字母組成 — 對原始字符串進行編碼的結果s𝑠.

可以保證n𝑛在測試中的所有測試用例不超過2?1052?105.

輸出

對于每個測試用例,輸出字符串s𝑠從中獲取編碼結果b𝑏已獲得。

輸入
復制
 
5
10
serofedsoc
3
ttf
9
tlrhgmaoi
1
w
15
hnndledmnhlttin
輸出
復制
codeforces
fft
algorithm
w
meetinthemiddle

#include<bits/stdc++.h>
using namespace std;
void solve()
{int n;cin>>n;string s,t,v;cin>>s;t=s;sort(t.begin(),t.end());for(int i=0;i<n;i++){if(t[i]!=t[i+1]) v+=t[i];}map<char,char>q;for(int i=0;i<v.size();i++){q[v[i]]=v[v.size()-1-i];}for(int i=0;i<n;i++){s[i]=q[s[i]];}cout<<s<<"\n";
}
int main()
{int t;cin>>t;while(t--){solve();}
}

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

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

相關文章

長難句5.30

Researchers measured people’s cortisol, which is a stress marker, while they were at work and while they were at home and found it higher at what is supposed to be a place of refuge. 研究人員測量了人們在工作中和在家里的皮質醇(壓力的一種標志)&#xff0c;結…

在 JavaScript 中循環遍歷數組的多種方法

在JavaScript編程中,遍歷數組是一個非常常見的操作。根據不同的需求和JavaScript的不同版本,我們有多種方法來完成這一操作。本文將介紹幾種有效的方法,包括現代的和傳統的方式,同時分析每一種方法的優缺點。 1. 使用 for...of 語法 for...of 是在 ECMAScript 2015(ES6)…

Spring Boot集成statemachine快速入門demo

1.什么是statemachine&#xff1f; Spring Statemachine 是應用程序開發人員在 Spring 應用程序中使用狀態機概念的框架&#xff0c;從設計層面分析&#xff1a;狀態機目的是解決復雜的狀態管理流程&#xff0c;保證程序單一原則和開閉原則&#xff1b;業務角度分析&#xff1…

【面試】什么是Java虛擬機

目錄 1. 說明2. 關鍵點 1. 說明 1.Java虛擬機&#xff08;Java Virtual Machine&#xff0c;簡稱JVM&#xff09;是運行所有Java程序的抽象計算機&#xff0c;是Java語言的運行環境。2.JVM是Java平臺無關性的關鍵&#xff0c;它允許Java程序在任何支持JVM的硬件和操作系統上運…

【大數據面試題】34 手寫一個 Flink SQL 樣例

一步一個腳印,一天一道大數據面試題 博主希望能夠得到大家的點贊收,藏支持!非常感謝~ 點贊,收藏是情分,不點是本分。祝你身體健康,事事順心! 我們來看看 Flink SQL大概流程和樣例: 流程: 1.創建 流處理環境 StreamExecutionEnvironment env 2.創建 表環境 StreamTab…

為啥裝了erlang,還報錯erl: command not found?

轉載說明&#xff1a;如果您喜歡這篇文章并打算轉載它&#xff0c;請私信作者取得授權。感謝您喜愛本文&#xff0c;請文明轉載&#xff0c;謝謝。 問題背景&#xff1a; 在一臺不通外網的服務器上裝rabbitmq&#xff0c;然后在啟動的時候&#xff0c;遇到了報錯 “/usr/lib/…

C#中使用Mapster

Mapster是一個開源的.NET對象映射庫&#xff0c;它提供了一種簡單而強大的方式來處理對象之間的映射。 多個映射框架的性能對比&#xff1a; 第一步安裝Mapster 使用方法 public class Test {public string name { get; set; }public string sex { get; set; }public string…

C語言數據結構(超詳細講解)| 二叉樹的實現

二叉樹 引言 在計算機科學中&#xff0c;數據結構是算法設計的基石&#xff0c;而二叉樹&#xff08;Binary Tree&#xff09;作為一種基礎且廣泛應用的數據結構&#xff0c;具有重要的地位。無論是在數據庫索引、內存管理&#xff0c;還是在編譯器實現中&#xff0c;二叉樹都…

記錄Win11安裝打印機驅動過程

1. 首先下載打印機對應型號的驅動 可以從這里下載&#xff1a;打印機驅動,打印機驅動下載 - 打印機驅動網 2. 下載 3. 打開控制面板-->設備和打印機 找到目標打印機添加設備即可 新增打印紙張尺寸

B站稿件生產平臺高可用建設分享

背景 B站作為國內領先的內容分享平臺&#xff0c;其核心功能之一便是支持UP主們創作并分享各類視頻內容。UP主稿件系統作為B站內容生產的關鍵環節&#xff0c;承擔著從內容創作到發布的全過程管理。為了滿足不同創作者的需求&#xff0c;B站提供了多種投稿渠道&#xff0c;包括…

方差分析的七種類型

方差分析&#xff08;ANOVA&#xff09;是一種用于檢驗兩個以上樣本均數差別的顯著性統計方法。根據不同的研究設計和數據類型&#xff0c;方差分析可以分為以下7種類型。 一、單因素方差分析 ①單因素方差分析說明 單因素方差分析用于研究一個定類數據&#xff08;自變量&am…

【原創教程】MES服務器與成品打標機控制說明

1 實現的功能及應用的場合 MES即制造執行系統(manufacturing execution system,簡稱MES),即在加強MRP計劃的執行功能,把MRP計劃同車間作業現場控制,通過執行系統聯系起來。 MES是一個生產管理智能化的一個系統,是用于生產時記錄數據、產量等信息的智能管理系統。 該項…

SpockMockStatic方法

SpockMockStatic方法 參考: https://blog.csdn.net/knighttools/article/details/44630975 ? static方法 import com.meituan.mafka.client.producer.IProducerProcessor; import com.meituan.mdp.langmodel.api.message.AssistantMessage; import com.sankuai.gaigc.arrang…

文件批量重命名001到100如何操作?這幾個文件改名小技巧學起來

文件批量重命名001到100怎么操作&#xff1f;作為打工一族&#xff0c;每天都需要跟很多文件打交道&#xff0c;有時文件太多了&#xff0c;查找起來像是大海撈針&#xff0c;特別是圖片文件。這個時候我們就需要對大量文件進行整理和排序&#xff0c;這樣有助于提高我們的工作…

微信小程序 自定義 tabBar

自定義 tabBar | 微信開放文檔 本文案例使用的Taro 非原生微信小程序 使用流程 1. 配置信息 在 app.json 中的 tabBar 項指定 custom 字段&#xff0c;同時其余 tabBar 相關配置也補充完整。所有 tab 頁的 json 里需聲明 usingComponents 項&#xff0c;也可以在 app.json 全局…

Java語言的應用場景

1、開發移動應用程序 例如&#xff1a;Android。 2、開發服務應用程序&#xff0c;搭建WEB界面。 例如&#xff1a;Servlet、JSP。 3、開發應用服務器。 例如Tomcat。 4、開發網絡通信程序。 5、開發圖形化界面桌面端。 Java支持用AWT、Swing、JavaFX三種包來開發圖形化界面…

電腦提示缺少vcruntime140_1.dll的解決方法,總結7種有效方法

vcruntime140_1.dll是Microsoft Visual C 2015運行時庫的一部分&#xff0c;它為使用Visual Studio 2015開發的應用程序提供了必要的運行時組件。該文件支持C程序的執行&#xff0c;包括內存管理、輸入輸出操作以及多線程功能等。缺失或損壞此文件可能導致應用程序無法啟動或運…

廣告聯盟四大家

國內四大廣告承接商&#xff1a;①抖音旗下-穿山甲②快手旗下-快手聯盟③百度旗下-百青藤④騰訊旗下-優量匯 我們目前在互聯網上能看到的所有廣告都是由他們發放的&#xff0c;在其中我們打小游戲復活看廣告&#xff0c;獲得道具看廣告&#xff0c;看劇看廣告&#xff0c;這…

數據庫的隔離級別和索引使用

先看一下隔離級別&#xff0c; 隔離級別首先要明確 &#xff0c;隔離的越重&#xff0c;那么自然會失去效率&#xff0c;為什么有這么多的隔離級別&#xff0c;其實就是平衡業務關系盡可能的提高效率。 下面看下隔離級別和介紹&#xff1a; 讀未提交是指&#xff1a;一個事務…

Oracle SQL詳解

Oracle SQL是一種用于管理和操作Oracle數據庫的編程語言。以下是一些基本的Oracle SQL語法和建表建用戶的詳解。 創建用戶 在Oracle中&#xff0c;創建用戶通常需要具有足夠權限的用戶&#xff08;通常是具有DBA角色的用戶&#xff09;。以下是一個創建用戶的例子&#xff1a;…