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.
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.
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 .
100 50 50
50
199 60 40
119.4
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;
}
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.
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.
Print the new name of the corporation.
6 1 police p m
molice
11 6 abacabadaba a b b c a d e g f a b b
cdcbcdcfcdc
#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;
}
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 = a1, bn = 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 - 1, ai 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.
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.
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.
4 0 0 1 1
0 0 0 1 1
5 0 1 0 1 0
2 0 0 0 0 0
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……
(未完)