国庆第二场训练赛

A

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≤105) 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
Input
9 3
ACAABCCAB
Output
6
Input
9 4
ABCABCABC
Output
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.

B

Find out if it is possible to partition the first n positive integers into two non-empty disjoint sets S1 and S2 such that:

gcd(sum(S1),sum(S2))>1
Here sum(S) denotes the sum of all elements present in set S and gcd means thegreatest common divisor.

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

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

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

Otherwise, print “Yes” (quotes for clarity), followed by two lines, describing S1 and S2 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
Input
1
Output
No
Input
3
Output
Yes
1 2
2 1 3
Note
In the first example, there is no way to partition a single number into two non-empty sets, hence the answer is “No”.

In the second example, the sums of the sets are 2 and 4 respectively. The gcd(2,4)=2>1, hence that is one of the possible answers.
题意:

给定 n 个数,选出某些数组成集合S1,剩余的数组成集合S2

如果 GCD(S1,S2)> 1 的话,按要求输出即可

思路:

样例输出有多种,可以指定 S1集合里边只有一位数,那么S2集合里存的是剩余的数

1、首先当 n=1||n=2 的时候,是不符合题意的,可以直接输出NO\2

2、sum(n)= n×(n+1)/ 2 ;

遍历1-n中的数,如果 GCD( i,sum-i) > 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 gcd(int a,int b)
{
    return b>0?gcd(b,a%b):a;
}
int main()
{
    int n,m,sum;
    cin>>n;
    if(n<=2)
        cout<<"No"<<endl;
    else
    {
        cout<<"Yes"<<endl;
        int x,flag=0,t;
        sum=n*(n+1)/2;
        for(int i=1;i<=n;i++)
        {
            x=sum-i;
            if(gcd(i,x)>1)
            {
                flag=1;
                t=i;
                break;
            }
        }
        if(flag)
        {
            cout<<1<<" "<<t<<endl;
            cout<<n-1;
            for(int i=1;i<=n;i++)
            {
                if(i!=t)
                cout<<" "<<i;
            }
            cout<<endl;
        }
    }
    return 0;
}

C

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≤100000) — the sizes of the list.

The second line contains n integers ai (1≤ai≤106), describing the list of the player A, who starts the game.

The third line contains n integers bi (1≤bi≤106), 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
Input
2
1 4
5 1
Output
0
Input
3
100 100 100
100 100 100
Output
0
Input
2
2 1
5 6
Output
-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.
题意:

A B 两人各有一列数字,每进行一轮游戏,A B 都可以选择去掉对方中的某一个数,或者是从自己列表中选择一个数字加到自己的分数里边,并且两人都是采取的最好的策略,每一轮游戏的时候都想使自己的分数尽可能的大,最后输出 A-B 的值

思路:

先将列表中得数由大到小排序,每进行一轮游戏的时候,如果对方得数比自己当前的数字大,就选择将对方的数字去掉

否则,将该数字加到自己的分数上边(每一轮都采取最好的策略,最好使自己的分数尽可能的大)

#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()
{
    LL n;
    LL a[100010],b[100010];
    cin>>n;
    for(LL i=0; i<n; i++)
        cin>>a[i];
    for(LL i=0; i<n; i++)
        cin>>b[i];
    sort(a,a+n,cmp);
    sort(b,b+n,cmp);
    LL i=0,j=0,sum1=0,sum2=0,flag=0;
    while(i<n||j<n)
    {
        if(flag%2==0)
        {
            if(a[i]<=b[j])
                j++;
            else
            {
                sum1+=a[i];
                i++;
            }

        }
        else
        {
            if(b[j]<=a[i])
                i++;
            else
            {
                sum2+=b[j];
                j++;
            }

        }
        flag++;
    }
    cout<<sum1-sum2<<endl;
    return 0;
}

D

A group of n dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they’ve studied their dancing moves and can’t change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.

On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer’s suit is the same as the color of the rightmost dancer’s suit, the 2nd left is the same as 2nd right, and so on.

The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it’s possible, what’s the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.

Input
The first line contains three integers n, a, and b (1≤n≤20, 1≤a,b≤100) — the number of dancers, the cost of a white suit, and the cost of a black suit.

The next line contains n numbers ci, i-th of which denotes the color of the suit of the i-th dancer. Number 0 denotes the white color, 1 — the black color, and 2 denotes that a suit for this dancer is still to be bought.

Output
If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect.

Examples
Input
5 100 1
0 1 2 1 2
Output
101
Input
3 10 12
1 2 0
Output
-1
Input
3 12 1
0 1 0
Output
0
Note
In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer.

In the second sample, the leftmost dancer’s suit already differs from the rightmost dancer’s suit so there is no way to obtain the desired coloring.

In the third sample, all suits are already bought and their colors form a palindrome.
题意:

给定一个字符串,已知有些位置上是'W'(用 0表示) ,有些位置上是'B'(用1表示),有些位置不确定(用2表示)

你的任务是给 不确定位置上 (2)买一些衣服,使得该字符串是一个回文串,并且要求所花费的价钱最少

已知买白色需要a,买黑色需要b

思路:

分类讨论(暴力)

1、1个人的时候,如果是0、1已经确定好得数,不需要花费,如果是2,花费min(a,b)

2、如果首位与末位不相等的话,不能构成回文数

3、讨论对应位置取值情况 (循环跑对称)

0 0

1 1     以上两种不需要花费

0 2

2 0     以上两种需要花费 a

1 2

2 1     以上两种需要花费 b

2 2     需要花费 2×min(a,b)

0 1

1 0     以上两种不满足题意

4、当n是奇数的时候,对称位置在循环中是取不到的,所以特判一下对称位置的数值

#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,a,b,sum=0,flag=1;
    int c[50];
    cin>>n>>a>>b;
    for(int i=0; i<n; i++)
        cin>>c[i];
//    if(a<b)
//        swap(a,b);
    for(int i=0; i<n/2; i++)
    {
        if(c[i]==c[n-i-1]&&c[i]!=2&&c[n-i-1]!=2)
            continue;
        if(c[i]!=c[n-i-1]&&c[i]==2)
        {
            if(c[n-i-1]==0)
                sum+=a;
            if(c[n-i-1]==1)
                sum+=b;
        }
        if(c[i]!=c[n-i-1]&&c[n-i-1]==2)
        {
            if(c[i]==0)
                sum+=a;
            if(c[i]==1)
                sum+=b;
        }
        if(c[n-i-1]==2&&c[i]==2)
            sum+=2*min(a,b);
        if(c[i]!=c[n-i-1]&&c[i]!=2&&c[n-i-1]!=2)
        {
            cout<<"-1"<<endl;
            flag=0;
            break;
        }
    }
    if(flag)
    {
        if(n%2==1)
        {
            if(c[n/2]==2)
                cout<<sum+min(a,b)<<endl;
            else
                cout<<sum<<endl;
        }
        else
            cout<<sum<<endl;
    }
    return 0;
}

E

Long story short, shashlik is Miroslav’s favorite food. Shashlik is prepared on several skewers simultaneously. There are two states for each skewer: initial and turned over.

This time Miroslav laid out n skewers parallel to each other, and enumerated them with consecutive integers from 1 to n in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number i, it leads to turning k closest skewers from each side of the skewer i, that is, skewers number i−k, i−k+1, …, i−1, i+1, …, i+k−1, i+k (if they exist).

For example, let n=6 and k=1. When Miroslav turns skewer number 3, then skewers with numbers 2, 3, and 4 will come up turned over. If after that he turns skewer number 1, then skewers number 1, 3, and 4 will be turned over, while skewer number 2 will be in the initial position (because it is turned again).

As we said before, the art of cooking requires perfect timing, so Miroslav wants to turn over all n skewers with the minimal possible number of actions. For example, for the above example n=6 and k=1, two turnings are sufficient: he can turn over skewers number 2 and 5.

Help Miroslav turn over all n skewers.

Input
The first line contains two integers n and k (1≤n≤1000, 0≤k≤1000) — the number of skewers and the number of skewers from each side that are turned in one step.

Output
The first line should contain integer l — the minimum number of actions needed by Miroslav to turn over all n skewers. After than print l integers from 1 to n denoting the number of the skewer that is to be turned over at the corresponding step.

Examples
Input
7 2
Output
2
1 6
Input
5 1
Output
2
1 4
Note
In the first example the first operation turns over skewers 1, 2 and 3, the second operation turns over skewers 4, 5, 6 and 7.

In the second example it is also correct to turn over skewers 2 and 5, but turning skewers 2 and 4, or 1 and 5 are incorrect solutions because the skewer 3 is in the initial state after these operations.

猜你喜欢

转载自blog.csdn.net/qq_38984851/article/details/83076191