Codeforces AIM Tech Round (Div. 2) 題解


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 dLv1v2 (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 3
1 2
1 3
1 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;
	 }
  }
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章