Codefoces 1133 A-D题解
A - Middle of the Contest CodeForces - 1133A
Polycarp is going to participate in the contest. It starts at h1:m1 and ends at h2:m2. It is guaranteed that the contest lasts an even number of minutes (i.e. m1%2=m2%2, where x%y is x modulo y). It is also guaranteed that the entire contest is held during a single day. And finally it is guaranteed that the contest lasts at least two minutes.
Polycarp wants to know the time of the midpoint of the contest. For example, if the contest lasts from 10:00 to 11:00 then the answer is 10:30, if the contest lasts from 11:10 to 11:12 then the answer is 11:11.
input
The first line of the input contains two integers h1 and m1 in the format hh:mm.
The second line of the input contains two integers h2 and m2 in the same format (hh:mm).
It is guaranteed that 0≤h1,h2≤23 and 0≤m1,m2≤59.
It is guaranteed that the contest lasts an even number of minutes (i.e. m1%2=m2%2, where x%y is x modulo y). It is also guaranteed that the entire contest is held during a single day. And finally it is guaranteed that the contest lasts at least two minutes.
output
Print two integers h3 and m3 (0≤h3≤23,0≤m3≤59) corresponding to the midpoint of the contest in the format hh:mm. Print each number as exactly two digits (prepend a number with leading zero if needed), separate them with ‘:’.
题目大意:
给出两个时间,然后输出中间时间
具体做法:
以第一个时间的小时数整点为起始点,然后加上第一个时间的分钟数加上第二个时间的分钟数加上两个小时数的差*60得到一个数,然后用这个数除于2,然后对60取余就是最后的分钟数;除于60 加上第一个时间的小时数就是最后时间的小时数。
需要注意的一点就是格式02%d,其他应该没有难点。
代码如下
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
scanf("%d:%d",&a,&b);
scanf("%d:%d",&c,&d);
int sum=(c-a)*60+b+d;
sum/=2;
printf("%02d:",a+sum/60);
printf("%02d",sum%60);
return 0;
}
B - Preparation for International Women’s Day CodeForces - 1133B
International Women’s Day is coming soon! Polycarp is preparing for the holiday.
There are n candy boxes in the shop for sale. The i-th box contains di candies.
Polycarp wants to prepare the maximum number of gifts for k girls. Each gift will consist of exactly two boxes. The girls should be able to share each gift equally, so the total amount of candies in a gift (in a pair of boxes) should be divisible by k. In other words, two boxes i and j (i≠j) can be combined as a gift if di+dj is divisible by k.
How many boxes will Polycarp be able to give? Of course, each box can be a part of no more than one gift. Polycarp cannot use boxes “partially” or redistribute candies between them.
Input
The first line of the input contains two integers n and k (1≤n≤2⋅105,1≤k≤100) — the number the boxes and the number the girls.
The second line of the input contains n integers d1,d2,…,dn (1≤di≤109), where di is the number of candies in the i-th box.
Output
Print one integer — the maximum number of the boxes Polycarp can give as gifts.
题目大意:
给你一个n序列和一个k,然后判断有几个数字能组合在一起整除k,不能重复,也就是一个只能和一个组团干k。
思路:
把n个数每一个都去对k取余,然后统计余数为0、1、2…k的分别有多少个,然后选择累加min(i,(k-i)),要把0和2*k=n的做特殊处理
最后输出总和的2倍
暴力不仅卡时间而且还会很麻烦。
代码如下
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
int k;
cin>>n>>k;
int s[n+1];
int counts[k]={0};
for(int i=1;i<=n;i++)
{
cin>>s[i];
counts[s[i]%k]++;
}
int sum=0;
for(int i=0;2*i<=k;i++)
{
if(i==0||2*i==k)
sum+=counts[i]/2;
else
{
sum+=min(counts[i],counts[k-i]);
}
}
cout<<sum*2<<endl;
return 0;
}
C - Balanced Team CodeForces - 1133C
You are a coach at your local university. There are n students under your supervision, the programming skill of the i-th student is ai.
You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than 5.
Your task is to report the maximum possible number of students in a balanced team.
Input
The first line of the input contains one integer n (1≤n≤2⋅105) — the number of students.
The second line of the input contains n integers a1,a2,…,an (