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

國慶第二場訓練賽

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.