Codeforces Round #643 (Div. 2)【A—D】

A. Sequence with Digits

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

Let’s define the following recurrence:
an+1=an+minDigit(an)⋅maxDigit(an).
Here minDigit(x) and maxDigit(x) are the minimal and maximal digits in the decimal representation of x without leading zeroes. For examples refer to notes.

Your task is calculate aK for given a1 and K.

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

Each test case consists of a single line containing two integers a1 and K (1≤a1≤1018, 1≤K≤1016) separated by a space.

Output
For each test case print one integer aK on a separate line.

Example
input
8
1 4
487 1
487 2
487 3
487 4
487 5
487 6
487 7
output
42
487
519
528
544
564
588
628
Note
a1=487

a2=a1+minDigit(a1)⋅maxDigit(a1)=487+min(4,8,7)⋅max(4,8,7)=487+4⋅8=519

a3=a2+minDigit(a2)⋅maxDigit(a2)=519+min(5,1,9)⋅max(5,1,9)=519+1⋅9=528

a4=a3+minDigit(a3)⋅maxDigit(a3)=528+min(5,2,8)⋅max(5,2,8)=528+2⋅8=544

a5=a4+minDigit(a4)⋅maxDigit(a4)=544+min(5,4,4)⋅max(5,4,4)=544+4⋅5=564

a6=a5+minDigit(a5)⋅maxDigit(a5)=564+min(5,6,4)⋅max(5,6,4)=564+4⋅6=588

a7=a6+minDigit(a6)⋅maxDigit(a6)=588+min(5,8,8)⋅max(5,8,8)=588+5⋅8=628

题解:
在这里插入图片描述

#include <bits/stdc++.h>
#define ll long long 
using namespace std;
const int inf = 0x3f3f3f3f;

int main()
{
    
    
    int T;
    scanf("%d",&T);
    while(T--)
    {
    
    
        ll n,k;
        scanf("%lld%lld",&n,&k);
        for(ll i=1; i<k; i++)
        {
    
    
            ll minn=inf,maxn=-inf;
            ll tmp=n;
            while(tmp)
            {
    
    
                ll now = tmp%10;
                minn = min(minn,now);
                maxn = max(maxn,now);
                tmp /= 10;
            }
            n += minn*maxn;
            if(minn==0)  break;
        }
        printf("%lld\n",n);
    }
    return 0;
}

B. Young Explorers

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties…

Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter ei — his inexperience. Russell decided that an explorer with inexperience e can only join the group of e or more people.

Now Russell needs to figure out how many groups he can organize. It’s not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.

Input
The first line contains the number of independent test cases T(1≤T≤2⋅105). Next 2T lines contain description of test cases.

The first line of description of each test case contains the number of young explorers N (1≤N≤2⋅105).

The second line contains N integers e1,e2,…,eN (1≤ei≤N), where ei is the inexperience of the i-th explorer.

It’s guaranteed that sum of all N doesn’t exceed 3⋅105.

Output
Print T numbers, each number on a separate line.

In i-th line print the maximum number of groups Russell can form in i-th test case.

Example
input
2
3
1 1 1
5
2 3 1 2 2
output
3
2
Note
In the first example we can organize three groups. There will be only one explorer in each group. It’s correct because inexperience of each explorer equals to 1, so it’s not less than the size of his group.

In the second example we can organize two groups. Explorers with inexperience 1, 2 and 3 will form the first group, and the other two explorers with inexperience equal to 2 will form the second group.

This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to 2, and the second group using only one explorer with inexperience equal to 1. In this case the young explorer with inexperience equal to 3 will not be included in any group.

题解:
首先把数组从小到大sort一遍,然后从头开始遍历。
每到一个数先++cnt,如果这时cnt==当前的a[i]说明能凑成一组了,直接ans++,同时cnt=0置零。
贪心策略就是用尽可能少的数凑出来一组,因此要从小到大遍历。 比如 1 1 3,从小到大能凑出两组:1和1,从大到小只能凑出一组3 1 1。

#include <bits/stdc++.h>
using namespace std;
int n,a[200005];

int main()
{
    
    
    int t;
    cin>>t;
    while(t--)
    {
    
    
        cin>>n;
        int i;
        for(i=1; i<=n; i++)  scanf("%d",&a[i]);
        sort(a+1,a+n+1);
        int ans=0;
        int cnt=0;
        a[n+1]=0x3f3f3f3f;
        for(i=1; i<=n+1; i++) {
    
    
            cnt++;
            if(a[i]==cnt) {
    
    
                ans++;
                cnt=0;
            }
        }
        cout<<ans<<endl;
    }
}

C. Count Triangles

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

Like any unknown mathematician, Yuri has favourite numbers: A, B, C, and D, where A≤B≤C≤D. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides x, y, and z exist, such that A≤x≤B≤y≤C≤z≤D holds?

Yuri is preparing problems for a new contest now, so he is very busy. That’s why he asked you to calculate the number of triangles with described property.

The triangle is called non-degenerate if and only if its vertices are not collinear.

Input
The first line contains four integers: A, B, C and D (1≤A≤B≤C≤D≤5⋅105) — Yuri’s favourite numbers.

Output
Print the number of non-degenerate triangles with integer sides x, y, and z such that the inequality A≤x≤B≤y≤C≤z≤D holds.

Examples
input
1 2 3 4
output
4
input
1 2 2 5
output
3
input
500000 500000 500000 500000
output
1
Note
In the first example Yuri can make up triangles with sides (1,3,3), (2,2,3), (2,3,3) and (2,3,4).

In the second example Yuri can make up triangles with sides (1,2,2), (2,2,2) and (2,2,3).

In the third example Yuri can make up only one equilateral triangle with sides equal to 5⋅105.

题意: 给定A、B、C、D 给定限定条件A<=x<=B<=y<=C<=z<=D 问有多少个(x,y,z)能组成三角形

思路:
题目即统计满足x+y<z的方案数 容易想到枚举一个,O(1)计算方案数,但是细节有点多,不好码
还有一种做法是枚举x+y,设当前枚举到的x+y为i 那么需要计算的有两个:
1.满足i<z的z的数量
2.满足x<=y且x+y=i的(x,y)的数量 考虑如何计算2: 如果x为A,那么y为i-A 如果x为A+1,那么y为i-A-1 … 如果x为B,那么y为i-B 容易观察到y的变化范围为[i-B,i-A] 不过y还需要在[B,C]之内,取区间交就行了

#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
    
    
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    int ans=0;
    for(int i=a+b;i<=b+c;i++) {
    
    
        if(i>=c) {
    
    
            int l=max(i-b,b),r=min(i-a,c);
            int cnt=min(i-c,d-c+1);
            ans+=cnt*(r-l+1);
        }
    }
    cout<<ans<<endl;
    return 0;
}

D. Game With Array

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

Petya and Vasya are competing with each other in a new interesting game as they always do.

At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0≤K≤S.

In order to win, Vasya has to find a non-empty subarray in Petya’s array such that the sum of all selected elements equals to either K or S−K. Otherwise Vasya loses.

You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.

Input
The first line contains two integers N and S (1≤N≤S≤106) — the required length of the array and the required sum of its elements.

Output
If Petya can win, print “YES” (without quotes) in the first line. Then print Petya’s array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.

If Petya can’t win, print “NO” (without quotes).

You can print each letter in any register (lowercase or uppercase).

Examples
input
1 4
output
YES
4
2
input
3 4
output
NO
input
3 8
output
YES
2 1 5
4
在这里插入图片描述

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

int main()
{
    
    
    int n,s;
    cin>>n>>s;
    if(s>=2*n) {
    
    
        puts("YES");
        for(int i=0;i<n-1;i++)  cout<<2<<" ";
        cout<<s-2*(n-1)<<endl<<1;
    }
    else puts("NO");
}

猜你喜欢

转载自blog.csdn.net/Luoxiaobaia/article/details/107621420