国庆10.3

版权声明:莉莉莉 https://blog.csdn.net/qq_41700151/article/details/83000669

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个长凳一开始每个长凳有一定的人数,后来又来了一堆人,问每个长凳最大人数k的最小值和最大值。
代码:

#include<iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long
const int MAX=1e6;
using namespace std;
int a[100+5];
int main()
{
    int n,m,maxx=-1;
    ios::sync_with_stdio(false);
    cin>>n>>m;
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        if(a[i]>maxx)
            maxx=a[i];
        sum+=a[i];
    }
    int ans1,ans2;
    ans2=maxx+m;
    sum+=m;
    if(sum%n!=0)
       ans1=sum/n+1;
    else
        ans1=sum/n;
    ans1=max(ans1,maxx);
    cout<<ans1<<" "<<ans2;
    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”.
有n种饮料,每种饮料都有某种维生素,求花最少的钱得到最多的维生素的最小钱数
要么ABC,要么AB和C,AC和B,BC和A,AB和BC,AB和AC,AC和BC,找出最少的花费
代码:

#include<iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f
const int MAX=1e6;
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    int a=INF,b=INF,c=INF,ab=INF,ac=INF,bc=INF,abc=INF;
    string s;
    int n,co;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>co>>s;
        if(s=="A")
            a=min(a,co);
        if(s=="B")
            b=min(b,co);
        if(s=="C")
            c=min(c,co);
        if(s=="AB"||s=="BA")
            ab=min(ab,co);
        if(s=="BC"||s=="CB")
            bc=min(bc,co);
        if(s=="AC"||s=="CA")
            ac=min(ac,co);
        if(s=="ABC"||s=="ACB"||s=="BCA"||s=="BAC"||s=="CAB"||s=="CBA")
            abc=min(abc,co);
    }
    int ans=INF;
    ans=min(ans,a+b+c);
    ans=min(ans,ab+bc);
    ans=min(ans,ab+ac);
    ans=min(ans,bc+ac);
    ans=min(ans,ab+c);
    ans=min(ans,ac+b);
    ans=min(ans,bc+a);
    ans=min(ans,abc);
    if(ans==INF)
        cout<<"-1"<<endl;
    else
        cout<<ans<<endl;

}

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

1.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;
2.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].
题意:有两种操作,一种是1 a b,给第b个数赋上a×b的值,把第a个数删去;另一种是2 a,把第a个数删除。最后数组内只剩一个最大的数,把这些操作输出,只输出其中一种。
当有0的时候,如果有奇数个负数,就把所有的0整合到一起,并把最大的负数整合到0上,然后把这个0删除,如果有偶数个负数,就只把所有0整合到一起然后删掉。
当没有零的时候,如果有负数个奇数,就删掉最大的奇数,如果有偶数个就不需要删除。

代码:

#include<iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
const int MAX=1e6;
using namespace std;
int a[200000+10];     //存输入的这些数
int w0[200000+10];    //记录0的位置
int main()
{
    int n;
    ios::sync_with_stdio(false);
    int d=0,sum=0,k=0;
    int maxx=-INF;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&a[i]);
        if(a[i]<0)
        {
            d++;
            if(a[i]>maxx)
                maxx=a[i],k=i;
        }
        if(a[i]==0)
            w0[++sum]=i;
    }
    if(sum!=0)
    {
        if(d%2!=0)
        {
                w0[++sum]=k;      //把最大负数也改成0并且放到w0数组
                a[k]=0;     
        }
    }
    else
    {
        if(d%2!=0)
        {
           printf("2 %d\n",k);
           a[k]=0;
        }
    }

    for(int i=1;i<sum;i++)       //把所有0和到一个位置
        printf("1 %d %d\n",w0[i],w0[i+1]);
    if(sum!=0&&sum!=n)      //特判一下全为0和只有0和一个负数时不删除这个数
            printf("2 %d\n",w0[sum]);
    int t;
    for(int i=1;i<=n;i++)
        if(a[i]!=0)
        {
            t=i;
            break;
        }

    for(int i=t+1;i<=n;i++)
    {
        if(a[i]!=0)
            printf("1 %d %d\n",t,i),t=i;
    }
    return 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).
代码:

#include<iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long
#define INF 0x3f3f3f3f
const int MAX=1e6;
using namespace std;
int main()
{
    string s[105];
    int t;
    int a[4];
    ios::sync_with_stdio(false);
    cin>>t;

    for(int i=1; i<=t; i++)
    {
        cin>>s[i];
        memset(a,0,sizeof(a));
        int flag=0;
        int n=s[i].size();
        for(int j=0; j<n; j++)
        {
            if(s[i][j]>='a'&&s[i][j]<='z')
                a[0]++;
            if(s[i][j]>='A'&&s[i][j]<='Z')
                a[1]++;
            if(s[i][j]>='0'&&s[i][j]<='9')
                a[2]++;
        }
        //cout<<a[0]<<a[1]<<a[2]<<endl;
        if(a[1]&&a[2]&&a[0])
        {
           cout<<s[i]<<endl;
            continue;
        }
        if(a[0]==0)
        {
            if(a[1]>=2)
            {
                for(int k=0; k<n; k++)
                    if(s[i][k]>='A'&&s[i][k]<='Z')
                    {
                        s[i][k]='a';
                        flag=1;
                        break;
                    }
            }
            if(!flag)
            {
                for(int k=0; k<n; k++)
                    if(s[i][k]>='0'&&s[i][k]<='9')
                    {
                        s[i][k]='a';
                        break;
                    }
            }
        }
        flag=0;
        if(a[1]==0)
        {
            if(a[0]>=2)
            {
                for(int k=0; k<n; k++)
                    if(s[i][k]>='a'&&s[i][k]<='z')
                    {
                        s[i][k]='A';
                        flag=1;
                        break;
                    }
            }
            if(!flag)
            {
                for(int k=0; k<n; k++)
                    if(s[i][k]>='0'&&s[i][k]<='9')
                    {
                        s[i][k]='A';
                        break;
                    }
            }
        }
        flag=0;
        if(a[2]==0)
        {
            if(a[1]>=2)
            {
                for(int k=0; k<n; k++)
                    if(s[i][k]>='A'&&s[i][k]<='Z')
                    {
                        s[i][k]='0';
                        flag=1;
                        break;
                    }
            }
            if(!flag)
            {
                for(int k=0; k<n; k++)
                    if(s[i][k]>='a'&&s[i][k]<='z')
                    {
                        s[i][k]='0';
                        break;
                    }
            }
        }
        cout<<s[i]<<endl;

    }
    return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_41700151/article/details/83000669
今日推荐