1. 程式人生 > >國慶第三場訓練賽

國慶第三場訓練賽

A - A CodeForces - 1042A

There are n benches in the Berland Central park. It is known that ai people are currently sitting on the i-th bench. Another m people are coming to the park and each of them is going to have a seat on some bench out of n available.

Let k be the maximum number of people sitting on one bench after additional m people came to the park. Calculate the minimum possible k and the maximum possible k.

Nobody leaves the taken seat during the whole process.

Input
The first line contains a single integer n (1≤n≤100) — the number of benches in the park.

The second line contains a single integer m (1≤m≤10000) — the number of people additionally coming to the park.

Each of the next n lines contains a single integer ai (1≤ai≤100) — the initial number of people on the i-th bench.

Output
Print the minimum possible k and the maximum possible k, where k is the maximum number of people sitting on one bench after additional m people came to the park.

Examples
Input
4
6
1
1
1
1
Output
3 7
Input
1
10
5
Output
15 15
Input
3
6
1
6
5
Output
6 12
Input
3
7
1
6
5
Output
7 13
Note
In the first example, each of four benches is occupied by a single person. The minimum k is 3. For example, it is possible to achieve if two newcomers occupy the first bench, one occupies the second bench, one occupies the third bench, and two remaining — the fourth bench. The maximum k is 7. That requires all six new people to occupy the same bench.

The second example has its minimum k equal to 15 and maximum k equal to 15, as there is just a single bench in the park and all 10 people will occupy it.
題意:

有n個長椅,椅子上起初有 a【i】個人,現在增加m個人,這 m個人可以隨意選擇座位

k代表的是最大數量的人坐在長椅上,最後求k的最大值與最小值

k(max) = 所有的m個人坐在起初人最多的地方

k(min) = 既保證坐的人數量最大,又要求此時的k最小,那麼只能是平均分

需要注意的是:

求 k的最小值的時候,平均分完人數之後,很有可能均分的人數是要比你起初最大的人數要小,所以需要比較一下(我這裡就是卡住了,一直WA下去)

當然只有一個長椅的時候,不管來多少人,都只能坐在同一個位置,k的最大與最小值是相同的

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
typedef long long LL;
using namespace std;
const int INF = 0x3f3f3f3f;
int cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int n,m,a[110],sum=0,maxn=0,x,y;
    cin>>n;
    cin>>m;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        sum+=a[i];
        maxn=max(maxn,a[i]);
    }
    y=maxn+m;
    sum+=m;
    if(sum%n)
    {
        x=sum/n+1;
    }
    else
        x=sum/n;
    x=max(x,maxn);
    cout<<x<<" "<<y<<endl;
    return 0;
}

B - B CodeForces - 1042B

Berland shop sells n kinds of juices. Each juice has its price ci. Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin “A”, vitamin “B” and vitamin “C”. Each juice can contain one, two or all three types of vitamins in it.

Petya knows that he needs all three types of vitamins to stay healthy. What is the minimum total price of juices that Petya has to buy to obtain all three vitamins? Petya obtains some vitamin if he buys at least one juice containing it and drinks it.

Input
The first line contains a single integer n (1≤n≤1000) — the number of juices.

Each of the next n lines contains an integer ci (1≤ci≤100000) and a string si — the price of the i-th juice and the vitamins it contains. String si contains from 1 to 3 characters, and the only possible characters are “A”, “B” and “C”. It is guaranteed that each letter appears no more than once in each string si. The order of letters in strings si is arbitrary.

Output
Print -1 if there is no way to obtain all three vitamins. Otherwise print the minimum total price of juices that Petya has to buy to obtain all three vitamins.

Examples
Input
4
5 C
6 B
16 BAC
4 A
Output
15
Input
2
10 AB
15 BA
Output
-1
Input
5
10 A
9 BC
11 CA
4 A
5 B
Output
13
Input
6
100 A
355 BCA
150 BC
160 AC
180 B
190 CA
Output
250
Input
2
5 BA
11 CB
Output
16
Note
In the first example Petya buys the first, the second and the fourth juice. He spends 5+6+4=15 and obtains all three vitamins. He can also buy just the third juice and obtain three vitamins, but its cost is 16, which isn’t optimal.

In the second example Petya can’t obtain all three vitamins, as no juice contains vitamin “C”.
題意:

Petya想要去補充一些維他命,他必須通過買一些果汁並喝掉才能吸收果汁中的維他命,這些果汁各有一些價格

維他命包括 A、B、C,已知某些果汁中含有不同型別的維他命,有的含有 A、AB、ABC、B、等等

求補充A、B、C 維他命所需要的最少費用是多少

思路:

對於這個題我就是完全暴力模擬求解了

我用了結構體和好多陣列,後來發現結構體是用不到的

如果想補充A、B、C的話,可以分為以下幾種型別:

1、買含有單獨的A、B、C的果汁

2、買含有 ABC的果汁

3、買兩瓶

AB+BC、AB+AC、AB+C、BC+AB、BC+AC、BC+A、AC+AB、AC+BC、AC+B

注意到有重複的情況,去重之後為

AB+BC、AB+AC、AB+C、BC+AC、BC+A、AC+B

然後去比較這幾種情況哪一種花的錢數最少就可以

首先給價格賦值為最大 ,但需要注意的是不能定義為 0x3f3f3f3f ,當三個都為最大值加起來的時候,有可能超int 的範圍,所以定義 INF= 0x3f3f3f

需要注意最後一種樣例

並沒有出現A、B、C、這一種情況,所以ans=v(A)+v(B)+v(C)> INF ,輸出 %d的形式是 < 0 的,

所以特判一下,如果值小於0的話,直接定義為最大INF

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
typedef long long LL;
using namespace std;
const int INF = 0x3f3f3f;
int cmp(int a,int b)
{
    return a>b;
}
int gcd(int a,int b)
{
    return b>0?gcd(b,a%b):a;
}
int main()
{
    int n,min1=INF,min2=INF,min3=INF,minn=INF,min4=INF,min5=INF,min6=INF;
    string s;
    int v;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>v>>s;
        if(s=="A")
        {
            min1=min(v,min1);
        }
        if(s=="B")
        {
            min2=min(v,min2);
        }
        if(s=="C")
        {
            min3=min(v,min3);
        }
        if(s.size()>2)
        {
            minn=min(minn,v);
        }
        if(s=="AB"||s=="BA")
        {
            min4=min(v,min4);
        }
        if(s=="BC"||s=="CB")
        {
            min5=min(v,min5);
        }
        if(s=="AC"||s=="CA")
        {
            min6=min(v,min6);
        }
    }
    int ans=INF;
    ans=min(ans,min1+min2+min3);
    ans=min(ans,min1+min5);
    ans=min(ans,min2+min6);
    ans=min(ans,min3+min4);
    ans=min(ans,min4+min5);
    ans=min(ans,min4+min6);
    ans=min(ans,min5+min6);
    ans=min(ans,minn);
    if(ans!=INF)
        cout<<ans<<endl;
    else
        cout<<"-1"<<endl;
    return 0;
}

C - C CodeForces - 1042C

You are given an array a consisting of n integers. You can perform the following operations with it:

Choose some positions i and j (1≤i,j≤n,i≠j), write the value of ai⋅aj into the j-th cell and remove the number from the i-th cell;
Choose some position i and remove the number from the i-th cell (this operation can be performed no more than once and at any point of time, not necessarily in the beginning).
The number of elements decreases by one after each operation. However, the indexing of positions stays the same. Deleted numbers can’t be used in the later operations.

Your task is to perform exactly n−1 operations with the array in such a way that the only number that remains in the array is maximum possible. This number can be rather large, so instead of printing it you need to print any sequence of operations which leads to this maximum number. Read the output format to understand what exactly you need to print.

Input
The first line contains a single integer n (2≤n≤2⋅105) — the number of elements in the array.

The second line contains n integers a1,a2,…,an (−109≤ai≤109) — the elements of the array.

Output
Print n−1 lines. The k-th line should contain one of the two possible operations.

The operation of the first type should look like this: 1 ik jk, where 1 is the type of operation, ik and jk are the positions of the chosen elements.

The operation of the second type should look like this: 2 ik, where 2 is the type of operation, ik is the position of the chosen element. Note that there should be no more than one such operation.

If there are multiple possible sequences of operations leading to the maximum number — print any of them.

Examples
Input
5
5 -2 0 1 -3
Output
2 3
1 1 2
1 2 4
1 4 5
Input
5
5 2 0 4 0
Output
1 3 5
2 5
1 1 2
1 2 4
Input
2
2 -1
Output
2 2
Input
4
0 -10 0 0
Output
1 1 2
1 2 3
1 3 4
Input
4
0 0 0 0
Output
1 1 2
1 2 3
1 3 4
Note
Let X be the removed number in the array. Let’s take a look at all the examples:

The first example has, for example, the following sequence of transformations of the array: [5,−2,0,1,−3]→[5,−2,X,1,−3]→[X,−10,X,1,−3]→ [X,X,X,−10,−3]→[X,X,X,X,30]. Thus, the maximum answer is 30. Note, that other sequences that lead to the answer 30 are also correct.

The second example has, for example, the following sequence of transformations of the array: [5,2,0,4,0]→[5,2,X,4,0]→[5,2,X,4,X]→[X,10,X,4,X]→ [X,X,X,40,X]. The following answer is also allowed:

1 5 3
1 4 2
1 2 1
2 3
Then the sequence of transformations of the array will look like this: [5,2,0,4,0]→[5,2,0,4,X]→[5,8,0,X,X]→[40,X,0,X,X]→ [40,X,X,X,X].

The third example can have the following sequence of transformations of the array: [2,−1]→[2,X].

The fourth example can have the following sequence of transformations of the array: [0,−10,0,0]→[X,0,0,0]→[X,X,0,0]→[X,X,X,0].

The fifth example can have the following sequence of transformations of the array: [0,0,0,0]→[X,0,0,0]→[X,X,0,0]→[X,X,X,0].

D - D CodeForces - 1051A

Vasya came up with a password to register for EatForces — a string s. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits.

But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords “abaCABA12”, “Z7q” and “3R24m” are valid, and the passwords “qwerty”, “qwerty12345” and “Password” are not.

A substring of string s is a string x=slsl+1…sl+len−1(1≤l≤|s|,0≤len≤|s|−l+1). len is the length of the substring. Note that the empty string is also considered a substring of s, it has the length 0.

Vasya’s password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length.

Note that the length of s should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits.

Input
The first line contains a single integer T (1≤T≤100) — the number of testcases.

Each of the next T lines contains the initial password s (3≤|s|≤100), consisting of lowercase and uppercase Latin letters and digits.

Only T=1 is allowed for hacks.

Output
For each testcase print a renewed password, which corresponds to given conditions.

The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is 0. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords “abcdef” → “a7cdEf” is 4, because the changed positions are 2 and 5, thus (5−2)+1=4.

It is guaranteed that such a password always exists.

If there are several suitable passwords — output any of them.

Example
Input
2
abcDCE
htQw27
Output
abcD4E
htQw27
Note
In the first example Vasya’s password lacks a digit, he replaces substring “C” with “4” and gets password “abcD4E”. That means, he changed the substring of length 1.

In the second example Vasya’s password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0).
題意:給一個密碼s,然後輸出包含大小寫字母和數字

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
typedef long long LL;
using namespace std;
const int INF = 0x3f3f3f;
int cmp(int a,int b)
{
    return a>b;
}
int gcd(int a,int b)
{
    return b>0?gcd(b,a%b):a;
}
int main()
{
    int t;
    string s;
    int a,b,c,aa,bb,cc;
    cin>>t;
    while(t--)
    {
        a=0,b=0,c=0;
        cin>>s;
        for(int i=0; i<s.size(); i++)
        {
            if(s[i]>='A'&&s[i]<='Z')
                a++,aa=i;
            if(s[i]>='a'&&s[i]<='z')
                b++,bb=i;
            if(s[i]>='0'&&s[i]<='9')
                c++,cc=i;
        }
        if(a!=0&&b!=0&&c!=0)
            cout<<s<<endl;
        else
        {
            if(a==0)
            {
                if(b>=2)
                {
                    for(int i=0; i<s.size(); i++)
                    {
                        if(s[i]>='a'&&s[i]<='z')
                        {
                            s[i]='A';
                            break;
                        }
                    }
                }
                else
                {
                    for(int i=0; i<s.size(); i++)
                    {
                        if(s[i]>='0'&&s[i]<='9')
                        {
                            s[i]='A';
                            break;
                        }
                    }
                }
            }
            if(b==0)
            {
                if(a>=2)
                {
                    for(int i=0; i<s.size(); i++)
                    {
                        if(s[i]>='A'&&s[i]<='Z')
                        {
                            s[i]='a';
                            break;
                        }
                    }
                }
                else
                {
                    for(int i=0; i<s.size(); i++)
                    {
                        if(s[i]>='0'&&s[i]<='9')
                        {
                            s[i]='a';
                            break;
                        }
                    }
                }
            }
            if(c==0)
            {
                if(a>=2)
                {
                    for(int i=0; i<s.size(); i++)
                    {
                        if(s[i]>='A'&&s[i]<='Z')
                        {
                            s[i]='1';
                            break;
                        }
                    }
                }
                else
                {
                    for(int i=0; i<s.size(); i++)
                    {
                        if(s[i]>='a'&&s[i]<='z')
                        {
                            s[i]='1';
                            break;
                        }
                    }
                }
            }
            cout<<s<<endl;
        }
    }
    return 0;
}

E - E CodeForces - 1051B

You are given a set of all integers from l to r inclusive, l<r, (r−l+1)≤3⋅105 and (r−l) is always odd.

You want to split these numbers into exactly r−l+12 pairs in such a way that for each pair (i,j) the greatest common divisor of i and j is equal to 1. Each number should appear in exactly one of the pairs.

Print the resulting pairs or output that no solution exists. If there are multiple solutions, print any of them.

Input
The only line contains two integers l and r (1≤l<r≤1018, r−l+1≤3⋅105, (r−l) is odd).

Output
If any solution exists, print “YES” in the first line. Each of the next r−l+12 lines should contain some pair of integers. GCD of numbers in each pair should be equal to 1. All (r−l+1) numbers should be pairwise distinct and should have values from l to r inclusive.

If there are multiple solutions, print any of them.

If there exists no solution, print “NO”.

Example
Input
1 8
Output
YES
2 7
4 1
3 8
6 5
題意:找出l~r之間gcd為1的數對,輸出多種情況,所以我們可以觀察的到連續的奇數和偶數在一起的話,gcd就是1.

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
typedef long long LL;
using namespace std;
const int INF = 0x3f3f3f3f;
int cmp(int a,int b)
{
    return a>b;
}
int gcd(int a,int b)
{
    return b>0?gcd(b,a%b):a;
}
int main()
{
    LL l,r,c=1,d=1;
    cin>>l>>r;
    cout<<"YES"<<endl;
    for(LL i=l;i<=r;i++)
    {
        if(i%2==0)
            cout<<i<<endl;
        else
            cout<<i<<endl;
    }
    return 0;
}