Codeforces Round #293 (Div. 2) (ABCDE题解)

2024-03-20 14:32

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

比赛链接:http://codeforces.com/contest/518


A. Vitaly and Strings
time limit per test: 1 second
memory limit per test: 256 megabytes

Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time.

During the last lesson the teacher has provided two stringss andt to Vitaly. The strings have the same length, they consist of lowercase English letters, strings is lexicographically smaller than stringt. Vitaly wondered if there is such string that is lexicographically larger than strings and at the same is lexicographically smaller than stringt. This string should also consist of lowercase English letters and have the length equal to the lengths of stringss andt.

Let's help Vitaly solve this easy problem!

Input

The first line contains string s (1 ≤ |s| ≤ 100), consisting of lowercase English letters. Here,|s| denotes the length of the string.

The second line contains string t (|t| = |s|), consisting of lowercase English letters.

It is guaranteed that the lengths of strings s and t are the same and strings is lexicographically less than stringt.

Output

If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes).

If such string exists, print it. If there are multiple valid strings, you may print any of them.

Sample test(s)
Input
a
c
Output
b
Input
aaa
zzz
Output
kkk
Input
abcdefg
abcdefh
Output
No such string
Note

String s = s1s2...sn is said to be lexicographically smaller thant = t1t2...tn, if there exists suchi, that s1 = t1, s2 = t2, ...si - 1 = ti - 1, si < ti.


题目大意:给两个长度相同的字符串s,t,求任意一个长度与它们相同的串ans,s < ans < t


题目分析:暴力模拟,每次修改不同位置的值,并将后面都变为a,与t比较


#include <cstdio>
#include <cstring>
char s[105], t[105], ans[105];int main()
{scanf("%s %s", s, t);int len = strlen(s);bool flag = false;for(int i = 0; i < len; i++){if(s[i] != 'z'){strcpy(ans, s);ans[i]++;for(int j = i + 1; j < len; j++)ans[j] = 'a';if(strcmp(ans, t) < 0){flag = true;break;}}}printf("%s\n", flag ? ans : "No such string");
}



B. Tanya and Postcard
time limit per test: 2 seconds
memory limit per test: 256 megabytes

Little Tanya decided to present her dad a postcard on his Birthday. She has already created a message — strings of lengthn, consisting of uppercase and lowercase English letters. Tanya can't write yet, so she found a newspaper and decided to cut out the letters and glue them into the postcard to achieve strings. The newspaper contains stringt, consisting of uppercase and lowercase English letters. We know that the length of stringt greater or equal to the length of the strings.

The newspaper may possibly have too few of some letters needed to make the text and too many of some other letters. That's why Tanya wants to cut somen letters out of the newspaper and make a message of length exactlyn, so that it looked as much as possible likes. If the letter in some position has correct value and correct letter case (in the strings and in the string that Tanya will make), then she shouts joyfully "YAY!", and if the letter in the given position has only the correct value but it is in the wrong case, then the girl says "WHOOPS".

Tanya wants to make such message that lets her shout "YAY!" as much as possible. If there are multiple ways to do this, then her second priority is to maximize the number of times she says "WHOOPS". Your task is to help Tanya make the message.

Input

The first line contains line s (1 ≤ |s| ≤ 2·105), consisting of uppercase and lowercase English letters — the text of Tanya's message.

The second line contains line t (|s| ≤ |t| ≤ 2·105), consisting of uppercase and lowercase English letters — the text written in the newspaper.

Here |a| means the length of the stringa.

Output

Print two integers separated by a space:

  • the first number is the number of times Tanya shouts "YAY!" while making the message,
  • the second number is the number of times Tanya says "WHOOPS" while making the message.
Sample test(s)
Input
AbC
DCbA
Output
3 0
Input
ABC
abc
Output
0 3
Input
abacaba
AbaCaBA
Output
3 4


题目大意:给两个字符串s,t,t长度大于等于s,用t中字符构成s,如果t中能找到s中的字符则YAY次数加1且该字符算作已在s中某一位置,如果t中能找到s中的字符但与其大小写形式不同,WHOOPS次数加1,现在求YAYWHOOPS的值


题目分析:hash存一下,然后遍历26个字母,记录有用的,拿全部减去有用的就是WHOOPS


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[200005], t[200005];
int hs[150], ht[150];
int main() 
{int a = 0, b = 0;memset(hs, 0, sizeof(hs));memset(ht, 0, sizeof(ht));scanf("%s %s", s, t);int l1 = strlen(s);int l2 = strlen(t);for(int i = 0; i < l1; i++)hs[s[i]] ++;for(int i = 0; i < l2; i++)ht[t[i]] ++;for(char ch = 'A'; ch <= 'Z'; ch++) {int tmp = min(hs[ch], ht[ch]) + min(hs[ch + 32], ht[ch + 32]);a += tmp;b += min(hs[ch] + hs[ch + 32], ht[ch] + ht[ch + 32]) - tmp;}printf("%d %d", a, b);
}



C. Anya and Smartphone
time limit per test: 1 second
memory limit per test: 256 megabytes

Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly n applications, each application has its own icon. The icons are located on different screens, one screen containsk icons. The icons from the first to thek-th one are located on the first screen, from the(k + 1)-th to the 2k-th ones are on the second screen and so on (the last screen may be partially empty).

Initially the smartphone menu is showing the screen number1. To launch the application with the icon located on the screent, Anya needs to make the following gestures: first she scrolls to the required screen numbert, by makingt - 1 gestures (if the icon is on the screent), and then make another gesture — press the icon of the required application exactly once to launch it.

After the application is launched, the menu returns to the first screen. That is, to launch the next application you need to scroll through the menu again starting from the screen number1.

All applications are numbered from 1 to n. We know a certain order in which the icons of the applications are located in the menu at the beginning, but it changes as long as you use the operating system.Berdroid is intelligent system, so it changes the order of the icons by moving the more frequently used icons to the beginning of the list. Formally, right after an application is launched, Berdroid swaps the application icon and the icon of a preceding application (that is, the icon of an application on the position that is smaller by one in the order of menu). The preceding icon may possibly be located on the adjacent screen. The only exception is when the icon of the launched application already occupies the first place, in this case the icon arrangement doesn't change.

Anya has planned the order in which she will launch applications. How many gestures should Anya make to launch the applications in the planned order?

Note that one application may be launched multiple times.

Input

The first line of the input contains three numbersn, m, k (1 ≤ n, m, k ≤ 105) — the number of applications that Anya has on her smartphone, the number of applications that will be launched and the number of icons that are located on the same screen.

The next line contains n integers, permutationa1, a2, ..., an — the initial order of icons from left to right in the menu (from the first to the last one),ai —  is the id of the application, whose icon goesi-th in the menu. Each integer from1 to n occurs exactly once amongai.

The third line contains m integersb1, b2, ..., bm(1 ≤ bi ≤ n) — the ids of the launched applications in the planned order. One application may be launched multiple times.

Output

Print a single number — the number of gestures that Anya needs to make to launch all the applications in the desired order.

Sample test(s)
Input
8 3 3
1 2 3 4 5 6 7 8
7 8 1
Output
7
Input
5 4 2
3 1 5 2 4
4 4 4 4
Output
8
Note

In the first test the initial configuration looks like(123)(456)(78), that is, the first screen contains icons of applications1, 2, 3, the second screen contains icons4, 5, 6, the third screen contains icons 7, 8.

After application 7 is launched, we get the new arrangement of the icons — (123)(457)(68). To launch it Anya makes3 gestures.

After application 8 is launched, we get configuration(123)(457)(86). To launch it Anya makes3 gestures.

After application 1 is launched, the arrangement of icons in the menu doesn't change. To launch it Anya makes1 gesture.

In total, Anya makes 7 gestures.


题目大意:n个app,要打开m个,每个页面有k个app,每次打开一个app要从第一页翻到其所在页并按下该app的图表,翻一页是一个操作,按是一个操作,没打开一个app后自动回到第一个页面,而且每次打开一个app如果它不在第一个,则将其位置与其前一个交换,问打开m个app所需要的全部操作数


题目分析:hash存位置,模拟一下,翻页面的次数除一下就得到了,根据题意交换值和值的位置


#include <cstdio>
#include <algorithm>
using namespace std;
int const MAX = 1e5 + 5;
int a[MAX], re[MAX];
int main()
{int n, m, k;long long ans = 0;scanf("%d %d %d", &n, &m, &k);for(int i = 1; i <= n; i++){scanf("%d", &a[i]);re[a[i]] = i;}for(int i = 0; i < m; i++){int b;scanf("%d", &b);ans += (re[b] - 1) / k + 1;int tmp = a[re[b] - 1];if(re[b] > 1){swap(re[b], re[tmp]);swap(a[re[b]], a[re[tmp]]);}}printf("%I64d\n", ans);
}


D. Ilya and Escalator
time limit per test: 2 seconds
memory limit per test: 256 megabytes

Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor.

Let's assume that n people stand in the queue for the escalator. At each second one of the two following possibilities takes place: either the first person in the queue enters the escalator with probability p, or the first person in the queue doesn't move with probability(1 - p), paralyzed by his fear of escalators and making the whole queue wait behind him.

Formally speaking, the i-th person in the queue cannot enter the escalator until people with indices from1 toi - 1 inclusive enter it. In one second only one person can enter the escalator. The escalator is infinite, so if a person enters it, he never leaves it, that is he will be standing on the escalator at any following second. Ilya needs to count the expected value of the number of people standing on the escalator aftert seconds.

Your task is to help him solve this complicated task.

Input

The first line of the input contains three numbersn, p, t (1 ≤ n, t ≤ 2000,0 ≤ p ≤ 1). Numbers n and t are integers, numberp is real, given with exactly two digits after the decimal point.

Output

Print a single real number — the expected number of people who will be standing on the escalator aftert seconds. The absolute or relative error mustn't exceed10 - 6.

Sample test(s)
Input
1 0.50 1
Output
0.5
Input
1 0.50 4
Output
0.9375
Input
4 0.20 2
Output
0.4


题目链接:http://codeforces.com/contest/518/problem/d


题目大意:n个人排队上电梯,排头每秒上去的概率为p,一共t秒,求t秒都电梯内人数的期望


题目分析:简单的概率dp,dp[i][j]表示第i秒电梯上有j个人的概率,最后累计一下期望


#include <cstdio>
#include <cstring>
double dp[2005][2005];int main()
{int n, t;double p, ans = 0;memset(dp, 0, sizeof(dp));dp[0][0] = 1;scanf("%d %lf %d", &n, &p, &t);for(int i = 1; i <= t; i++){for(int j = n; j >= 0; j--){if(j == n)dp[i][j] = dp[i - 1][j - 1] * p + dp[i - 1][j];else if(j != 0)dp[i][j] = dp[i - 1][j - 1] * p + dp[i - 1][j] * (1 - p);elsedp[i][j] = dp[i - 1][j] * (1 - p);}}for(int i = 1; i <= n; i++)ans += (dp[t][i] * i);printf("%.7f\n", ans);
}



E. Arthur and Questions
time limit per test: 2 seconds
memory limit per test: 256 megabytes

After bracket sequences Arthur took up number theory. He has got a new favorite sequence of lengthn (a1, a2, ..., an), consisting of integers and integerk, not exceeding n.

This sequence had the following property: if you write out the sums of all its segments consisting ofk consecutive elements (a1  +  a2 ...  +  ak,  a2  +  a3  +  ...  +  ak + 1,  ...,  an - k + 1  +  an - k + 2  +  ...  +  an), then those numbers will form strictly increasing sequence.

For example, for the following sample: n = 5,  k = 3,  a = (1,  2,  4,  5,  6) the sequence of numbers will look as follows: (1  +  2  +  4,  2  +  4  +  5,  4  +  5  +  6) = (7,  11,  15), that means that sequence a meets the described property.

Obviously the sequence of sums will have n - k + 1 elements.

Somebody (we won't say who) replaced some numbers in Arthur's sequence by question marks (if this number is replaced, it is replaced by exactly one question mark). We need to restore the sequence so that it meets the required property and also minimize the sum |ai|, where|ai| is the absolute value ofai.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 105), showing how many numbers are in Arthur's sequence and the lengths of segments respectively.

The next line contains n space-separated elementsai (1 ≤ i ≤ n).

If ai  =  ?, then thei-th element of Arthur's sequence was replaced by a question mark.

Otherwise, ai ( - 109 ≤ ai ≤ 109) is the i-th element of Arthur's sequence.

Output

If Arthur is wrong at some point and there is no sequence that could fit the given information, print a single string "Incorrect sequence" (without the quotes).

Otherwise, print n integers — Arthur's favorite sequence. If there are multiple such sequences, print the sequence with the minimum sum|ai|, where|ai| is the absolute value ofai. If there are still several such sequences, you are allowed to print any of them. Print the elements of the sequence without leading zeroes.

Sample test(s)
Input
3 2
? 1 2
Output
0 1 2 
Input
5 1
-10 -9 ? -7 -6
Output
-10 -9 -8 -7 -6 
Input
5 3
4 6 7 2 9
Output
Incorrect sequence


题目大意:n个数的序列,?为不确定数字,要求序列的连续k项和严格递增,问怎样构造能使序列各项的绝对值的和最小


题目分析:a[1] + a[2] + ... + a[k] < a[2] + a[3] +... +a[k + 1] < a[3] + a[4] +...+ a[k + 2]分析可以发现满足条件的序列必满足

a[1] < a[k + 1] < a[2*k + 1]....

a[2] < a[k + 2] < a[2*k + 2]....

......

在序列最后插入k个INF,枚举时用来当边界,构造时计算?个数,通过?个数确定满足条件的最小值,然后依次向后赋值,每次值只加1,保证绝对值和最小


#include <cstdio>
#include <algorithm>
using namespace std;
int const INF = 0x3fffffff;
char s[20];
int a[200000];int main() 
{int n, k;scanf("%d %d", &n, &k);for(int i = 0; i < n; i++) {scanf("%s", s);if(s[0] == '?')a[i] = INF;elsesscanf(s, "%d", &a[i]);}for(int i = n; i < n + k; i++)a[i] = INF + 1;for(int i = 0; i < k; i++) {int mi = -INF, cnt = 0;for(int j = i; j < n + k; j += k) {if(a[j] == INF)cnt++;else {if(a[j] - mi <= cnt) {printf("Incorrect sequence\n");return 0;}int num = max(mi + 1, min(a[j] - cnt, -cnt / 2));for(int tmp = cnt; tmp > 0; tmp--){int idx = j - tmp * k;a[idx] = num++;}mi = a[j];cnt = 0;}}}for(int i = 0; i < n - 1; i++)printf("%d ", a[i]);printf("%d\n", a[n - 1]);
}


这篇关于Codeforces Round #293 (Div. 2) (ABCDE题解)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

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

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

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

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

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

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述