20200321VJ训练总结

1.Color the Fence
原题链接
Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanya’s house. Igor thinks that the larger the number is, the more chance to win Tanya’s heart he has.

Unfortunately, Igor could only get v liters of paint. He did the math and concluded that digit d requires ad liters of paint. Besides, Igor heard that Tanya doesn’t like zeroes. That’s why Igor won’t use them in his number.

Help Igor find the maximum number he can write on the fence.

Input
The first line contains a positive integer v (0 ≤ v ≤ 106). The second line contains nine positive integers a1, a2, …, a9 (1 ≤ ai ≤ 105).

Output
Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1.

Examples
Input
5
5 4 3 2 1 2 3 4 5
Output
55555
Input
2
9 11 1 12 5 8 9 10 6
Output
33
Input
0
1 1 1 1 1 1 1 1 1
Output
-1
这个题首先根据所需最少原料的数字判断出最大长度,再从字符串首端开始,判断能否将其转化为更大的数字(在v没法被最少原料整除时)
AC代码如下

``

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
long long a[10],v,Min=100005,index,ans,cnt;
int main()
{
    cin>>v;
    for(int i=1;i<=9;i++)
    {
        cin>>a[i];
        if(a[i]<=Min)
        {
            index=i;
            Min=a[i];
        }
    }
    if(Min>v)
    {
        cout<<"-1";
        return 0;
    }
    else
    {
        if(v%Min==0)
            for(int i=1;i<=v/Min;i++)
                cout<<index;
        else
        {
            ans=v%Min;
            int i;
            while(ans)
            {
                for(i=9;i>index;i--)
                    if(a[i]-a[index]<=ans)
                    {
                        cout<<i;
                        ans-=a[i]-a[index];
                        cnt++;
                        break;
                    }
                if(i==index) break;
            }
            if(!cnt)
            {
                for(i=1;i<=v/Min;i++)
                    cout<<index;
            }
            else
            {
                for(int i=1;i<=v/Min-cnt;i++)
                cout<<index;
            }
        }
    }
    return 0;
}

``
2.Random Teams
题目链接
n participants of the competition were split into m teams in some manner so that each team has at least one participant. After the competition each pair of participants from the same team became friends.

Your task is to write a program that will find the minimum and the maximum number of pairs of friends that could have formed by the end of the competition.

Input
The only line of input contains two integers n and m, separated by a single space (1 ≤ m ≤ n ≤ 109) — the number of participants and the number of teams respectively.

Output
The only line of the output should contain two integers kmin and kmax — the minimum possible number of pairs of friends and the maximum possible number of pairs of friends respectively.

Examples
Input
5 1
Output
10 10
Input
3 2
Output
1 1
Input
6 3
Output
3 6
Note
In the first sample all the participants get into one team, so there will be exactly ten pairs of friends.

In the second sample at any possible arrangement one team will always have two participants and the other team will always have one participant. Thus, the number of pairs of friends will always be equal to one.

In the third sample minimum number of newly formed friendships can be achieved if participants were split on teams consisting of 2 people, maximum number can be achieved if participants were split on teams of 1, 1 and 4 people.
这个题通过找规律就可以看出,最大值即为1~n-m的和,最小值为1到n/m-1的和乘以m
Time Limit代码

``

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
long long n,m,Min,Max;
int main()
{
    cin>>n>>m;
    for(long long i=1;i<=n-m;i++)
        Max+=i;
    for(long long i=1;i<=n/m-1;i++)
        Min+=i;
    Min*=m;
    for(long long i=1;i<=n%m;i++)
        Min+=n/m;
    cout<<Min<<" "<<Max;
    return 0;
}

``
AC代码

``

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
long long n,m,Min,Max,t1,t2;
int main()
{
    cin>>n>>m;
    Max=(n-m+1)*(n-m)/2;
    t1=n/m;
    t2=n%m;
    Min+=t1*(t1-1)/2*(m-t2);
    Min+=(t1+1)*(t1)/2*t2;
    cout<<Min<<" "<<Max;
    return 0;
}

``
3.A and B and Team Training
题目链接
A and B are preparing themselves for programming contests.

An important part of preparing for a competition is sharing programming knowledge from the experienced members to those who are just beginning to deal with the contests. Therefore, during the next team training A decided to make teams so that newbies are solving problems together with experienced participants.

A believes that the optimal team of three people should consist of one experienced participant and two newbies. Thus, each experienced participant can share the experience with a large number of people.

However, B believes that the optimal team should have two experienced members plus one newbie. Thus, each newbie can gain more knowledge and experience.

As a result, A and B have decided that all the teams during the training session should belong to one of the two types described above. Furthermore, they agree that the total number of teams should be as much as possible.

There are n experienced members and m newbies on the training session. Can you calculate what maximum number of teams can be formed?

Input
The first line contains two integers n and m (0 ≤ n, m ≤ 5·105) — the number of experienced participants and newbies that are present at the training session.

Output
Print the maximum number of teams that can be formed.

Examples
Input
2 6
Output
2
Input
4 5
Output
3
Note
Let’s represent the experienced players as XP and newbies as NB.

In the first test the teams look as follows: (XP, NB, NB), (XP, NB, NB).

In the second test sample the teams look as follows: (XP, NB, NB), (XP, NB, NB), (XP, XP, NB).
此题最大值较好求,我的想法最小值是通过先分组,每组n/m个人,算出此时的总数,然后每一组加一个人,直到把n%m个人分完为止,最小值即为前面算出来的值加上n%m个n/m
AC代码

``

#include<iostream>
using namespace std;
long long a,b,Max,ans;
int main()
{
    cin>>a>>b;
    if(a==b)
        Max=(a+b)/3;
    else if(a>b)
    {
        Max=a-b;
        a-=2*Max;
        b-=Max;
        if(b<0) Max+=b;
        else Max+=(a+b)/3;
    }
    else if(b>a)
    {
        Max=b-a;
        a-=Max;
        b-=2*Max;
        if(a<0) Max+=a;
        else Max+=(a+b)/3;
    }
    cout<<Max;
    return 0;
}

``
4.Product of three Numbers
题目链接
You are given one integer number n. Find three distinct integers a,b,c such that 2≤a,b,c and a⋅b⋅c=n or say that it is impossible to do it.

If there are several answers, you can print any.

You have to answer t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤100) — the number of test cases.

The next n lines describe test cases. The i-th test case is given on a new line as one integer n (2≤n≤109).

Output
For each test case, print the answer on it. Print “NO” if it is impossible to represent n as a⋅b⋅c for some distinct integers a,b,c such that 2≤a,b,c.

Otherwise, print “YES” and any possible such representation.

Example
Input
5
64
32
97
2
12345
Output
YES
2 4 8
NO
NO
NO
YES
3 5 823
此题直接暴力枚举即可,但记得要限制循环次数,尽量减少循环次数。
AC代码

``

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
	ll t;
	cin>>t;
	while (t--)
	{
		ll n, x, a, b, c;
		cin>>n;
		a = -1;
		for (x = 2; x <= n / x; x++)
		{
			if (n % x == 0)
			{
				a = x;
				break;
			}
		}
		if (a == -1)
		{
			cout<<"NO"<<endl;
			continue;
		}
		n /= a;
		b = -1;
		for (x = a + 1; x < n / x; x++)
		{
			if (n % x == 0)
			{
				b = x;
				c = n / x;
				break;
			}
		}
		if (b == -1)
		{
			cout<<"NO"<<endl;
			continue;
		}
		cout<<"YES"<<endl<<a<<" "<<b<<" "<<c<<endl;
	}
	return 0;
}

``

发布了8 篇原创文章 · 获赞 12 · 访问量 180

猜你喜欢

转载自blog.csdn.net/weixin_46434074/article/details/105001735
vj