【Codeforces Round #508div2】(A,B,C,D)

A. Equality
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string
s
of length
n
, which consists only of the first
k
letters of the Latin alphabet. All letters in string
s
are uppercase.

A subsequence of string
s
is a string that can be derived from
s
by deleting some of its symbols without changing the order of the remaining symbols. For example, “ADE” and “BD” are subsequences of “ABCDE”, but “DEA” is not.

A subsequence of
s
called good if the number of occurences of each of the first
k
letters of the alphabet is the same.

Find the length of the longest good subsequence of
s
.

Input
The first line of the input contains integers
n
(
1

n

10
5
) and
k
(
1

k

26
).

The second line of the input contains the string
s
of length
n
. String
s
only contains uppercase letters from ‘A’ to the
k
-th letter of Latin alphabet.

Output
Print the only integer — the length of the longest good subsequence of string
s
.

Examples
inputCopy
9 3
ACAABCCAB
outputCopy
6
inputCopy
9 4
ABCABCABC
outputCopy
0
Note
In the first example, “ACBCAB” (“ACAABCCAB”) is one of the subsequences that has the same frequency of ‘A’, ‘B’ and ‘C’. Subsequence “CAB” also has the same frequency of these letters, but doesn’t have the maximum possible length.

In the second example, none of the subsequences can have ‘D’, hence the answer is
0
.
solve: a n s = k m i n ( c [ 0 ] . . . . . . c [ k ] ) ;
c [ i ] i + A

#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
string s;
int re[26];
int main(){
    int n,k;
    cin>>n>>k;
    cin>>s;
    for(int i=0;i<n;i++){
        re[s[i]-'A']++;
    }
    int ans=INF;
    for(int i=0;i<k;i++){
        ans=min(ans,re[i]);
    }
    cout<<k*ans<<endl;
    return 0;
}

B. Non-Coprime Partition
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Find out if it is possible to partition the first
n
positive integers into two non-empty disjoint sets
S
1
and
S
2
such that:

g
c
d
(
s
u
m
(
S
1
)
,
s
u
m
(
S
2
)
)
>
1
Here
s
u
m
(
S
)
denotes the sum of all elements present in set
S
and
g
c
d
means thegreatest common divisor.

Every integer number from
1
to
n
should be present in exactly one of
S
1
or
S
2
.

Input
The only line of the input contains a single integer
n
(
1

n

45
000
)

Output
If such partition doesn’t exist, print “No” (quotes for clarity).

Otherwise, print “Yes” (quotes for clarity), followed by two lines, describing
S
1
and
S
2
respectively.

Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty.

If there are multiple possible partitions — print any of them.

Examples
inputCopy
1
outputCopy
No
inputCopy
3
outputCopy
Yes
1 2
2 1 3
solve:只有n=1或n=2的时候”No”,其他的直接暴力判断即可,只需要找到一个属即可满足。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
int main(){
    ll n;
    cin>>n;
    if(n==1||n==2){
        cout<<"No"<<endl;
    }
    else{
        ll s=n*(n+1)/2;
        ll re;
        bool flag=0;
        for(int i=1;i<=n;i++){
            if(gcd(s-i,i)>1){
                re=i;
                flag=1;
                break;
            }
        }
        if(flag){
            puts("Yes");
            cout<<1<<" "<<re<<endl;
            cout<<n-1;
            for(int i=1;i<=n;i++){
                if(i!=re) cout<<" "<<i;
            }
            puts("");
        }
        else{
            puts("No");
        }
    }
    return 0;
}

C. Gambling
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Two players A and B have a list of
n
integers each. They both want to maximize the subtraction between their score and their opponent’s score.

In one turn, a player can either add to his score any element from his list (assuming his list is not empty), the element is removed from the list afterward. Or remove an element from his opponent’s list (assuming his opponent’s list is not empty).

Note, that in case there are equal elements in the list only one of them will be affected in the operations above. For example, if there are elements
{
1
,
2
,
2
,
3
}
in a list and you decided to choose
2
for the next turn, only a single instance of
2
will be deleted (and added to the score, if necessary).

The player A starts the game and the game stops when both lists are empty. Find the difference between A’s score and B’s score at the end of the game, if both of the players are playing optimally.

Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves. In this problem, it means that each player, each time makes a move, which maximizes the final difference between his score and his opponent’s score, knowing that the opponent is doing the same.

Input
The first line of input contains an integer
n
(
1

n

100
000
) — the sizes of the list.

The second line contains
n
integers
a
i
(
1

a
i

10
6
), describing the list of the player A, who starts the game.

The third line contains
n
integers
b
i
(
1

b
i

10
6
), describing the list of the player B.

Output
Output the difference between A’s score and B’s score (
A

B
) if both of them are playing optimally.

Examples
inputCopy
2
1 4
5 1
outputCopy
0
inputCopy
3
100 100 100
100 100 100
outputCopy
0
inputCopy
2
2 1
5 6
outputCopy
-3
Note
In the first example, the game could have gone as follows:

A removes
5
from B’s list.
B removes
4
from A’s list.
A takes his
1
.
B takes his
1
.
Hence, A’s score is
1
, B’s score is
1
and difference is
0
.

There is also another optimal way of playing:

A removes
5
from B’s list.
B removes
4
from A’s list.
A removes
1
from B’s list.
B removes
1
from A’s list.
The difference in the scores is still
0
.

In the second example, irrespective of the moves the players make, they will end up with the same number of numbers added to their score, so the difference will be
0
.
solve:直接模拟即可,每到一个人选择,他会比较两方当前最大值的大小。
自己大就拿,对手大就把对手的撤走,如果一方为空,只能撤走对方的最大值。

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

using namespace std;
int main(){
    ll n;
    scanf("%I64d",&n);
    vector<ll> a(n);
    vector<ll> b(n);
    for(ll i=0;i<n;i++){
        scanf("%I64d",&a[i]);
    }
    for(ll i=0;i<n;i++){
        scanf("%I64d",&b[i]);
    }
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    ll ta=0;
    ll tb=0;
    for(ll i=0;i<n;i++){
        if(((!a.empty()) && a.back() > b.back())||b.empty()){
            ta+=a.back();
            a.pop_back();
        }
        else{
            b.pop_back();
        }
        if(((!b.empty()) && b.back() > a.back())||a.empty()){
            tb+=b.back();
            b.pop_back();
        }
        else{
            a.pop_back();
        }
    }
    printf("%I64d\n",ta-tb);
    return 0;
}

D. Slime
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
There are
n
slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it.

Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists).

When a slime with a value
x
eats a slime with a value
y
, the eaten slime disappears, and the value of the remaining slime changes to
x

y
.

The slimes will eat each other until there is only one slime left.

Find the maximum possible value of the last slime.

Input
The first line of the input contains an integer
n
(
1

n

500
000
) denoting the number of slimes.

The next line contains
n
integers
a
i
(

10
9

a
i

10
9
), where
a
i
is the value of
i
-th slime.

Output
Print an only integer — the maximum possible value of the last slime.

Examples
inputCopy
4
2 1 2 1
outputCopy
4
inputCopy
5
0 -1 -1 -1 -1
outputCopy
4
Note
In the first example, a possible way of getting the last slime with value
4
is:

Second slime eats the third slime, the row now contains slimes
2
,

1
,
1
Second slime eats the third slime, the row now contains slimes
2
,

2
First slime eats the second slime, the row now contains
4
In the second example, the first slime can keep eating slimes to its right to end up with a value of
4
.
solve: n = 1 需要特判,接下来考虑数组。
1 数组中既有正又有负,ans= Σ a b s ( a [ i ] ) ;
2 只有正数或负数,那么我们把最小的那个值变成和其他数相反的类型。
先求和 s u m , a n s = s u m a b s ( m i n ) + m i n

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll maxn = 1000050;
ll a[maxn];
int main(){
    ll n;
    cin>>n;
    ll sum=0;
    ll minn=INF;
    for(ll i=0;i<n;i++){
        cin>>a[i];
    }
    if(n==1) {
        cout<<a[0]<<endl;
    }
    else{
    ll flag=0;
    for(ll i=0;i<n;i++){
        if(a[i]>=0){
            flag=1;
            break;
        }
    }
    if(!flag){
        for(ll i=0;i<n;i++){
            a[i]=-a[i];
        }
    }
    for(ll i=0;i<n;i++){
        sum+=abs(a[i]);
        minn=min(minn,a[i]);
    }
    printf("%I64d\n",sum-abs(minn)-minn);
}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/duanghaha/article/details/82490610