1.Commentary Boxes
Description
Berland Football Cup starts really soon! Commentators from all over the world come to the event.
Organizers have already built So each box should be occupied by exactly one delegation.
If m, it is impossible to distribute the boxes to the delegations at the moment.
Organizers can build a new commentary box paying b burles. They can both build and demolish boxes arbitrary number of times (each time paying a corresponding fee). It is allowed to demolish all the existing boxes.
What is the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by m)?
Input
The only line contains four integer numbers b is the fee to demolish a box.
Output
Output the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by 0.
Sample Input
9 7 3 8
15
2 7 3 7
14
30 6 17 19
0
Hint
In the first example organizers can build 3 burles for the each of them.
In the second example organizers can demolish 7 burles for the each of them.
In the third example organizers are already able to distribute all the boxes equally among the delegations, each one get 5 boxes.
题目意思:足球赛之前要给每一支代表队分配评论框,每一支代表队都必须得到数量相同的评论框,现在有n个评论框,m支代表队,可以新建一些评论框代价是a,也可以拆除一些评论框代价是b。问最后至少花费多少。
解题思路:分别算出新建一些和拆除一些评论框的花费,找到最少的花费就行了。
#include<cstdio> #include<cstring> #include<algorithm> #define ll long long int using namespace std; int main() { ll n,m,a,b,x,y,ans1,ans2,ans; scanf("%lld%lld%lld%lld",&n,&m,&a,&b); x=n%m; y=n/m; ans1=x*b;///拆 ans2=((y+1)*m-n)*a;//建 ans=min(ans1,ans2); printf("%lld\n",ans); return 0; }
2.Micro-World
You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.
You know that you have n bacteria in the Petri dish and size of the i-th bacteria is ai. Also you know intergalactic positive integer constant K.
The i-th bacteria can swallow the j-th bacteria if and only if ai>aj and ai≤aj+K. The j-th bacteria disappear, but the i-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria i can swallow any bacteria jif ai>aj and ai≤aj+K. The swallow operations go one after another.
For example, the sequence of bacteria sizes a=[101,53,42,102,101,55,54] and K=1. The one of possible sequences of swallows is: [101,53,42,102,101––––,55,54] → [101,53–––,42,102,55,54] → [101––––,42,102,55,54] → [42,102,55,54–––] → [42,102,55]. In total there are 3 bacteria remained in the Petri dish.
Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.
The first line contains two space separated positive integers n and K (1≤n≤2⋅105, 1≤K≤106) — number of bacteria and intergalactic constant K.
The second line contains n space separated integers a1,a2,…,an (1≤ai≤106) — sizes of bacteria you have.
Print the only integer — minimal possible number of bacteria can remain.
7 1 101 53 42 102 101 55 54
3
6 5 20 15 10 15 20 25
1
7 1000000 1 1 1 1 1 1 1
7
The first example is clarified in the problem statement.
In the second example an optimal possible sequence of swallows is: [20,15,10,15,20–––,25] → [20,15,10,15–––,25] → [20,15,10–––,25] → [20,15–––,25] → [20–––,25] → [25].
In the third example no bacteria can swallow any other bacteria.
题目意思:有n个细菌,每个细菌的尺寸为ai,现在有以常数k,如果细菌i的尺寸ai大于细菌j的尺寸aj,并且ai<=aj+k,那么细菌i就可以吃掉细菌j,问最后可以剩于多少个细菌。
解题思路:用map记录相同尺寸的细菌的个数,之后排序去重,遍历的是一类的细菌(尺寸相同的细菌归为一类),这样会降低时间复杂度。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<map> 5 using namespace std; 6 #define LL long long int 7 LL a[300010]; 8 int main() 9 { 10 LL m,n,i,j,l; 11 while(scanf("%lld%lld",&n,&m)!=EOF) 12 { 13 map<int,int>mp; 14 memset(a,0,sizeof(a)); 15 for(i=0; i<n; i++) 16 { 17 scanf("%lld",&a[i]); 18 mp[a[i]]++; 19 } 20 sort(a,a+n); 21 l=unique(a,a+n)-a; 22 for(i=1; i<l; i++) 23 { 24 if(a[i]>a[i-1]&&a[i-1]+m>=a[i]) 25 { 26 n=n-mp[a[i-1]]; 27 } 28 } 29 printf("%lld\n",n); 30 } 31 return 0; 32 }
3.Strage
Description
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 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' — 26tons.
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 — 50) – the number of available stages and the number of stages to use in the rocket.
The second line contains string 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.
Sample Input
5 3
xyabd
29
7 4
problem
34
2 2
ab
-1
12 1
abaabbaaabbb
1
Hint
In the first example, the following rockets satisfy the condition:
- "adx" (weight is 29);
- "ady" (weight is 0);
- "bdx" (weight is 30);
- "bdy" (weight is 31).
Rocket "adx" has the minimal weight, so the answer is 29.
In the second example, target rocket is "belo". Its weight is 34.
In the third example, b'. This rocket doesn't satisfy the condition, because these letters are adjacent in the
alphabet. Answer is -1.
题目意思:给一串字符串,让我们从其中取指定数量的字符,使得得到字符的值是所取的指定的数量所有字符中值最小的,这个取法规定是相邻的不能取,另一个要求是不能取这个字母之前的数。
解题思路:先排序,之后按照题目要求遍历一遍就可以了。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 bool cmp(int a,int b) 7 { 8 return a<b; 9 } 10 int main() 11 { 12 int a,b,i,j,k=0,ans=0; 13 char x[10000],y[10000]; 14 scanf("%d %d",&a,&b); 15 getchar(); 16 scanf("%s",&x); 17 sort(x,x+a,cmp); 18 for(i=0; i<a; i++) 19 { 20 if(x[i]-1!=y[k-1]&&x[i]!=y[k-1])///不相邻不相同 21 { 22 y[k++]=x[i]; 23 } 24 if(k>=b) 25 { 26 break; 27 } 28 } 29 for(i=0; i<k; i++) 30 { 31 ans+=y[i]-'a'+1; 32 } 33 if(k!=b) 34 { 35 printf("-1\n"); 36 } 37 else 38 { 39 printf("%d\n",ans); 40 } 41 return 0; 42 }