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

54 篇文章 0 订阅
53 篇文章 0 订阅

比赛链接: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);
            else
                dp[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;
        else
            sscanf(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]);
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值