Codefoces 1133ABCDE部分题解

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 (1≤ai≤109), where ai is a programming skill of the i-th student.

Output

Print one integer — the maximum possible number of students in a balanced team.

Example

在这里插入图片描述

题目大意:

给你n个能力值,找人去参加比赛,目的是找的人更多,而不是能力有多强(这就是我思路一直卡的原因,想当然了),但是不能队员之间的能力值超过五,也就是找出排序后的连续区间,区间端点的差不超过5

思路:

排序后,布置两个指针,一个指针从第一个开始,然后那个指针跟在后面,然后第一个指针一次走一个,第二个指针一次可能走好几个,停止取决于两个指针所指的地方差超过了5或者后面那个指针溢出了,这样的话,时间复杂度仅仅为O(n),相当于两个指针一块把整个数组遍历了一遍。然后第一个指针每移动一步,取一个max,最后结果就是max

代码如下

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int s[n+1];
    for(int i=1;i<=n;i++)
    {
        cin>>s[i];
    }
    sort(s+1,s+n+1);
    int r=2;
    int ans=1;
    for(int i=1;i<=n;i++)
    {
        while((s[r]-s[i])<=5&&r<=n) 
            r++;
        ans=max(ans,r-i);
    }
    cout<<ans<<endl;
    return 0;
}
``

D - Zero Quantity Maximization CodeForces - 1133D

You are given two arrays a and b, each contains n integers.

You want to create a new array c as follows: choose some real (i.e. not necessarily integer) number d, and then for every i∈[1,n] let ci:=d⋅ai+bi.

Your goal is to maximize the number of zeroes in array c. What is the largest possible answer, if you choose d optimally?

Input

The first line contains one integer n (1≤n≤2⋅105) — the number of elements in both arrays.

The second line contains n integers a1, a2, …, an (−109≤ai≤109).

The third line contains n integers b1, b2, …, bn (−109≤bi≤109).≤2⋅105,1≤k≤2⋅105) — the length of a and the maximum possible value of some ai correspondingly. It is guratanteed that n is even (i.e. divisible by 2). The second line of the test case contains n integers a1,a2,…,an (1≤ai≤k), where ai is the i-th element of a.
It is guaranteed that the sum of n (as well as the sum of k) over all test cases does not exceed 2⋅105 (∑n≤2⋅105, ∑k≤2⋅105).

Output

Print one integer — the maximum number of zeroes in array c, if you choose d optimally.
**

Examples

**
Input
5
1 2 3 4 5
2 4 7 11 3
Output
2
Input
3
13 37 39
1 2 3
Output
2
Input
4
0 0 0 0
1 2 3 4
Output
0
Input
3
1 2 -1
-6 -12 6
Output
3
Note
In the first example, we may choose d=−2.
In the second example, we may choose d=−113.
In the third example, we cannot obtain any zero in array c, no matter which d we choose.
In the fourth example, we may choose d=6.

题目大意:

给你两个数组a,b,你找一个实数d,然后去构建一个数组c,ci=dai+bi,使得*c数组中0的数量最多,最后输出你能所找到一个d,c数组中最多有的0的个数

思路:

这道题用map比较简单吧,如果用hash,不太好处理,或者可能爆掉。
由于要c等于0,那么d就等于-bi/ai;
可是bi/ai就可能是小数,那么可以利用
在这里插入图片描述
然后采用这样的方式在这里插入图片描述
构建map
这样不仅可以避免double精度问题,也就是把小数变成了两个最简分数形式下的一个分母,一个分子,都是整数,而且还可以实现寻找最多满足条件的d的数量最大。
注意:当a=0 且 b=0 时,无论 d 如何取值都满足,应当直接加入答案。当a≠0且b=0 时,d无论取何值都不满足。当a≠0且b=0时,b无论是何值,都是等价的,应当统计对应成同一个d 。

map的用法

代码如下

这是我自己找的一个半题解,然后改的

#include<bits/stdc++.h>
using namespace std;


map<pair<int,int>,int> mp;
int gcd(int x,int y){return (y==0?x:gcd(y,x%y));}

int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    int a[n+1],b[n+1];
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) cin>>b[i];
    int tag=0;
    pair<int,int> p;
    for(int i=1;i<=n;i++)
    {
        if(a[i]) 
        {
            if(b[i]==0) 
            {
                p.first=0;
                p.second=0;
                mp[p]++;
            }
            else
            {
                int tmp=gcd(a[i],b[i]);
                p.first=-b[i]/tmp;
                p.second=a[i]/tmp;
                mp[p]++;
            }
            
        }
        else if(b[i]==0) tag++;
    }
    int ans=0;
    for(auto it=mp.begin();it!=mp.end();it++)
        ans=max(ans,it->second);
    cout<<ans+tag<<endl;
    return 0;
}

还有一种,真的能直接用小数

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f

using namespace std;
 
typedef long long ll;
const ll N=1e5+10;
const int mod=1000000007;
 
int n;
map<long double,int>mp;
int num=0;
int ma=0;
 
signed main (){
    
    
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    long double a[n], b[n];
    for(int i=0;i<n;i++){
    
    
        cin >> a[i];
    }
    for(int i=0;i<n;i++){
    
    
        cin >> b[i];
        if(a[i]==0){
    
    
            if(b[i]==0){
    
    
                num++;
            }
        }
        else{
    
    
            mp[b[i]/a[i]]++;
        }
    }
    map<long double,int>::iterator it;
    for(it=mp.begin(); it!=mp.end(); it++){
    
    
        if(it->second>ma){
    
    
            ma=it->second;
        }
    }
    cout << ma+num;
}

这里大神用的long double
反正这个题得用map,简单一点

E - K Balanced Teams CodeForces - 1133E

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 form k teams for yet another new programming competition. As you know, the more students are involved in competition the more probable the victory of your university is! So you have to form no more than k (and at least one) non-empty teams so that the total number of students in them is maximized. But you also know that each team should be balanced. It means that the programming skill of each pair of students in each team should differ by no more than 5. Teams are independent from one another (it means that the difference between programming skills of two students from two different teams does not matter).

It is possible that some students not be included in any team at all.

Your task is to report the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.

If you are Python programmer, consider using PyPy instead of Python when you submit your code.

Input

The first line of the input contains two integers n and k (1≤k≤n≤5000) — the number of students and the maximum number of teams, correspondingly.

The second line of the input contains n integers a1,a2,…,an (1≤ai≤109), where ai is a programming skill of the i-th student.

Output

Print one integer — the maximum possible total number of students in no more than k (and at least one) non-empty balanced teams.

Examples

Input
5 2
1 2 15 15 15
Output
5
Input
6 1
36 4 1 25 9 16
Output
2
Input
4 4
1 10 100 1000
Output
4

题意

尽可能多的让学生参加比赛,但是最多只能有k个团队,而且一个团队之间的差距不能超过5

思路

思路:二维dp,dp[i][j] 表示前 i 个人分了 k 组的情况下最多能将多少人划入组内。

首先给能力值进行排序,再进行dp。

转移方程为dp[i][j]=max(dp[i−1][j],dp[i−x][j−1]),(其中x需要满足a[i−x]−a[i]≤5) 因为对能力值排序过了,所以可以用单调队列来动态维护dp[i−x][j−1]的值,也可以用其他方法。因为从尽量靠前转移过来,结果是最优的,所以可以只枚举从最尽量最左转移过来,维护一个左指针。因为左指针始终是单调的,即使直接扫,也能够达到O(n)的复杂度。最后找出 i=n 列中最大的dp值就是答案。

#include<bits/stdc++.h>
using namespace std;
int p[5005],d[5005][5005],pos[5005];
int n;
int f(int i,int q){
    
    
    if(q==0||i>n)return 0;
    int& ans=d[i][q];
    if(ans) return ans;
    return ans=max(f(i+1,q),f(i+pos[i],q-1)+pos[i]);
}
int main(){
    
    
    int m;
    cin>>n>>m;
    for(int i{
    
    1};i<=n;i++) cin>>p[i];
    sort(p+1,p+n+1);
    for(int i=1;i<=n;i++) pos[i]=upper_bound(p+1,p+n+1,p[i]+5)-p-i;
    cout<<f(1,m)<<endl;
	return 0;
}

总结:

前三题都不算太难,思路也都有,细节方面做的不到位;另外读题也要细致,不要想当然!!!
第四题的话,用map比较好,而且还要转化,把小数转化为最简公分母的分子和分母存储
编写人署名:张国辉

猜你喜欢

转载自blog.csdn.net/m0_50511504/article/details/108398104