Codefoces 1133ABCDE partial solution

Codefoces 1133 AD solution

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 ‘:’.
Insert picture description here

Main idea:

Give two times, then output the intermediate time

specific methods:

Start with the hour of the first time as the starting point, then add the minutes of the first time plus the minutes of the second time plus the difference between the two hours * 60 to get a number, and then use this Divide the number by 2, and then take the remainder of 60 to get the final minute; divide by 60 plus the hour of the first time is the hour of the final time.
One thing to pay attention to is the format 02%d, and there should be no difficulty in the others.

code show as below
#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.
Insert picture description here

Main idea:

Give you a sequence of n and a k, and then judge that there are several numbers that can be combined to divide k and cannot be repeated, that is, one can only be combined with one group.

Ideas:

Take the remainder of each of the n numbers, then count how many remainders are 0, 1, 2...k, and then choose to accumulate min (i, (ki)), and 0 and 2*k= Special processing of n will
finally output 2 times the sum.
Violence not only takes time but also can be troublesome.

code show as below

#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

Insert picture description here

Main idea:

Give you n ability points, find someone to participate in the game, the purpose is to find more people, not how strong the ability is (this is the reason why I have been stuck in thinking, take it for granted), but the ability value between the players cannot exceed Five, that is, to find the continuous interval after sorting, the difference between the endpoints of the interval does not exceed 5

Ideas:

After sorting, arrange two pointers, one pointer starts from the first, then that pointer follows, then the first pointer moves one at a time, and the second pointer may move several at a time. The stop depends on what the two pointers point to The difference exceeds 5 or the latter pointer overflows. In this case, the time complexity is only O(n), which is equivalent to traversing the entire array with two pointers. Then every time the first pointer moves, a max is taken, and the final result is max

code show as below

#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.

Main idea:

Give you two arrays a, b, you find a real number d, then build an array c, ci=d ai+bi, make the number of 0 in the *c array the most, and finally output the array of d, c you can find The maximum number of zeros in

Ideas:

This question is relatively simple to use map, if you use hash, it is not easy to handle, or may burst.
Since c is equal to 0, then d is equal to -bi/ai;
but bi/ai may be a decimal, then you can use
Insert picture description here
it to Insert picture description here
construct a map in
this way. This not only avoids double precision problems, but also turns decimals into two One denominator and one numerator in the simplest fractional form are all integers, and it can also achieve the maximum number of d that meet the condition.
Note: When a=0 and b=0, no matter how the value of d is satisfied, the answer should be added directly. When a≠0 and b=0, d is not satisfied no matter what value it takes. When a≠0 and b=0, no matter what the value of b is, it is equivalent and should be statistically corresponding to the same d.

Usage of map

code show as below

This is a semi-problem solution I found myself, and then corrected it

#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;
}

There is another one, which can really use decimals directly

#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;
}

The long double used by the great god here,
anyway, this question must use map, simpler

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

Title

Let students participate in the competition as much as possible, but there can only be k teams at most, and the gap between a team cannot exceed 5

Ideas

Idea: The two-dimensional dp,dp[i][j] indicates how many people can be included in the group if the first i people are divided into k groups.

Sort the ability values ​​first, and then perform dp.

The transfer equation is dp[i][j]=max(dp[i−1][j],dp[i−x][j−1]), (where x needs to satisfy a[i−x]−a[ i]≤5) Because the ability values ​​have been sorted, monotonic queues can be used to dynamically maintain the value of dp[i−x][j−1], and other methods can also be used. Because the result is the best to transfer from the front as far as possible, so you can only enumerate and transfer from the farthest to the farthest to maintain a left pointer. Because the left pointer is always monotonous, even if it is scanned directly, it can achieve O(n) complexity. Finally, finding the largest dp value in the i=n column is the answer.

#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;
}

to sum up:

The first three questions are not too difficult, there are also ideas, and the details are not in place; in addition, the questions should be read carefully and don't take it for granted! ! !
For the fourth question, it is better to use map, and it is necessary to transform the decimal number into the numerator and denominator of the simplest common denominator.
Author's signature: Zhang Guohui

Guess you like

Origin blog.csdn.net/m0_50511504/article/details/108398104