您的位置:首页 > 大数据 > 人工智能

Codeforces AIM Tech Round (Div. 2) 题解

2016-02-21 20:02 721 查看
A. Save Luke

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Input

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.

Output

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

.

Sample test(s)

input
2 6 2 2


output
1.00000000000000000000


input
1 9 1 2


output
2.66666666666666650000


Note

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);
}


B. Making a String

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Input

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.

Output

Print a single integer — the maximum length of the string that meets all the requirements.

Sample test(s)

input
3
2 5 5


output
11


input
3
1 1 2


output
3


Note

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".

题意:求n个字母,最多分别有ai个,而一个字符串中每种字母出现次数是唯一的,求最长的符合要求的字符串。

由此可知,只要出现有相同出现次数就一定会减少直到为零或没有重复的次数。那么只要排序,从大到小枚举,记录当前最小的出现次数数值。那么每次从当前最小数值开始比较是否出现过。将可行值相加即可

#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;
}


C. Graph and String

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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.

Input

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.

Output

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.

Sample test(s)

input
2 1
1 2


output
Yes
aa


input
4 31 2
1 31 4


output
No


Note

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.

题意:给出n个点,点上属性为a,b,c中的一种,相邻或相等,如ab,bc,aa,bb,cc的两点相连成边,而如ca,ac则不能相连。给出相连的边,求是否有图能符合以上要求,若有则输出Yes并输出一种符合要求的图,否则输出No
直接构造,首先不能相连的必定只有ac,ca这种情况,所以先将这种点标记好,如果有冲突就不可行。然后处理可以相连的点,如果不是c的点与c的点相连,则必定是b,有冲突的则不可行。剩下还未确定的点就一定是a

#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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: