Codeforces Round #382 (Div. 2)A到D题 A. Ostap and //B. Urbanization//C. Tennis Championship//D. Taxes

A. Ostap and Grasshopper
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

On the way to Rio de Janeiro Ostap kills time playing with a grasshopper he took with him in a special box. Ostap builds a line of length nsuch that some cells of this line are empty and some contain obstacles. Then, he places his grasshopper to one of the empty cells and a small insect in another empty cell. The grasshopper wants to eat the insect.

Ostap knows that grasshopper is able to jump to any empty cell that is exactly k cells away from the current (to the left or to the right). Note that it doesn't matter whether intermediate cells are empty or not as the grasshopper makes a jump over them. For example, if k = 1 the grasshopper can jump to a neighboring cell only, and if k = 2 the grasshopper can jump over a single cell.

Your goal is to determine whether there is a sequence of jumps such that grasshopper will get from his initial position to the cell with an insect.

Input

The first line of the input contains two integers n and k (2 ≤ n ≤ 1001 ≤ k ≤ n - 1) — the number of cells in the line and the length of one grasshopper's jump.

The second line contains a string of length n consisting of characters '.', '#', 'G' and 'T'. Character '.' means that the corresponding cell is empty, character '#' means that the corresponding cell contains an obstacle and grasshopper can't jump there. Character 'G' means that the grasshopper starts at this position and, finally, 'T' means that the target insect is located at this cell. It's guaranteed that characters 'G' and 'T' appear in this line exactly once.

Output

If there exists a sequence of jumps (each jump of length k), such that the grasshopper can get from his initia

题意大约是从G跳到T,每次能跳k个格,只能跳到‘  . ’上,不能跳到‘ #’上

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<vector>
#include<string.h>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
	char ch[300];
	int n,k,i,be,en,len,t;
	scanf("%d %d",&n,&k);
	getchar();
	scanf("%s",ch);
	for(i=0;i<n;i++)
	{
		if(ch[i]=='G')
		be=i;
		else if(ch[i]=='T')
		en=i;
	}
	if(be>en)
	{
		t=en;
		en=be;
		be=t;
	}
	int flog=0;
	for(i=be;i<=en;i+=k)
	{
		if(i==en)
		{
			flog=1;
			printf("YES\n");	
		}
		else if(ch[i]=='#')
		{
			break;
		}
	}
	if(!flog)
	printf("NO\n");
	return 0;
 } 


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

Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided to ask his help in the question of urbanization. There are n people who plan to move to the cities. The wealth of the i of them is equal to ai. Authorities plan to build two cities, first for n1 people and second for n2 people. Of course, each of n candidates can settle in only one of the cities. Thus, first some subset of candidates of size n1 settle in the first city and then some subset of size n2 is chosen among the remaining candidates and the move to the second city. All other candidates receive an official refuse and go back home.

To make the statistic of local region look better in the eyes of their bosses, local authorities decided to pick subsets of candidates in such a way that the sum of arithmetic mean of wealth of people in each of the cities is as large as possible. Arithmetic mean of wealth in one city is the sum of wealth ai among all its residents divided by the number of them (n1 or n2 depending on the city). The division should be done in real numbers without any rounding.

Please, help authorities find the optimal way to pick residents for two cities.

Input

The first line of the input contains three integers nn1 and n2 (1 ≤ n, n1, n2 ≤ 100 000n1 + n2 ≤ n) — the number of candidates who want to move to the cities, the planned number of residents of the first city and the planned number of residents of the second city.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 100 000), the i-th of them is equal to the wealth of the i-th candidate.

Output

Print one real value — the maximum possible sum of arithmetic means of wealth of cities' residents. You 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 .

Examples
input
2 1 1
1 5
output
6.00000000
input
4 2 1
1 4 2 3
output
6.50000000
Note

In the first sample, one of the optimal solutions is to move candidate 1 to the first city and candidate 2 to the second.

In the second sample, the optimal solution is to pick candidates 3 and 4 for the first city, and candidate 2 for the second one. Thus we obtain (a3 + a4) / 2 + a2 = (3 + 2) / 2 + 4 = 6.5

题意从n个数里选N1, N2 个数,再把这两堆数分别求平均值,把大的数放在数量少的堆里,sort一下就好了

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<vector>
#include<string.h>
#define inf 0x3f3f3f3f
using namespace std;
int num[100005];
int cmp(int a,int b)
{
	return a>b;
}
int main()
{
	int n,n1,n2,i,t=0;
	double sum=0,ans=0;
	scanf("%d %d %d",&n,&n1,&n2);
	for(i=0;i<n;i++)
	scanf("%d",&num[i]);
	sort(num,num+n,cmp);
	if(n1>n2)
	{
		t=n1;
		n1=n2;
		n2=t;
	}
	int flog=0;
	for(t=0,i=0;i<n;i++)
	{
		t++;
		sum+=num[i];
		if(t==n1&&!flog)
		{
			ans+=(sum/n1*1.0);
			sum=0;
			t=0;
			flog=1;
		}
		else if(t==n2)
		{
		//	printf("%lf ",ans);
			ans+=(sum/n2*1.0);
			break;
		}
	}	
	printf("%.8lf\n",ans);
	return 0;
 } 


C. Tennis Championship
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Famous Brazil city Rio de Janeiro holds a tennis tournament and Ostap Bender doesn't want to miss this event. There will be n players participating, and the tournament will follow knockout rules from the very first game. That means, that if someone loses a game he leaves the tournament immediately.

Organizers are still arranging tournament grid (i.e. the order games will happen and who is going to play with whom) but they have already fixed one rule: two players can play against each other only if the number of games one of them has already played differs by no more than one from the number of games the other one has already played. Of course, both players had to win all their games in order to continue participating in the tournament.

Tournament hasn't started yet so the audience is a bit bored. Ostap decided to find out what is the maximum number of games the winner of the tournament can take part in (assuming the rule above is used). However, it is unlikely he can deal with this problem without your help.

Input

The only line of the input contains a single integer n (2 ≤ n ≤ 1018) — the number of players to participate in the tournament.

Output

Print the maximum number of games in which the winner of the tournament can take part.

Examples
input
2
output
1
input
3
output
2
input
4
output
2
input
10
output
4
Note

In all samples we consider that player number 1 is the winner.

In the first sample, there would be only one game so the answer is 1.

In the second sample, player 1 can consequently beat players 2 and 3.

In the third sample, player 1 can't play with each other player as after he plays with players 2 and 3 he can't play against player 4, as he has 0 games played, while player 1 already played 2. Thus, the answer is 2 and to achieve we make pairs (1, 2) and (3, 4) and then clash the winners.

题意是n个人做游戏,输了就被淘汰,每个人只能和游戏次数与他自己的游戏次数相差不超过1的人做游戏,求其中一人最多的游戏次数,上的某飞巨的车,太强了,先用dp打表,发现规律是斐波那契数列

  2=1 3=2 4=2 5=3 6=3 7=3 8=4 9=4 10=4 11=4 12=4 13=5 14=5 15=5 16=5 17=5 18=5
19=5 20=5 21=6 22=6 23=6 24=6 25=6 26=6 27=6 28=6 29=6 30=6 31=6 32=6 33=6  34=7 35=7 36=7 37=7 38=7 39=7 40=7 41=7 42=7 43=7 44=7 45=7 46=7 47=7 48=7  49=7 50=7 51=7 52=7 53=7 54=7 55=8 56=8 57=8 58=8 59=8 60=8 61=8 62=8 63=8  64=8 65=8 66=8 67=8 68=8 69=8 70=8 71=8 72=8 73=8 74=8 75=8 76=8 77=8 78=8  79=8 80=8 81=8 82=8 83=8 84=8 85=8 86=8 87=8 88=8 89=9 90=9 91=9 92=9

打表代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
	int dp[200];
	memset(dp,0,sizeof(dp));
	dp[1]=0;
	dp[2]=1;
	for(int i=3;i<100;i++)
	{
		for(int j=i-1;j>=i/2;j--)
		{
			if(abs(dp[j]-dp[i-j])<=1)
			{
				dp[i]=max(dp[i],max(dp[j],dp[i-j])+1);
			}
		}
	}
	for(int i=1;i<100;i++)
	{
		printf("%d %d\n",i,dp[i]);
	}
	return 0;
}

AC代码:
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<vector>
#include<string.h>
#define inf 0x3f3f3f3f
using namespace std;
long long int f[200];
int main()
{
	int i;
	f[1]=1;
	f[2]=3;
	f[3]=5;
	for(i=4;i<92;i++)
	f[i]=f[i-1]+f[i-2];
	//for(int i=0;i<92;i++)
//	printf("%lld\n",f[i]);
	long long int n;
	scanf("%lld",&n);
	for(i=0;i<92;i++)
	{
		if(n<f[i])
		break;
	}
	printf("%d",i-1);		
 } 





D. Taxes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mr. Funt now lives in a country with a very specific tax laws. The total income of mr. Funt during this year is equal to n (n ≥ 2) burles and the amount of tax he has to pay is calculated as the maximum divisor of n (not equal to n, of course). For example, if n = 6 then Funt has to pay 3 burles, while for n = 25 he needs to pay 5 and if n = 2 he pays only 1 burle.

As mr. Funt is a very opportunistic person he wants to cheat a bit. In particular, he wants to split the initial n in several partsn1 + n2 + ... + nk = n (here k is arbitrary, even k = 1 is allowed) and pay the taxes for each part separately. He can't make some part equal to 1 because it will reveal him. So, the condition ni ≥ 2 should hold for all i from 1 to k.

Ostap Bender wonders, how many money Funt has to pay (i.e. minimal) if he chooses and optimal way to split n in parts.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 2·109) — the total year income of mr. Funt.

Output

Print one integer — minimum possible number of burles that mr. Funt has to pay as a tax.

Examples
input
4
output
2
input
27
output
3
题意很简单,先判断n是不是素数,素数为1,对于非素数:
哥德巴赫猜想:一个偶数肯定总能表示成两个素数和,所以偶数的结果一定是2,当时觉得偶数是2,奇数肯定是3,结果被人hack了,奇数需要特判一下,因为奇数也有分成两个素数的情况,那么肯定是一个奇素数,一个偶素数,只有一个2是偶素数,所以特判一下n-2是不是素数即可,例如9,答案就应该是2;
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<vector>
#include<string.h>
#define inf 0x3f3f3f3f
using namespace std;
bool prime(int x);
int main()
{
	int n,i,ans=0;
	scanf("%d",&n);	
	if(prime(n))
	printf("1\n");
	else
	{
		if(n&1)
		{
			if(prime(n-2))
			printf("2\n");
			else
			printf("3\n");
		}
		else
		printf("2\n"); 
	}
		

 } 
bool prime(int x)
{
	int i;
	for(i=2;i<=sqrt(x);i++)
	{
		if(x%i==0)
		return 0;
	}
	return 1;
}


水题切得太慢,题意理解太慢



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值