Codeforces Round #327 (Div. 2)

A. Wizards' Duel
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Harry Potter and He-Who-Must-Not-Be-Named engaged in a fight to the death once again. This time they are located at opposite ends of the corridor of length l. Two opponents simultaneously charge a deadly spell in the enemy. We know that the impulse of Harry's magic spell flies at a speed of p meters per second, and the impulse of You-Know-Who's magic spell flies at a speed of q meters per second.

The impulses are moving through the corridor toward each other, and at the time of the collision they turn round and fly back to those who cast them without changing their original speeds. Then, as soon as the impulse gets back to it's caster, the wizard reflects it and sends again towards the enemy, without changing the original speed of the impulse.

Since Harry has perfectly mastered the basics of magic, he knows that after the second collision both impulses will disappear, and a powerful explosion will occur exactly in the place of their collision. However, the young wizard isn't good at math, so he asks you to calculate the distance from his position to the place of the second meeting of the spell impulses, provided that the opponents do not change positions during the whole fight.

Input

The first line of the input contains a single integer l (1 ≤ l ≤ 1 000) — the length of the corridor where the fight takes place.

The second line contains integer p, the third line contains integer q (1 ≤ p, q ≤ 500) — the speeds of magical impulses for Harry Potter and He-Who-Must-Not-Be-Named, respectively.

Output

Print a single real number — the distance from the end of the corridor, where Harry is located, to the place of the second meeting of the spell impulses. Your answer will be considered correct if its absolute or relative error will not exceed 10 - 4.

Namely: let's assume that your answer equals a, and the answer of the jury is b. The checker program will consider your answer correct if .

Sample test(s)
input
100
50
50
output
50
input
199
60
40
output
119.4
Note

In the first sample the speeds of the impulses are equal, so both of their meetings occur exactly in the middle of the corridor.

题意:有2个人分别在一条长度为l的走廊的两端,开始左边的人用魔法以p速度攻击右边的人。 右边的人用魔法以q速度攻击

左边的人。但魔法第一次碰撞时,魔法会以相反的方向相同的速度回到释放者。当魔法回到释放者身边时,释放者释放第二次

魔法,问第二次魔法发生碰撞的位置。(答案要求误差小于10-4次方)

思路:其实就是问第一次碰撞的位置。因为以同样的速度所以第一次碰撞的位置也是第二次碰撞的位置。既然是给定l求一个满足

要求的位置。那么就相当于在l上找一个值,想到了二分。

对于二分。可以有两种方法:二分时间,求距离。二分距离,求时间。

还有一种做法就是直接推出答案:l/(p+q)*p,保留5位小数以上

<二分时间>

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
#define INF 1<<30
const int MAXN = 10;
int main()
{
    int le,a,b;
    cin >> le>>a>>b;
    double disa, disb,dis;
    double l = 0, r = (le / (a*1.0)) + 1;
    while (1)
    {
        double mid = (l + r) / 2;
        if (fabs(mid*a + mid*b - le) < 0.0001)
        {
            dis = mid*a;
            break;
        }
        if ((mid*a + mid*b) < le)
        {
            l = mid + 0.0001;
        }
        else
        {
            r = mid - 0.0001;
        }
    }
    cout << dis << endl;
    //system("pause");
    return 0;
}
<直接求答案>

#include <iostream>

using namespace std;
int main()
{
    int l, p , q;
    cin >> l >> p >> q;
    double time = (double)l/(double)(p+q);
    double dist = (double)p * time;
    cout << dist << endl;
    return 0;
}

B. Rebranding
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name.

For this purpose the corporation has consecutively hired m designers. Once a company hires the i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters xi by yi, and all the letters yi by xi. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that xi coincides with yi. The version of the name received after the work of the last designer becomes the new name of the corporation.

Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can't wait to find out what is the new name the Corporation will receive.

Satisfy Arkady's curiosity and tell him the final version of the name.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the length of the initial name and the number of designers hired, respectively.

The second line consists of n lowercase English letters and represents the original name of the corporation.

Next m lines contain the descriptions of the designers' actions: the i-th of them contains two space-separated lowercase English letters xi and yi.

Output

Print the new name of the corporation.

Sample test(s)
input
6 1
police
p m
output
molice
input
11 6
abacabadaba
a b
b c
a d
e g
f a
b b
output
cdcbcdcfcdc
题意:要给公司换个新的名字,公司请了m名设计师。每个设计都会提供一个意见:将名字中的字母x换成y, 要y换成x.(可能x和y相等)
现在给出原公司的名字。问经过这m名设计师的意见过后。新的公司名字是什么?
思路:对于每个建议。我们只需判断一下26个字母变成了什么就可以 
比如:a b
原来:a->a b->b(->:对应)
之后:a->b   b->a  
然后b c
原来:a->b  b->a c...
之后:a->c  b->a c...
之后在输出原名字的时候,看字母对应的字母即可。

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
#define INF 1<<30
const int MAXN = 200005;
int main()
{
    int n, m;
    char ta, tb;
    char word[26], name[MAXN];
    cin >> n >> m;
    cin >> name;
    for (int i = 0; i < 26; i++)
    {
        word[i] = 'a' + i;
    }
    for (int i = 0; i < m; i++)
    {
        cin >> ta >> tb;
        if (ta == tb)
        {
            continue;
        }
        for (int i = 0; i < 26; i++)
        {
            if (word[i] == ta)
            {
                word[i] = tb;
            }
            else if (word[i] == tb)
            {
                word[i] = ta;
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << word[name[i] - 'a'];
    }
    cout << endl;
    //system("pause");
    return 0;
}

C. Median Smoothing
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice.

Applying the simplest variant of median smoothing to the sequence of numbers a1, a2, ..., an will result a new sequence b1, b2, ..., bn obtained by the following algorithm:

  • b1 = a1bn = an, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence.
  • For i = 2, ..., n - 1 value bi is equal to the median of three values ai - 1ai and ai + 1.

The median of a set of three numbers is the number that goes on the second place, when these three numbers are written in the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 is equal to 1.

In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only.

Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it.

Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one.

Input

The first input line of the input contains a single integer n (3 ≤ n ≤ 500 000) — the length of the initial sequence.

The next line contains n integers a1, a2, ..., an (ai = 0 or ai = 1), giving the initial sequence itself.

Output

If the sequence will never become stable, print a single number  - 1.

Otherwise, first print a single integer — the minimum number of times one needs to apply the median smoothing algorithm to the initial sequence before it becomes is stable. In the second line print n numbers separated by a space  — the resulting sequence itself.

Sample test(s)
input
4
0 0 1 1
output
0
0 0 1 1
input
5
0 1 0 1 0
output
2
0 0 0 0 0
Note

In the second sample the stabilization occurs in two steps: , and the sequence 00000 is obviously stable.


题意:给定一个只有0和1的串。现在要求从原串经过一定步骤形成一个新的串,这个串满足非递减的。

步骤:

新串的第一个和最后一个和原串的一样,然后第2至(n-1)个,是对应位置i和i-1和i+1这三个数的(排序之后)中位数

问:如果能转成一个新的非递减的串,那么输入转换的次数和最终的串。如果不能则输出-1

思路:因为题目已经说明,a0=0或者an=1,那么肯定可以转换成功,注意处理101010这种情况,如果10101这种长度为奇数,那么就会变成全是1或者0,如果为偶数长度,那么就会变成一半1,一半0……

(未完)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值