Luke Skywalker got locked up in a rubbish shredder between two presses. R2D2 is already working on his rescue, but Luke needs to stay alive as long as possible. For simplicity we will assume that everything happens on a straight line, the presses are initially at coordinates 0 and L, and they move towards each other with speed v1 and v2, respectively. Luke has width d and is able to choose any position between the presses. Luke dies as soon as the distance between the presses is less than his width. Your task is to determine for how long Luke can stay alive.
The first line of the input contains four integers d, L, v1, v2 (1 ≤ d, L, v1, v2 ≤ 10 000, d < L) — Luke's width, the initial position of the second press and the speed of the first and second presses, respectively.
Print a single real value — the maximum period of time Luke can stay alive for. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.
Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
2 6 2 2
1.00000000000000000000
1 9 1 2
2.66666666666666650000
In the first sample Luke should stay exactly in the middle of the segment, that is at coordinates [2;4], as the presses move with the same speed.
In the second sample he needs to occupy the position . In this case both presses move to his edges at the same time.
题意:求两个人分别从x轴0,l坐标点同时出发,求两人相距大于等于d宽度的最长时间t
时间是小数。
直接求解即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
int d,l,v1,v2;
double ans;
cin>>d>>l>>v1>>v2;
ans=(l-d)*1.0/(v1+v2);
printf("%lf\n",ans);
}
You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied:
- the i-th letter occurs in the string no more than ai times;
- the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once.
The first line of the input contains a single integer n (2 ≤ n ≤ 26) — the number of letters in the alphabet.
The next line contains n integers ai (1 ≤ ai ≤ 109) — i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.
Print a single integer — the maximum length of the string that meets all the requirements.
3 2 5 5
11
3 1 1 2
3
For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc".
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
long long a[50];
map<long long,int>mp;
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
long long ans=0;
long long cur=a[n-1];
for(int i=n-1;i>=0;i--){
if(cur==0)break;
if(mp[a[i]]==1){
a[i]=min(a[i],cur);
a[i]--;
}
cur=a[i];
mp[a[i]]=1;
ans=ans+a[i];
}
cout<<ans<<endl;
}
One day student Vasya was sitting on a lecture and mentioned a string s1s2... sn, consisting of letters "a", "b" and "c" that was written on his desk. As the lecture was boring, Vasya decided to complete the picture by composing a graph G with the following properties:
- G has exactly n vertices, numbered from 1 to n.
- For all pairs of vertices i and j, where i ≠ j, there is an edge connecting them if and only if characters si and sj are either equal or neighbouring in the alphabet. That is, letters in pairs "a"-"b" and "b"-"c" are neighbouring, while letters "a"-"c" are not.
Vasya painted the resulting graph near the string and then erased the string. Next day Vasya's friend Petya came to a lecture and found some graph at his desk. He had heard of Vasya's adventure and now he wants to find out whether it could be the original graph G, painted by Vasya. In order to verify this, Petya needs to know whether there exists a string s, such that if Vasya used this s he would produce the given graph G.
The first line of the input contains two integers n and m — the number of vertices and edges in the graph found by Petya, respectively.
Each of the next m lines contains two integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the edges of the graph G. It is guaranteed, that there are no multiple edges, that is any pair of vertexes appear in this list no more than once.
In the first line print "Yes" (without the quotes), if the string s Petya is interested in really exists and "No" (without the quotes) otherwise.
If the string s exists, then print it on the second line of the output. The length of s must be exactly n, it must consist of only letters "a", "b" and "c" only, and the graph built using this string must coincide with G. If there are multiple possible answers, you may print any of them.
2 1 1 2
Yes aa
4 3 1 2 1 3 1 4
No
In the first sample you are given a graph made of two vertices with an edge between them. So, these vertices can correspond to both the same and adjacent letters. Any of the following strings "aa", "ab", "ba", "bb", "bc", "cb", "cc" meets the graph's conditions.
In the second sample the first vertex is connected to all three other vertices, but these three vertices are not connected with each other. That means that they must correspond to distinct letters that are not adjacent, but that is impossible as there are only two such letters: a and c.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
int f[505][505];
int a[505];
int main()
{
int n,m;
cin>>n>>m;
int x,y;
for(int i=0;i<m;i++){
cin>>x>>y;
f[x][y]=f[y][x]=1;
}
int flag=0;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(f[i][j]==0){
if(a[i]!=3){
a[i]=1;
a[j]=3;
}else if(a[i]==3&&a[j]==3){
flag=1;
break;
}else if(a[i]==3){
a[j]=1;
}
}
}
if(flag) break;
}
if(flag) {
cout<<"No"<<endl;
} else{
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(f[i][j]==1){
if(a[j]==3&&a[i]==0){
a[i]=2;
}else if(a[j]==3&&a[i]==1){
flag=1;
break;
}else if(a[i]==3&&a[j]==1){
flag=1;
break;
}else if(a[i]==3&&a[j]==0){
a[j]=2;
}
}
}
if(flag) break;
}
if(flag){
cout<<"No"<<endl;
}else{
for(int i=1;i<=n;i++)
if(a[i]==0) a[i]=1;
cout<<"Yes"<<endl;
char ch;
for(int i=1;i<=n;i++){
ch=a[i]+'a'-1;
cout<<ch;
}
cout<<endl;
}
}
}