文章目錄
- A I'm a teapot
- B You're a teapot
- C Flush
- D XNOR Operation
- E Trapezium
- F We're teapots
- G Binary Operation
AtCoder Beginner Contest 418
A I’m a teapot
Takahashi is a teapot.
Since he is a teapot, he will gladly accept tea, but will refuse any other liquid.
Determine whether you can pour a liquid named SSS into him.
You are given a string SSS of length NNN consisting of lowercase English letters.
Determine whether SSS is a string that ends with tea
.
Constraints
- 1≤N≤201 \leq N \leq 201≤N≤20
- NNN is an integer.
- SSS is a string of length NNN consisting of lowercase English letters.
翻譯
高橋是一個茶壺。
既然他是茶壺,他就會欣然接受茶水,而拒絕其他任何液體。
確定能否向它倒入名為 SSS 的液體。
給你一個長度為 NNN 的字符串 SSS ,由小寫英文字母組成。
請判斷 SSS 是否是以 tea
結尾的字符串。
約束
- 1≤N≤201 \leq N \leq 201≤N≤20
- NNN 是整數。
- SSS 是長度為 NNN 的字符串,由小寫英文字母組成。
分析:判斷字符的結尾是不是 tea
即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, INF = 0x3f3f3f3f;void solve() {int n; string s; cin >> n >> s;bool f = (n > 2 && s.substr(n - 3, 3) == "tea");cout << (f ? "Yes" : "No") << "\n";
}
int main() {// freopen("1.in", "r", stdin);int T = 1; while (T--) solve();return 0;
}
B You’re a teapot
I begin with T and end with T, and I am full of T. What am I?
For a string ttt, define the filling rate as follows:
- If the first and last characters of ttt are both
t
and ∣t∣≥3|t| \geq 3∣t∣≥3: Let xxx be the number oft
in ttt. Then the filling rate of ttt is x?2∣t∣?2\displaystyle\frac{x-2}{|t|-2}∣t∣?2x?2?, where ∣t∣|t|∣t∣ denotes the length of ttt. - Otherwise: the filling rate of ttt is 000.
You are given a string SSS. Find the maximum possible filling rate of a substring of SSS.
What is a substring?
A substring of SSS is a string obtained by removing zero or more characters from the beginning and the end of SSS. For example,ab
,bc
, andbcd
are substrings ofabcd
, whileac
,dc
, ande
are not substrings ofabcd
.
Constraints
- 1≤∣S∣≤1001 \leq |S| \leq 1001≤∣S∣≤100
- SSS is a string consisting of lowercase English letters.
翻譯
我是什么?
對于一個字符串 ttt ,定義填充率如下:
- 如果 ttt 的第一個字符和最后一個字符都是 "T "和 ∣t∣≥3|t| \geq 3∣t∣≥3 :設 xxx 是 ttt 中 "t "的個數。那么 ttt 的填充率為 x?2∣t∣?2\displaystyle\frac{x-2}{|t|-2}∣t∣?2x?2? ,其中 ∣t∣|t|∣t∣ 表示 ttt 的長度。
- 否則: ttt 的填充率為 000 。
給你一個字符串 SSS 。求 SSS 子串的最大填充率。
什么是子串?
SSS 的子串是指從 SSS 的開頭和結尾刪除零個或多個字符后得到的字符串。例如,ab
、bc
和bcd
是abcd
的子串,而ac
、dc
和e
不是abcd
的子串。
約束
- 1≤∣S∣≤1001 \leq |S| \leq 1001≤∣S∣≤100
- SSS 是一個由小寫英文字母組成的字符串。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, INF = 0x3f3f3f3f;void solve() {string s; cin >> s;int x = 0, n = s.size();double ans = 0;for (int i = 0; i < n; i++)for (int j = i + 1; j < n; j++)if (s[i] == 't' && s[j] == 't') {int x = 0, len = j - i + 1;for (int k = i; k <= j; k++)x += (s[k] == 't');ans = max(ans, (x - 2.0) / (len - 2.0));}cout << fixed << setprecision(12) << ans << "\n";
}
int main() {// freopen("1.in", "r", stdin);int T = 1; while (T--) solve();return 0;
}
C Flush
On the poker table, there are tea bags of NNN different flavors. The flavors are numbered from 1 through NNN, and there are AiA_iAi? tea bags of flavor iii (1≤i≤N1 \leq i \leq N1≤i≤N).
You will play a game using these tea bags. The game has a parameter called difficulty between 1 and A1+?+ANA_1 + \cdots + A_NA1?+?+AN?, inclusive. A game of difficulty bbb proceeds as follows:
- You declare an integer xxx. Here, it must satisfy b≤x≤A1+?+ANb \leq x \leq A_1 + \cdots + A_Nb≤x≤A1?+?+AN?.
- The dealer chooses exactly xxx tea bags from among those on the table and gives them to you.
- You check the flavors of the xxx tea bags you received, and choose bbb tea bags from them.
- If all bbb tea bags you chose are of the same flavor, you win. Otherwise, you lose.
The dealer will do their best to make you lose.
You are given QQQ queries, so answer each of them. The jjj-th query is as follows:
- For a game of difficulty BjB_jBj?, report the minimum integer xxx you must declare at the start to guarantee a win. If it is impossible to win, report ?1-1?1 instead.
Constraints
- 1≤N≤3×1051 \leq N \leq 3 \times 10^51≤N≤3×105
- 1≤Q≤3×1051 \leq Q \leq 3 \times 10^51≤Q≤3×105
- 1≤Ai≤1061 \leq A_i \leq 10^61≤Ai?≤106 (1≤i≤N1 \leq i \leq N1≤i≤N)
- 1≤Bj≤min?(109,A1+?+AN)1 \leq B_j \leq \min(10^9, A_1 + \cdots + A_N)1≤Bj?≤min(109,A1?+?+AN?) (1≤j≤Q1 \leq j \leq Q1≤j≤Q)
- All input values are integers.
翻譯:
在撲克桌上,有不同口味的茶包。口味從 111 到 NNN 編號,有 AiA_iAi? 個茶包口味 iii(1≤i≤N1\leq i\leq N1≤i≤N)。
你將用這些茶包玩游戲。游戲有一個名為難度的參數,介于 111 和 a1+?+aNa_1+\cdots+a_Na1?+?+aN? 之間。難度游戲 bbb 的收益如下:
- 聲明一個整數 xxx。這里,它必須滿足 b≤x≤A1+?+ANb\leq x\leq A_1+\cdots+A_Nb≤x≤A1?+?+AN?。
- 經銷商從桌上的茶包中準確地選擇 xxx 個茶包,并將其送給您。
- 您檢查收到的 xxx 個茶包,并從中選擇 bbb 個茶包。
- 如果你選擇的 bbb 個茶包都是相同的味道,你就贏了。否則,你輸了。
經銷商會盡最大努力讓你輸。
您會收到 QQQ 的查詢,請逐一回答。第 jjj 個查詢如下:
- 對于難度為 BjB_jBj? 的游戲,報告您必須在開始時聲明的最小整數 xxx,以確保獲勝。如果不可能獲勝,則報告 ?1-1?1。
約束條件
- 1≤N≤3×1051 \leq N \leq 3 \times 10^51≤N≤3×105
- 1≤Q≤3×1051 \leq Q \leq 3 \times 10^51≤Q≤3×105
- 1≤Ai≤1061 \leq A_i \leq 10^61≤Ai?≤106 (1≤i≤N1 \leq i \leq N1≤i≤N)
- 1≤Bj≤min?(109,A1+?+AN)1 \leq B_j \leq \min(10^9, A_1 + \cdots + A_N)1≤Bj?≤min(109,A1?+?+AN?) (1≤j≤Q1 \leq j \leq Q1≤j≤Q)
- 所有輸入值都是整數。
分析:
本題目要模擬樣例,找到題目所求:當相同元素的數量至少為 bbb 的需要最少有解數量 xxx。
解法:考慮對原數組 aia_iai? 排序,求出前綴和 sis_isi?,二分查詢第一個≥b\ge b≥b 的元素位置 ididid,如果答案存在,則 ans=sid?1+(b?1)×(n?id+1)+1ans=s_{id-1} + (b-1) \times (n-id+1) +1ans=sid?1?+(b?1)×(n?id+1)+1;否則 ans=?1ans=-1ans=?1。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, INF = 0x3f3f3f3f;
int n, q, a[N], b;
ll s[N];void solve() {cin >> n >> q;for (int i = 1; i <= n; i++) cin >> a[i];sort(a + 1, a + 1 + n);for (int i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];while (q--) {cin >> b;int id = lower_bound(a + 1, a + 1 + n, b) - a;ll ans = -1;if (id <= n && a[id] >= b)ans = s[id - 1] + 1ll * (b - 1) * (n - id + 1) + 1;cout << ans << "\n";}
}
int main() {// freopen("1.in", "r", stdin);int T = 1; while (T--) solve();return 0;
}
D XNOR Operation
This problem is a subproblem of Problem G.
A non-empty string SSS consisting of 0
and 1
is called a beautiful string when it satisfies the following condition:
- (Condition) You can perform the following sequence of operations until the length of SSS becomes 111 and make the only character remaining in SSS be
1
.- Choose any integer iii satisfying 1≤i≤∣S∣?11 \leq i \leq |S| - 11≤i≤∣S∣?1.
- Define an integer xxx as follows:
- If Si=S_i =Si?=
0
and Si+1=S_{i+1} =Si+1?=0
, let x=1x = 1x=1. - If Si=S_i =Si?=
0
and Si+1=S_{i+1} =Si+1?=1
, let x=0x = 0x=0. - If Si=S_i =Si?=
1
and Si+1=S_{i+1} =Si+1?=0
, let x=0x = 0x=0. - If Si=S_i =Si?=
1
and Si+1=S_{i+1} =Si+1?=1
, let x=1x = 1x=1.
- If Si=S_i =Si?=
- Remove SiS_iSi? and Si+1S_{i+1}Si+1?, and insert the digit corresponding to xxx in their place.
For example, if S=S=S=10101
and you choose i=2i=2i=2, the string after the operation is1001
.
You are given a string TTT of length NNN consisting of 0
and 1
.
Find the number of beautiful strings that are substrings of TTT. Even if two substrings are identical as strings, count them separately if they are taken from different positions.
What are substrings? A substring of SSS is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of SSS.
For example, 10
is a substring of 101
, but 11
is not a substring of 101
.
Constraints
- 1≤N≤2×1051 \leq N \leq 2 \times 10^51≤N≤2×105
- NNN is an integer.
- TTT is a string of length NNN consisting of
0
and1
.
翻譯:
本問題是問題 G 的子問題。
由 0
和 1
組成的非空字符串 SSS 滿足以下條件時,稱為優美字符串:
- 條件)你可以執行以下一系列操作,直到 SSS 的長度變為 111 ,并使 SSS 中唯一剩下的字符是
1
。- 選擇滿足 1≤i≤∣S∣?11 \leq i \leq |S| - 11≤i≤∣S∣?1 的任意整數 iii 。
- 定義整數 xxx 如下:
- 如果 Si=S_i =Si?=
0
且 {98421818}0和 $S_{i+1} =$ 0
,設 x=1x = 1x=1 . - 若 Si=S_i =Si?=
0
且 {56774510}0
, 則設 x=1x = 1x=1 .0和 $S_{i+1} =$ 1
,則 x=0x = 0x=0 . - 若 Si=S_i =Si?=
1
,且 Si+1=S_{i+1} =Si+1?=0
,則讓 {12243413}.0",則 x=0x = 0x=0 . - 如果 Si=S_i =Si?=
1
和 {1848987}0
, 讓 {297737}.1且 $S_{i+1} =$
1`,則設 x=1x = 1x=1 。
- 如果 Si=S_i =Si?=
- 刪除 SiS_iSi? 和 Si+1S_{i+1}Si+1? ,并插入與 xxx 相對應的數字。
例如,如果 S=S=S= 10101",而您選擇了 i=2i=2i=2 ,則操作后的字符串為 “1001”。
給定長度為 NNN 的字符串 TTT 由 0
和 1
組成。
求作為 TTT 的子串的美麗字符串的個數。即使兩個子串是相同的字符串,如果它們取自不同的位置,也要分別計算。
什么是子串? SSS 的子串是刪除 SSS 開頭的零個或多個字符和結尾的零個或多個字符后得到的字符串。
例如,10
是101
的子串,但11
不是101
的子串。
約束
- 1≤N≤2×1051 \leq N \leq 2 \times 10^51≤N≤2×105
- NNN 是整數。
- TTT 是長度為 NNN 的字符串,由
0
和1
組成。
分析:
E Trapezium
There are NNN points on a two-dimensional plane, with the iii-th point at coordinates (Xi,Yi)(X_i, Y_i)(Xi?,Yi?). It is guaranteed that no two points are at the same position, and no three points are collinear.
Among the combinations of four points from these points, how many combinations can form a trapezoid as a polygon with those four points as vertices?
Constraints
- 4≤N≤20004 \leq N \leq 2\,0004≤N≤2000
- 0≤Xi,Yi≤1070 \leq X_i, Y_i \leq 10^70≤Xi?,Yi?≤107 (1≤i≤N1 \leq i \leq N1≤i≤N)
- No two points are at the same location.
- No three points are collinear.
- All input values are integers.
翻譯:
在一個二維平面上有 NNN 個點,其中第 iii 個點的坐標為 (Xi,Yi)(X_i, Y_i)(Xi?,Yi?) 。保證沒有兩個點在同一位置,也沒有三個點是共線的。
在這些點的四點組合中,以這四點為頂點能組成梯形多邊形的組合有多少種?
約束
- 4≤N≤20004 \leq N \leq 2\,0004≤N≤2000
- 0≤Xi,Yi≤1070 \leq X_i, Y_i \leq 10^70≤Xi?,Yi?≤107 ( 1≤i≤N1 \leq i \leq N1≤i≤N )
- 沒有兩個點在同一位置。
- 沒有三個點是相鄰的。
- 所有輸入值均為整數。
分析:
F We’re teapots
There are NNN teapots arranged in a row, numbered from 111 to NNN from left to right.
There is a sequence of integers (a1,…,aN)(a_1, \dots, a_N)(a1?,…,aN?), initially with values a1=?=aN=?1a_1 = \dots = a_N = -1a1?=?=aN?=?1.
You will fill each teapot with either tea or coffee so that the following conditions are all satisfied:
- For any two adjacent teapots, at least one of them contains tea.
- For any integer iii satisfying 1≤i≤N1 \leq i \leq N1≤i≤N, if ai≠?1a_i \neq -1ai?=?1, then exactly aia_iai? of teapots 1,…,i1, \dots, i1,…,i contain coffee.
You are given QQQ queries, which you should process in the given order.
The jjj-th query (1≤j≤Q1 \leq j \leq Q1≤j≤Q) is as follows:
- Change the value of aXja_{X_j}aXj?? to YjY_jYj?. Then, print the number, modulo 998244353998244353998244353, of ways to fill the teapots satisfying the conditions.
Constraints
- 2≤N≤2×1052 \leq N \leq 2 \times 10^52≤N≤2×105
- 1≤Q≤2×1051 \leq Q \leq 2 \times 10^51≤Q≤2×105
- 1≤Xj≤N1 \leq X_j \leq N1≤Xj?≤N (1≤j≤Q1 \leq j \leq Q1≤j≤Q)
- ?1≤Yj≤Xj-1 \leq Y_j \leq X_j?1≤Yj?≤Xj? (1≤j≤Q1 \leq j \leq Q1≤j≤Q)
- All input values are integers.
翻譯
有 NNN 個茶壺排成一排,從左到右依次編號為 111 至 NNN 。
有一串整數 (a1,…,aN)(a_1, \dots, a_N)(a1?,…,aN?) ,最初的值為 a1=?=aN=?1a_1 = \dots = a_N = -1a1?=?=aN?=?1 。
你要在每個茶壺中注入茶或咖啡,以滿足以下所有條件:
- 對于任意兩個相鄰的茶壺,其中至少有一個裝有茶葉。
- 對于滿足 1≤i≤N1 \leq i \leq N1≤i≤N 的任意整數 iii ,如果有 ai≠?1a_i \neq -1ai?=?1 ,那么在 1,…,i1, \dots, i1,…,i 的茶壺中,正好有 aia_iai? 個茶壺裝有咖啡。
給你 QQQ 個查詢,你應按給定的順序處理這些查詢。
jjj -th 查詢( 1≤j≤Q1 \leq j \leq Q1≤j≤Q )如下:
- 將 aXja_{X_j}aXj?? 的值改為 YjY_jYj? 。然后,打印出滿足條件的茶壺的裝水方式的數量,模數為 998244353998244353998244353 。
限制因素
- 2≤N≤2×1052 \leq N \leq 2 \times 10^52≤N≤2×105
- 1≤Q≤2×1051 \leq Q \leq 2 \times 10^51≤Q≤2×105
- 1≤Xj≤N1 \leq X_j \leq N1≤Xj?≤N ( 1≤j≤Q1 \leq j \leq Q1≤j≤Q )
- ?1≤Yj≤Xj-1 \leq Y_j \leq X_j?1≤Yj?≤Xj? ( 1≤j≤Q1 \leq j \leq Q1≤j≤Q )
- 所有輸入值均為整數。
分析:
G Binary Operation
There are 161616 integer tuples (A,B,C,D)(A, B, C, D)(A,B,C,D) satisfying A,B,C,D∈{0,1}A, B, C, D \in \lbrace 0, 1 \rbraceA,B,C,D∈{0,1}. For each of them, solve the following problem.
A non-empty string SSS consisting of
0
and1
is called a beautiful string when it satisfies the following condition:
- (Condition) You can perform the following sequence of operations until the length of SSS becomes 111 and make the only character remaining in SSS be
1
.
- Choose any integer iii satisfying 1≤i≤∣S∣?11 \leq i \leq |S| - 11≤i≤∣S∣?1.
- Define an integer xxx as follows:
- If Si=S_i =Si?=
0
and Si+1=S_{i+1} =Si+1?=0
, let x=Ax = Ax=A.- If Si=S_i =Si?=
0
and Si+1=S_{i+1} =Si+1?=1
, let x=Bx = Bx=B.- If Si=S_i =Si?=
1
and Si+1=S_{i+1} =Si+1?=0
, let x=Cx = Cx=C.- If Si=S_i =Si?=
1
and Si+1=S_{i+1} =Si+1?=1
, let x=Dx = Dx=D.- Remove SiS_iSi? and Si+1S_{i+1}Si+1?, and insert the digit corresponding to xxx in their place.
For example, if S=S=S=10101
and you choose i=2i=2i=2, the string after the operation is1001
if B=0B=0B=0, and1101
if B=1B=1B=1.You are given a string TTT of length NNN consisting of
0
and1
.
- Let LLL be the length of the longest beautiful string that is a substring of TTT (if no substring of TTT is a beautiful string, let L=?1L = -1L=?1),
- Let MMM be the number of beautiful strings that are substrings of TTT.
Find LLL and MMM. Even if two substrings are identical as strings, count them separately if they are taken from different positions.
What are substrings?
A substring of SSS is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of SSS.
For example,10
is a substring of101
, but11
is not a substring of101
.
Constraints
- 1≤N≤2×1051 \leq N \leq 2 \times 10^51≤N≤2×105
- NNN is an integer.
- TTT is a string of length NNN consisting of
0
and1
.
翻譯
有 161616 個整數元組 (A,B,C,D)(A, B, C, D)(A,B,C,D) 滿足 A,B,C,D∈{0,1}A, B, C, D \in \lbrace 0, 1 \rbraceA,B,C,D∈{0,1} 。請分別求解下列問題。
由 "0 "和 "1 "組成的非空字符串 SSS 滿足以下條件時,稱為優美字符串:
- (條件)你可以執行下面的操作序列,直到 SSS 的長度變為 111 ,并使 SSS 中唯一剩下的字符是
1
。
- 選擇滿足 1≤i≤∣S∣?11 \leq i \leq |S| - 11≤i≤∣S∣?1 的任意整數 iii 。
- 定義整數 xxx 如下:
- 如果 Si=S_i =Si?=
0
和 {53892861}0
。0和 $S_{i+1} =$ 0
,則設 x=Ax = Ax=A .- 如果 Si=S_i =Si?=
0
且 {346645525}0
, 則讓 x=Ax = Ax=A .0 "和 Si+1=S_{i+1} =Si+1?= “1”,設為 x=Bx = Bx=B 。- 如果 Si=S_i =Si?=
1
和 {3676760}1
,令 x=Bx = Bx=B .1 “和 Si+1=S_{i+1} =Si+1?= 0”,則 x=Cx = Cx=C .- 如果 Si=S_i =Si?=
1
和 {66227549}0
,讓 x=Cx = Cx=C .1和 $S_{i+1} =$
1.如果 $S_i =$
1和 $S_{i+1} =$
1`, 則讓 x=Dx = Dx=D .
- 刪除 SiS_iSi? 和 Si+1S_{i+1}Si+1? ,并插入與 xxx 相對應的數字。
例如,如果 S=S=S= 10101",并選擇 i=2i=2i=2 ,則操作后的字符串如果是 B=0B=0B=0 則為 “1001”,如果是 B=1B=1B=1 則為 “1101”。給出長度為 NNN 的字符串 TTT ,它由
0
和1
組成。
- 設 LLL 是長度為 TTT 的子串的最長優美字符串(如果 TTT 的子串都不是優美字符串,則設為 L=?1L = -1L=?1 )、
- 設 MMM 是作為 TTT 子串的優美字符串的個數。
找出 LLL 和 MMM 。即使兩個子串是相同的字符串,如果它們取自不同的位置,也要分別計算。
什么是子串?
SSS 的子串是刪除 SSS 開頭的零個或多個字符和結尾的零個或多個字符后得到的字符串。
例如,10
是101
的子串,但11
不是101
的子串。
約束
- 1≤N≤2×1051 \leq N \leq 2 \times 10^51≤N≤2×105
- NNN 是整數。
- TTT 是長度為 NNN 的字符串,由
0
和1
組成。
分析: