Codeforces Round #499 (Div. 2)

Codeforces Round #499 (Div. 2)

2019/11/30 (周六) 第一次训练赛

链接:Codeforces Round #499 (Div. 2)

A.Stages

time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output

题目描述

Natasha is going to fly to Mars. She needs to build a rocket, which consists of several stages in some order. Each of the stages is defined by a lowercase Latin letter. This way, the rocket can be described by the string — concatenation of letters, which correspond to the stages.

There are n stages available. The rocket must contain exactly k of them. Stages in the rocket should be ordered by their weight. So, after the stage with some letter can go only stage with a letter, which is at least two positions after in the alphabet (skipping one letter in between, or even more). For example, after letter ‘c’ can’t go letters ‘a’, ‘b’, ‘c’ and ‘d’, but can go letters ‘e’, ‘f’, …, ‘z’.

For the rocket to fly as far as possible, its weight should be minimal. The weight of the rocket is equal to the sum of the weights of its stages. The weight of the stage is the number of its letter in the alphabet. For example, the stage ‘a ‘weighs one ton,’ b ‘weighs two tons, and’ z’ — 26 tons.

Build the rocket with the minimal weight or determine, that it is impossible to build a rocket at all. Each stage can be used at most once.

Input

The first line of input contains two integers — n and k (1≤k≤n≤50) – the number of available stages and the number of stages to use in the rocket.
The second line contains string s, which consists of exactly n lowercase Latin letters. Each letter defines a new stage, which can be used to build the rocket. Each stage can be used at most once.

Output

Print a single integer — the minimal total weight of the rocket or -1, if it is impossible to build the rocket at all.

Examples
input
  • 5 3
    xyabd
  • 7 4
    problem
  • 2 2
    ab
  • 12 1
    abaabbaaabbb
output
  • 29
  • 34
  • -1
  • 1
Note

In the first example, the following rockets satisfy the condition:

  • “adx” (weight is 1+4+24=29);
  • “ady” (weight is 1+4+25=30);
  • “bdx” (weight is 2+4+24=30);
  • “bdy” (weight is 2+4+25=31).
  • Rocket “adx” has the minimal weight, so the answer is 29.

In the second example, target rocket is “belo”. Its weight is 2+5+12+15=34.

In the third example, n=k=2, so the rocket must have both stages: ‘a’ and ‘b’. This rocket doesn’t satisfy the condition, because these letters are adjacent in the alphabet. Answer is -1.

解题思路

本题可以使用贪心的思想,每次都选取当前重量最小的字符,结果就会最小。
所以先将所有的字符按照字典序排序,然后从第头开始如果满足题意(和前一位间隔至少一个字符)便取这个字符,直到字符串的位数达到k便是最优的选择,计算出此时的重量即可。

AC代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
	char s[55];
	int n,k;
	while (scanf("%d%d",&n,&k)!=EOF)
	{
		string ans="";
		int num=0;
		getchar();
		scanf("%s",s);
		getchar();
		sort(s,s+strlen(s));
		ans+=s[0];
		for(int i=1;i<n;i++)
		{
			if(ans.length()==k) break;
			if(s[i]-ans[ans.length()-1]>=2) ans+=s[i];
		}
		for(int i=0;i<k;i++)
		{
			num+=ans[i]-'a'+1;
		}
		if(ans.length()<k) num=-1;
		printf("%d\n",num);
	}
	return 0;
}

 
 

B. Planning The Expedition

time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output

题目描述

Natasha is planning an expedition to Mars for n people. One of the important tasks is to provide food for each participant.

The warehouse has m daily food packages. Each package has some food type ai.

Each participant must eat exactly one food package each day. Due to extreme loads, each participant must eat the same food type throughout the expedition. Different participants may eat different (or the same) types of food.

Formally, for each participant j Natasha should select his food type bj and each day j-th participant will eat one food package of type bj. The values bj for different participants may be different.

What is the maximum possible number of days the expedition can last, following the requirements above?

Input

The first line contains two integers n and m (1≤n≤100, 1≤m≤100) — the number of the expedition participants and the number of the daily food packages available.

The second line contains sequence of integers a1,a2,,am (1≤ai≤100), where ai is the type of i-th food package.

Output

Print the single integer — the number of days the expedition can last. If it is not possible to plan the expedition for even one day, print 0.

Examples
input
  • 4 10
    1 5 2 1 1 1 2 5 7 2
  • 100 1
    1
  • 2 5
    5 4 3 2 1
  • 3 9
    42 42 42 42 42 42 42 42 42
output
  • 2
  • 0
  • 1
  • 3
Note

In the first example, Natasha can assign type 1 food to the first participant, the same type 1 to the second, type 5 to the third and type 2 to the fourth. In this case, the expedition can last for 2 days, since each participant can get two food packages of his food type (there will be used 4 packages of type 1, two packages of type 2 and two packages of type 5).

In the second example, there are 100 participants and only 1 food package. In this case, the expedition can’t last even 1 day.

解题思路

将所有的同类型的食物装以一个桶里,再将桶从大到小排序,因为一个人只能吃同一类型的食物,所以支撑的的天数必然不能超过桶的最大值,所以设最大桶的值为最大天数,依次递减,然后用桶的值除以天数检验是否能满足n个人的需求,最先符合条件的天数即为本题的最大天数。当然如果人数比食物总数多的话就没必要算了直接输出0就行了。

AC代码
#include <bits/stdc++.h>
using namespace std;
int cmp(int a,int b)
{
	if(a>b) return 1;
	return 0;
}
int main()
{
	int n,m;
	int type[110];
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(type,0,sizeof(type));
		for(int i=0;i<m;i++)
		{
			int t;
			scanf("%d",&t);
			type[t]++;
		}
		sort(type,type+101,cmp);
		if(n>m)
		{
			printf("0\n");
			continue;
		}else
		{
			for(int i=type[0];i>=1;i--)
			{
				int count=0;
				for(int j=0;j<m;j++)
				{
					count+=type[j]/i;
				}
				if(count>=n)
				{
					printf("%d\n",i);
					break;
				}
			}
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值