国庆10.2

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

A-Equality CodeForces - 1038A
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.

在n个字母里面找前k个中出现相同次数的总和,如果不够前k种字母就输出0
代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int main()
{
    int n,k;
    int a[27];
    memset(a,0,sizeof(a));
    string s;
    cin>>n>>k;
    cin>>s;
    int m=s.size();
    for(int i=0;i<m;i++)
        a[s[i]-'A'+1]++;
    sort(a+1,a+k+1);
    if(a[1]==0)
        cout<<"0"<<endl;
    else
        cout<<a[1]*k<<endl;
}

B. Non-Coprime Partition CodeForces - 1038B
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,把前n个数分成两个集合,要求这两个集合的和的gcd>=2
由等差数列可知前n-1项的和为n(n-1)/2,与n的gcd为n
并且当n为1或者2时不满足
代码:

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int main()
{
   int n;
   ios::sync_with_stdio(false);
   cin>>n;
   if(n==1||n==2)
   {
       cout<<"No"<<endl;
       return 0;
   }
   cout<<"Yes"<<endl;
   cout<<n-1;
   for(int i=1;i<=n-1;i++)
    cout<<" "<<i;
   cout<<endl;
   cout<<1<<" "<<n;
   return 0;

}

C-Gambling CodeForces - 1038C
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可以拿一张自己的牌,也可以把对方的一张牌拿走,求A与B的分的差值,A先手,先从大到小排序,如果A的牌比B的大,就拿自己的,如果比B的小就把他的删掉,一开始一直WA8,后来开了longlong
代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long
using namespace std;
ll a[100000+5];
ll b[100000+5];
bool cmp(ll a,ll b)
{
    return a>b;
}
int main()
{
    ll n;
    ll ans1=0,ans2=0;
    ios::sync_with_stdio(false);
    scanf("%lld",&n);
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    for(ll i=1; i<=n; i++)
        scanf("%lld",&a[i]);
    for(ll i=1; i<=n; i++)
        scanf("%lld",&b[i]);

    sort(a+1,a+n+1,cmp);
    sort(b+1,b+n+1,cmp);
    ll i=1,j=1;
    while(i<=n||j<=n)
    {
        if(a[i]>=b[j])
        {
            ans1+=a[i];
            ++i;
            if(b[j]>=a[i])
                ans2+=b[j],++j;
            else
               ++i;
        }
        else
        {
            ++j;
            if(b[j]<a[i])
                ++i;
            else
                ans2+=b[j],++j;
        }
    }
    printf("%lld\n",ans1-ans2);

}

Palindrome Dance CodeForces - 1040A
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.

已经有衣服的就不能再买了,没有衣服的可以买,并且要形成回文,问最少的花费是多少

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define ll long long
using namespace std;
int s[25];
int main()
{
    int n,a[2];
    int flag=0,ans=0;
    cin>>n>>a[0]>>a[1];
    for(int i=1; i<=n; i++)
        cin>>s[i];
    for(int i=1; i<=n/2; i++)
    {
        if(s[i]!=s[n-i+1])
        {
            if(s[i]==2&&s[n-i+1]!=2)
                ans+=a[s[n-i+1]];
            else if(s[n-i+1]==2&&s[i]!=2)
                ans+=a[s[i]];
            else
            {
                flag=1;
                break;
            }

        }
        if(s[i]==s[n-i+1]&&s[i]==2)
            ans+=2*min(a[0],a[1]);
    }
    if(n&1)
    {
        if(s[n/2+1]==2)
            ans+=min(a[0],a[1]);
    }
    if(flag==1)
        cout<<"-1"<<endl;
    else
        cout<<ans<<endl;
    return 0;
}

E-Shashlik Cooking CodeForces - 1040B
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.

一堆烤肉,如果翻第n个那么从{n-k到n+k}也都会翻,问如何用最少的次数把所有的烤肉都翻一遍,并且输出要翻的烤肉的序号
如果k=0,要把每个都翻一次;如果k<=2k+1,只需要翻一次,翻中间的那个,如果大于就分情况讨论
代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <cstdlib>
#include <algorithm>
#define ll long long
using namespace std;
int a[1000+5];
int main()
{
    int n,k,s,ans=0;
    ios::sync_with_stdio(false);
    cin>>n>>k;
    memset(a,0,sizeof(a));
    if(k==0)
    {
        cout<<n<<endl;
        for(int i=1;i<=n;i++)
        {
            if(i!=n)
                cout<<i<<" ";
            else
                cout<<i;
        }
    }
    else if(n<=2*k+1)
    {
        cout<<"1"<<endl;
        cout<<n/2+1;
    }
    else
    {
        s=2*k+1;
        if(n%s>k||!(n%s))
        {
            for(int i=k+1;i<=n;i+=s)         
                a[i]=1,ans++;
        }
        else
        {
            for(int i=1;i<=n;i+=s)         
                a[i]=1,ans++;
        }
        cout<<ans<<endl;
        int kk;
        for(int i=1;i<=n;i++)
            if(a[i]==1)
            {
                cout<<i;
                kk=i;
                break;
            }
        for(int i=kk+1;i<=n;i++)
            if(a[i]==1)
             cout<<" "<<i;
    }
}

猜你喜欢

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