Educational Codeforces Round 159 (Rated for Div. 2) 之 A - E 题

2023-12-06 09:36

本文主要是介绍Educational Codeforces Round 159 (Rated for Div. 2) 之 A - E 题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • [A. Binary Imbalance](https://codeforces.com/contest/1902/problem/A)
    • Description
    • Solution
    • Code
  • [B. Getting Points](https://codeforces.com/contest/1902/problem/B)
    • Description
    • Solution
    • Code
  • [C. Insert and Equalize](https://codeforces.com/contest/1902/problem/C)
    • Description
    • Solution
    • Code
  • [D. Robot Queries](https://codeforces.com/contest/1902/problem/D)
    • Description
    • Solution
    • Code
  • [E. Collapsing Strings](https://codeforces.com/contest/1902/problem/E)
    • Description
    • Solution
    • Code
  • 视频题解

A. Binary Imbalance

Description

You are given a string s s s, consisting only of characters ‘0’ and/or ‘1’.

In one operation, you choose a position i i i from 1 1 1 to ∣ s ∣ − 1 |s| - 1 s1, where ∣ s ∣ |s| s is the current length of string s s s. Then you insert a character between the i i i-th and the ( i + 1 ) (i+1) (i+1)-st characters of s s s. If s i = s i + 1 s_i = s_{i+1} si=si+1, you insert ‘1’. If s i ≠ s i + 1 s_i \neq s_{i+1} si=si+1, you insert ‘0’.

Is it possible to make the number of zeroes in the string strictly greater than the number of ones, using any number of operations (possibly, none)?

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1t100) — the number of testcases.

The first line of each testcase contains an integer n n n ( 1 ≤ n ≤ 100 1 \le n \le 100 1n100).

The second line contains a string s s s of length exactly n n n, consisting only of characters ‘0’ and/or ‘1’.

Output

For each testcase, print “YES” if it’s possible to make the number of zeroes in s s s strictly greater than the number of ones, using any number of operations (possibly, none). Otherwise, print “NO”.

Example

input
3
2
00
2
11
2
10
output
YES
NO
YES

Note

In the first testcase, the number of zeroes is already greater than the number of ones.

In the second testcase, it’s impossible to insert any zeroes in the string.

In the third testcase, you can choose i = 1 i = 1 i=1 to insert a zero between the 1 1 1-st and the 2 2 2-nd characters. Since s 1 ≠ s 2 s_1 \neq s_2 s1=s2, you insert a ‘0’. The resulting string is “100”. It has two zeroes and only a single one, so the answer is “YES”.

Solution

具体见文后视频。


Code

#include <iostream>
#define int long longusing namespace std;typedef pair<int, int> PII;void solve()
{int N;string S;cin >> N >> S;S = ' ' + S;int Count = 0;for (int i = 1; i <= N; i ++){Count += (S[i] == '0');if (i < N && S[i] != S[i + 1]){cout << "YES" << endl;return;}}if (Count > (N - Count)) cout << "YES" << endl;else cout << "NO" << endl;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int Data;cin >> Data;while (Data --)solve();return 0;
}

B. Getting Points

Description

Monocarp is a student at Berland State University. Due to recent changes in the Berland education system, Monocarp has to study only one subject — programming.

The academic term consists of n n n days, and in order not to get expelled, Monocarp has to earn at least P P P points during those n n n days. There are two ways to earn points — completing practical tasks and attending lessons. For each practical task Monocarp fulfills, he earns t t t points, and for each lesson he attends, he earns l l l points.

Practical tasks are unlocked “each week” as the term goes on: the first task is unlocked on day 1 1 1 (and can be completed on any day from 1 1 1 to n n n), the second task is unlocked on day 8 8 8 (and can be completed on any day from 8 8 8 to n n n), the third task is unlocked on day 15 15 15, and so on.

Every day from 1 1 1 to n n n, there is a lesson which can be attended by Monocarp. And every day, Monocarp chooses whether to study or to rest the whole day. When Monocarp decides to study, he attends a lesson and can complete no more than 2 2 2 tasks, which are already unlocked and not completed yet. If Monocarp rests the whole day, he skips a lesson and ignores tasks.

Monocarp wants to have as many days off as possible, i. e. he wants to maximize the number of days he rests. Help him calculate the maximum number of days he can rest!

Input

The first line contains a single integer t c tc tc ( 1 ≤ t c ≤ 1 0 4 1 \le tc \le 10^4 1tc104) — the number of test cases. The description of the test cases follows.

The only line of each test case contains four integers n n n, P P P, l l l and t t t ( 1 ≤ n , l , t ≤ 1 0 9 1 \le n, l, t \le 10^9 1n,l,t109; 1 ≤ P ≤ 1 0 18 1 \le P \le 10^{18} 1P1018) — the number of days, the minimum total points Monocarp has to earn, the points for attending one lesson and points for completing one task.

It’s guaranteed for each test case that it’s possible not to be expelled if Monocarp will attend all lessons and will complete all tasks.

Output

For each test, print one integer — the maximum number of days Monocarp can rest without being expelled from University.

Example

input
5
1 5 5 2
14 3000000000 1000000000 500000000
100 20 1 10
8 120 10 20
42 280 13 37
output
0
12
99
0
37

Solution

具体见文后视频。


Code

#include <iostream>
#include <cmath>
#define int long longusing namespace std;typedef pair<int, int> PII;void solve()
{int N, P, L, T;cin >> N >> P >> L >> T;int Day = (N - 1) / 7 + 1;int Score = (Day / 2) * (L + 2 * T);if (Score >= P) cout << N - (int)ceil(P * 1.0 / (L + 2 * T)) << endl;else{if (Day & 1) Score += L + T;if (Score >= P) cout << N - (Day / 2 + 1) << endl;else{P -= Score;cout << N - (P + L - 1) / L - (Day / 2 + (Day & 1)) << endl;}}
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int Data;cin >> Data;while (Data --)solve();return 0;
}

C. Insert and Equalize

Description

You are given an integer array a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an, all its elements are distinct.

First, you are asked to insert one more integer a n + 1 a_{n+1} an+1 into this array. a n + 1 a_{n+1} an+1 should not be equal to any of a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an.

Then, you will have to make all elements of the array equal. At the start, you choose a positive integer x x x ( x > 0 x > 0 x>0). In one operation, you add x x x to exactly one element of the array. Note that x x x is the same for all operations.

What’s the smallest number of operations it can take you to make all elements equal, after you choose a n + 1 a_{n+1} an+1 and x x x?

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of testcases.

The first line of each testcase contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105).

The second line contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( − 1 0 9 ≤ a i ≤ 1 0 9 -10^9 \le a_i \le 10^9 109ai109). All a i a_i ai are distinct.

The sum of n n n over all testcases doesn’t exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.

Output

For each testcase, print a single integer — the smallest number of operations it can take you to make all elements equal, after you choose integers a n + 1 a_{n+1} an+1 and x x x.

Example

input
3
3
1 2 3
5
1 -19 17 -3 -15
1
10
output
6
27
1

Solution

具体见文后视频。


Code

#include <iostream>
#include <algorithm>
#define int long longusing namespace std;typedef pair<int, int> PII;const int SIZE = 2e5 + 10;int N;
int A[SIZE], Minus[SIZE];void solve()
{cin >> N;int Max = -1e18;for (int i = 1; i <= N; i ++)cin >> A[i], Max = max(A[i], Max);int X = 0;for (int i = 1; i <= N; i ++)X = __gcd(X, Max - A[i]);if (X == 0){cout << 1 << endl;return;}sort(A + 1, A + 1 + N);A[N + 1] = A[1] - X;for (int i = N - 1; i >= 1; i --)if (A[i + 1] - A[i] > X){A[N + 1] = A[i + 1] - X;break;}int Result = 0;for (int i = 1; i <= N + 1; i ++)Result += (Max - A[i]) / X;cout << Result << endl;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int Data;cin >> Data;while (Data --)solve();return 0;
}

D. Robot Queries

Description

There is an infinite 2 2 2-dimensional grid. Initially, a robot stands in the point ( 0 , 0 ) (0, 0) (0,0). The robot can execute four commands:

  • U — move from point ( x , y ) (x, y) (x,y) to ( x , y + 1 ) (x, y + 1) (x,y+1);
  • D — move from point ( x , y ) (x, y) (x,y) to ( x , y − 1 ) (x, y - 1) (x,y1);
  • L — move from point ( x , y ) (x, y) (x,y) to ( x − 1 , y ) (x - 1, y) (x1,y);
  • R — move from point ( x , y ) (x, y) (x,y) to ( x + 1 , y ) (x + 1, y) (x+1,y).

You are given a sequence of commands s s s of length n n n. Your task is to answer q q q independent queries: given four integers x x x, y y y, l l l and r r r; determine whether the robot visits the point ( x , y ) (x, y) (x,y), while executing a sequence s s s, but the substring from l l l to r r r is reversed (i. e. the robot performs commands in order s 1 s 2 s 3 … s l − 1 s r s r − 1 s r − 2 … s l s r + 1 s r + 2 … s n s_1 s_2 s_3 \dots s_{l-1} s_r s_{r-1} s_{r-2} \dots s_l s_{r+1} s_{r+2} \dots s_n s1s2s3sl1srsr1sr2slsr+1sr+2sn).

Input

The first line contains two integers n n n and q q q ( 1 ≤ n , q ≤ 2 ⋅ 1 0 5 1 \le n, q \le 2 \cdot 10^5 1n,q2105) — the length of the command sequence and the number of queries, respectively.

The second line contains a string s s s of length n n n, consisting of characters U, D, L and/or R.

Then q q q lines follow, the i i i-th of them contains four integers x i x_i xi, y i y_i yi, l i l_i li and r i r_i ri ( − n ≤ x i , y i ≤ n -n \le x_i, y_i \le n nxi,yin; 1 ≤ l ≤ r ≤ n 1 \le l \le r \le n 1lrn) describing the i i i-th query.

Output

For each query, print YES if the robot visits the point ( x , y ) (x, y) (x,y), while executing a sequence s s s, but the substring from l l l to r r r is reversed; otherwise print NO.

Example

input
8 3
RDLLUURU
-1 2 1 7
0 0 3 4
0 1 7 8
output
YES
YES
NO
input
4 2
RLDU
0 0 2 2
-1 -1 2 3
output
YES
NO
input
10 6
DLUDLRULLD
-1 0 1 10
-1 -2 2 5
-4 -2 6 10
-1 0 3 9
0 1 4 7
-3 -1 5 8
output
YES
YES
YES
NO
YES
YES

Solution

具体见文后视频。


Code

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#define int long longusing namespace std;typedef pair<int, int> PII;int N, Q;
string S;
std::map<char, int> dx, dy;
std::map<PII, int> Exist1, Exist2;
std::vector<PII> Point1, Point2;
std::map<PII, vector<int>> Id1, Id2;bool Check(int X, int Y, int L, int R)
{auto P1 = Point1[L - 1];auto P2 = Point2[N - R];X += P2.first - P1.first;Y += P2.second - P1.second;int P = lower_bound(Id2[{X, Y}].begin(), Id2[{X, Y}].end(), N - R) - Id2[{X, Y}].begin();if (Exist2.count({X, Y}) && Id2[{X, Y}][P] >= N - R + 1 && Id2[{X, Y}][P] <= N - L + 1) return 1;return 0;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);dy['U'] = 1, dy['D'] = -1, dy['L'] = 0, dy['R'] = 0;dx['U'] = 0, dx['D'] = 0, dx['L'] = -1, dx['R'] = 1;cin >> N >> Q >> S;string T = S;S = ' ' + S;reverse(T.begin(), T.end());T = ' ' + T;int X1 = 0, Y1 = 0, X2 = 0, Y2 = 0;Exist1[{X1, Y1}] = Exist2[{X2, Y2}] = 1;Id1[{X1, Y1}].push_back(0), Id2[{X2, Y2}].push_back(0);Point1.push_back({X1, Y1}), Point2.push_back({X2, Y2});for (int i = 1; i <= N; i ++){X1 += dx[S[i]], Y1 += dy[S[i]];X2 += dx[T[i]], Y2 += dy[T[i]];Id1[{X1, Y1}].push_back(i);Id2[{X2, Y2}].push_back(i);Exist1[{X1, Y1}] = Exist2[{X2, Y2}] = 1;Point1.push_back({X1, Y1});Point2.push_back({X2, Y2});}// cout << "Check:";// for (auto c : Id2[{-2, -1}])// 	cout << c << ' ';// cout << endl;while (Q --){int X, Y, L, R;cin >> X >> Y >> L >> R;if (Exist1.count({X, Y}) && *Id1[{X, Y}].begin() < L) cout << "YES" << endl;else if (Exist1.count({X, Y}) && Id1[{X, Y}].back() > R) cout << "YES" << endl;else if (Check(X, Y, L, R)) cout << "YES" << endl;else cout << "NO" << endl;}return 0;
}

E. Collapsing Strings

Description

You are given n n n strings s 1 , s 2 , … , s n s_1, s_2, \dots, s_n s1,s2,,sn, consisting of lowercase Latin letters. Let ∣ x ∣ |x| x be the length of string x x x.

Let a collapse C ( a , b ) C(a, b) C(a,b) of two strings a a a and b b b be the following operation:

  • if a a a is empty, C ( a , b ) = b C(a, b) = b C(a,b)=b;
  • if b b b is empty, C ( a , b ) = a C(a, b) = a C(a,b)=a;
  • if the last letter of a a a is equal to the first letter of b b b, then C ( a , b ) = C ( a 1 , ∣ a ∣ − 1 , b 2 , ∣ b ∣ ) C(a, b) = C(a_{1,|a|-1}, b_{2,|b|}) C(a,b)=C(a1,a1,b2,b), where s l , r s_{l,r} sl,r is the substring of s s s from the l l l-th letter to the r r r-th one;
  • otherwise, C ( a , b ) = a + b C(a, b) = a + b C(a,b)=a+b, i. e. the concatenation of two strings.

Calculate ∑ i = 1 n ∑ j = 1 n ∣ C ( s i , s j ) ∣ \sum\limits_{i=1}^n \sum\limits_{j=1}^n |C(s_i, s_j)| i=1nj=1nC(si,sj).

Input

The first line contains a single integer n n n ( 1 ≤ n ≤ 1 0 6 1 \le n \le 10^6 1n106).

Each of the next n n n lines contains a string s i s_i si ( 1 ≤ ∣ s i ∣ ≤ 1 0 6 1 \le |s_i| \le 10^6 1si106), consisting of lowercase Latin letters.

The total length of the strings doesn’t exceed 1 0 6 10^6 106.

Output

Print a single integer — ∑ i = 1 n ∑ j = 1 n ∣ C ( s i , s j ) ∣ \sum\limits_{i=1}^n \sum\limits_{j=1}^n |C(s_i, s_j)| i=1nj=1nC(si,sj).

Example

input
3
aba
ab
ba
output
20
input
5
abab
babx
xab
xba
bab
output
126

Solution

具体见文后视频。


Code

#include <iostream>
#include <vector>
#include <algorithm>
#define int long longusing namespace std;typedef pair<int, int> PII;const int SIZE = 1e6 + 10;int N;
string S[SIZE];
int Trie[SIZE][26], Count[SIZE], idx = 0;
int Point[SIZE], k;void Insert(string S)
{int p = 0;for (int i = 0; i < S.size(); i ++){int c = S[i] - 'a';if (!Trie[p][c]) Trie[p][c] = ++ idx;p = Trie[p][c];Count[p] ++;}
}int Query(string S)
{int p = 0, Depth = 0, Cnt = 0;k = 0;for (int i = 0; i < S.size(); i ++){int c = S[i] - 'a';if (!Trie[p][c]) break;p = Trie[p][c];Point[ ++ k] = p;}int Result = 0, Max = 0;for (int i = k; i >= 1; i --){Result += (min((int)S.size(), i) * 2) * (Count[Point[i]] - Max);Max = Count[Point[i]];}return Result;
}signed main()
{cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> N;int Len = 0;for (int i = 1; i <= N; i ++){cin >> S[i];string Tmp = S[i];reverse(Tmp.begin(), Tmp.end());Insert(Tmp);Len += (int)S[i].size();}int Result = 0;for (int i = 1; i <= N; i ++){int Minus = Query(S[i]);Result += Len + (int)S[i].size() * N - Minus;}cout << Result << endl;return 0;
}

视频题解

Educational Codeforces Round 159 (Rated for Div. 2) 之 A-E题


最后祝大家早日在这里插入图片描述

这篇关于Educational Codeforces Round 159 (Rated for Div. 2) 之 A - E 题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/461360

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

创建一个大的DIV,里面的包含两个DIV是可以自由移动

创建一个大的DIV,里面的包含两个DIV是可以自由移动 <body>         <div style="position: relative; background:#DDF8CF;line-height: 50px"> <div style="text-align: center; width: 100%;padding-top: 0px;"><h3>定&nbsp;位&nbsp;

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

CF#271 (Div. 2) D.(dp)

D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/474/problem/D We s

CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)

B. Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/B There