A. Reverse a Substring
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a string ss consisting of nn lowercase Latin letters.
Let's define a substring as a contiguous subsegment of a string. For example, "acab" is a substring of "abacaba" (it starts in position 33and ends in position 66), but "aa" or "d" aren't substrings of this string. So the substring of the string ss from position ll to position rr is s[l;r]=slsl+1…srs[l;r]=slsl+1…sr.
You have to choose exactly one of the substrings of the given string and reverse it (i. e. make s[l;r]=srsr−1…sls[l;r]=srsr−1…sl) to obtain a string that is less lexicographically. Note that it is not necessary to obtain the minimum possible string.
If it is impossible to reverse some substring of the given string to obtain a string that is less, print "NO". Otherwise print "YES" and anysuitable substring.
String xx is lexicographically less than string yy, if either xx is a prefix of yy (and x≠yx≠y), or there exists such ii (1≤i≤min(|x|,|y|)1≤i≤min(|x|,|y|)), that xi<yixi<yi, and for any jj (1≤j<i1≤j<i) xj=yjxj=yj. Here |a||a| denotes the length of the string aa. The lexicographic comparison of strings is implemented by operator < in modern programming languages.
Input
The first line of the input contains one integer nn (2≤n≤3⋅1052≤n≤3⋅105) — the length of ss.
The second line of the input contains the string ss of length nn consisting only of lowercase Latin letters.
Output
If it is impossible to reverse some substring of the given string to obtain a string which is lexicographically less, print "NO". Otherwise print "YES" and two indices ll and rr (1≤l<r≤n1≤l<r≤n) denoting the substring you have to reverse. If there are multiple answers, you can print any.
Examples
input
Copy
7 abacaba
output
Copy
YES 2 5
input
Copy
6 aabcfg
output
Copy
NO
Note
In the first testcase the resulting string is "aacabba".
一个字符串,找出来翻转任意子串,可以让字符串的字典序变小的一段区间
思路:只要找小于的位置就可以了
int main() {
ios::sync_with_stdio(false);
string str;
while(cin >> n >> str){
for(int i = 0; i < str.size() - 1;i++){
if(str[i] > str[i + 1]){
cout << "YES" << endl;
cout << i + 1 << " " << i + 2 << endl;
return 0;
}
}
cout << "NO" << endl;
}
}
B. Game with Telephone Numbers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
A telephone number is a sequence of exactly 1111 digits such that its first digit is 8.
Vasya and Petya are playing a game. Initially they have a string ss of length nn (nn is odd) consisting of digits. Vasya makes the first move, then players alternate turns. In one move the player must choose a character and erase it from the current string. For example, if the current string 1121, after the player's move it may be 112, 111 or 121. The game ends when the length of string ss becomes 11. If the resulting string is a telephone number, Vasya wins, otherwise Petya wins.
You have to determine if Vasya has a winning strategy (that is, if Vasya can win the game no matter which characters Petya chooses during his moves).
Input
The first line contains one integer nn (13≤n<10513≤n<105, nn is odd) — the length of string ss.
The second line contains the string ss (|s|=n|s|=n) consisting only of decimal digits.
Output
If Vasya has a strategy that guarantees him victory, print YES.
Otherwise print NO.
Examples
input
Copy
13 8380011223344
output
Copy
YES
input
Copy
15 807345619350641
output
Copy
NO
Note
In the first example Vasya needs to erase the second character. Then Petya cannot erase a character from the remaining string 880011223344 so that it does not become a telephone number.
In the second example after Vasya's turn Petya can erase one character character 8. The resulting string can't be a telephone number, because there is no digit 8 at all.
v和p玩游戏,v先手,p后手,给定字符串,两人游戏轮流删除任意位置一个字符,谁先让字符串长度为11且第一个是8即获胜。问先手是否必胜
先除去后10位,然后记录8的个数,再做差,判断一下就可以了
int main() {
ios::sync_with_stdio(false);
string str;
while(cin >> n >> str){
int flag = 0,cnt = 0;;
for(int i = 0;i <= n - 11;i++){
if(str[i] == '8'){
cnt++;
}
}
int cha = n - 11 + 1 - cnt;
if(cnt > cha){
cout << "YES" << endl;
}else{
cout << "NO" << endl;
}
}
}
C. Alarm Clocks Everywhere
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Ivan is going to sleep now and wants to set his alarm clock. There will be many necessary events tomorrow, the ii-th of them will start during the xixi-th minute. Ivan doesn't want to skip any of the events, so he has to set his alarm clock in such a way that it rings during minutes x1,x2,…,xnx1,x2,…,xn, so he will be awake during each of these minutes (note that it does not matter if his alarm clock will ring during any other minute).
Ivan can choose two properties for the alarm clock — the first minute it will ring (let's denote it as yy) and the interval between two consecutive signals (let's denote it by pp). After the clock is set, it will ring during minutes y,y+p,y+2p,y+3py,y+p,y+2p,y+3p and so on.
Ivan can choose any minute as the first one, but he cannot choose any arbitrary value of pp. He has to pick it among the given values p1,p2,…,pmp1,p2,…,pm (his phone does not support any other options for this setting).
So Ivan has to choose the first minute yy when the alarm clock should start ringing and the interval between two consecutive signals pjpj in such a way that it will ring during all given minutes x1,x2,…,xnx1,x2,…,xn (and it does not matter if his alarm clock will ring in any other minutes).
Your task is to tell the first minute yy and the index jj such that if Ivan sets his alarm clock with properties yy and pjpj it will ring during all given minutes x1,x2,…,xnx1,x2,…,xn or say that it is impossible to choose such values of the given properties. If there are multiple answers, you can print any.
Input
The first line of the input contains two integers nn and mm (2≤n≤3⋅105,1≤m≤3⋅1052≤n≤3⋅105,1≤m≤3⋅105) — the number of events and the number of possible settings for the interval between signals.
The second line of the input contains nn integers x1,x2,…,xnx1,x2,…,xn (1≤xi≤10181≤xi≤1018), where xixi is the minute when ii-th event starts. It is guaranteed that all xixi are given in increasing order (i. e. the condition x1<x2<⋯<xnx1<x2<⋯<xn holds).
The third line of the input contains mm integers p1,p2,…,pmp1,p2,…,pm (1≤pj≤10181≤pj≤1018), where pjpj is the jj-th option for the interval between two consecutive signals.
Output
If it's impossible to choose such values yy and jj so all constraints are satisfied, print "NO" in the first line.
Otherwise print "YES" in the first line. Then print two integers yy (1≤y≤10181≤y≤1018) and jj (1≤j≤m1≤j≤m) in the second line, where yy is the first minute Ivan's alarm clock should start ringing and jj is the index of the option for the interval between two consecutive signals (options are numbered from 11 to mm in the order they are given input). These values should be chosen in such a way that the alarm clock will ring during all given minutes x1,x2,…,xnx1,x2,…,xn. If there are multiple answers, you can print any.
Examples
input
Copy
3 5 3 12 18 2 6 5 3 3
output
Copy
YES 3 4
input
Copy
4 2 1 5 17 19 4 5
output
Copy
NO
input
Copy
4 2 1 5 17 19 2 1
output
Copy
YES 1 1
n个数 m个公差
找到一个数x并在上面选择一个公差使得这n个数可以用这个x和选择的公差表示。
没有输出No
思路:差分求gcd,然后枚举一下判断是否能除尽
int main() {
while(cin >> n >> m){
for(int i = 1;i <= n;i++) cin >> a[i];
for(int i = 1;i <= m;i++) cin >> p[i];
ll gcdd = 0ll;
for(int i = 2;i <= n;i++){
ll x = a[i] - a[i - 1];
gcdd = __gcd(gcdd,x);
}
ll ans = -1;
for(int i = 1;i <= m;i++){
if(gcdd % p[i] == 0){
ans = i;
break;
}
}
if(ans == -1){
cout << "NO" << endl;
continue;
}
cout << "YES" << endl;
cout << a[1] << " " << ans << endl;
}
return 0;
}
D. Beautiful Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array aa consisting of nn integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0.
You may choose at most one consecutive subarray of aa and multiply all values contained in this subarray by xx. You want to maximize the beauty of array after applying at most one such operation.
Input
The first line contains two integers nn and xx (1≤n≤3⋅105,−100≤x≤1001≤n≤3⋅105,−100≤x≤100) — the length of array aa and the integer xx respectively.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109) — the array aa.
Output
Print one integer — the maximum possible beauty of array aa after multiplying all values belonging to some consecutive subarray xx.
Examples
input
Copy
5 -2 -3 8 -2 1 -6
output
Copy
22
input
Copy
12 -3 1 3 3 7 1 3 3 7 1 3 3 7
output
Copy
42
input
Copy
5 10 -1 -2 -3 -4 -5
output
Copy
0
Note
In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22([-3, 8, 4, -2, 12]).
In the second test case we don't need to multiply any subarray at all.
In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0.
给定数组和x。你可以将某子区间乘x(可以不乘)。求最大子段和
本来以为是个贪心,判断了一下情况发现有点复杂,特别是x < 0的时候贪心枚举最小字段和是错误的
比如:
8 -2
-5 -3 -1 10 -2 -6 8 9 应该翻转 -2 -6 所以是43
直接dp
f[i][1]代表到i位置的最大字段和,f[i][2]代表到i位置要翻转所做的贡献,f[i][3]求一下所有方式的max
具体转移看code吧,还是很容易懂得
ll f[maxn][4];
int main() {
ios::sync_with_stdio(false);
ll x;
while(cin >> n >> x ){
vector<int>v;
for(int i = 1;i <= n;i++) cin >> a[i];
ll ans = 0;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= 3;j++){
if(j == 1) f[i][j] = max(f[i - 1][j] + a[i],a[i]);
else if(j == 2) f[i][j] = max(max(f[i- 1][j] + a[i] * x,f[i - 1][j - 1] + a[i] * x),a[i] * x);
else f[i][j] = max(max(f[i- 1][j] + a[i],f[i - 1][j - 1] + a[i]),a[i]);
}
ll Max = max(max(f[i][1],f[i][2]),f[i][3]);
ans = max(Max,ans);
}
cout << ans << endl;
}
return 0;
}
E:
交互题 有一个多项式f(x),各项系数不大于1e6+3.
现在需要你找到一个数x0使得f(x0)%1e6+3=0或者输出不存在
提问一个数q,返回f(q)%1e6+3
你最多提问50次。