• 2018SDIBT_国庆个人第二场


    A.codeforces1038A

    You are given a string ss of length nn, which consists only of the first kk letters of the Latin alphabet. All letters in string ss are uppercase.

    subsequence of string ss is a string that can be derived from ss 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 ss called good if the number of occurences of each of the first kkletters of the alphabet is the same.

    Find the length of the longest good subsequence of ss.

    Input

    The first line of the input contains integers nn (1n1051≤n≤105) and kk (1k261≤k≤26).

    The second line of the input contains the string ss of length nn. String ss only contains uppercase letters from 'A' to the kk-th letter of Latin alphabet.

    Output

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

    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 00.


    题意:给你一个字符串s长度为n,让你求的字符串只包含k种字符且出现次数相等,你可以对字符串s进行删减操作,输出求得的字符串的长度
    分析:只要找到字符串s里出现次数最少的字符,将它的出现次数乘以k,还要考虑当s里字符种数小于K的情况
     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 using namespace std;
     5 int main()
     6 {
     7     int n,k,a[100],b[100];
     8     char s[100005];
     9     while(~scanf("%d %d",&n,&k))
    10     {
    11         memset(a,0,sizeof(a));//a数组用来统计字符种数,如果出现了,就赋为1
    12         memset(b,0,sizeof(b));//b数组用来统计每个字符出现的次数
    13         scanf("%s",s);
    14         int kk=0;
    15         for(int i=0;i<n;i++)
    16         {
    17         if(a[s[i]-'A']==0)
    18         {
    19             kk++;//kk就表示s里字符的种数
    20             a[s[i]-'A']=1;
    21         }
    22         b[s[i]-'A']++;
    23         }
    24         sort(b,b+k);//排序,找到出现次数最小的字符
    25         if(kk!=k)
    26         printf("0
    ");//如果kk不等于给出的k时,输出0
    27         else
    28         printf("%d
    ",b[0]*k);//最小的出现次数乘以字符种类数
    29 
    30     }
    31     return 0;
    32 }

    B.codeforces1038B

    Find out if it is possible to partition the first nn positive integers into two non-empty disjoint sets S1S1 and S2S2 such that:

    gcd(sum(S1),sum(S2))>1gcd(sum(S1),sum(S2))>1

    Here sum(S)sum(S) denotes the sum of all elements present in set SS and gcdgcd means thegreatest common divisor.

    Every integer number from 11 to nn should be present in exactly one of S1S1 or S2S2.

    Input

    The only line of the input contains a single integer nn (1n450001≤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 S1S1and S2S2 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 22 and 44 respectively. The gcd(2,4)=2>1gcd(2,4)=2>1, hence that is one of the possible answers.

    错了两发很是心痛,No,NO,Yes,YES其实很不一样

    这题挺简单的,实际上就是构造。

    题意:给你一个n,让你把1 to n的所有数分成两组,使得这两组的和的最大公因数不等于1,输出这两组数

    分析:当n=1或者n=2时输出No,

               当n大于2时,如果n为奇数,第一组数为中间那个数,第二组数为除开中间数的所有数,因为最中间数的旁边两个数之和一定是它的两倍,依次取左边一个右边一个,都满足,所以除开中间那个数的所有数之和一定是中间那个数的倍数。

                                    如果n为偶数,第一组数取第一个数和最后一个数,第二组取剩下的数,因为第一个数与最后一个数之和等于第二个数与倒数第二个数之和,也等于第三个数和倒数第三个数之和....,所以剩下的数之和一定是第一个数和最后一个数之和的倍数。

              

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 using namespace std;
     5 int main()
     6 {
     7     int n;
     8     while(~scanf("%d",&n))
     9     {
    10         if(n==1||n==2)
    11             printf("No
    ");//当n=1或n=2
    12         else
    13         {
    14             printf("Yes
    ");
    15             if(n%2==0)//当n时偶数
    16             {
    17             printf("2 1 %d
    ",n);//输出第一个数和最后一个数
    18             printf("%d ",n-2);//剩下有n-2个数
    19             for(int i=2;i<=n-1;i++)
    20             {
    21                 if(i==n-1)
    22                     printf("%d
    ",i);
    23                 else
    24                     printf("%d ",i);
    25             }
    26             }
    27             else//当n是奇数
    28             {
    29                 printf("1 %d
    ",n/2+1);
    30                 printf("%d",n-1);//剩下有n-1个数
    31                 for(int i=1;i<=n;i++)
    32                 {
    33                     if(i!=n/2+1)//除开最中间的那个数不输出
    34                         printf(" %d",i);
    35                 }
    36                 printf("
    ");
    37             }
    38         }
    39     }
    40     return 0;
    41 }

    C.1038C Gambling

    C. Gambling
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Two players A and B have a list of nn 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}{1,2,2,3} in a list and you decided to choose 22 for the next turn, only a single instance of 22 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 nn (1n1000001≤n≤100000) — the sizes of the list.

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

    The third line contains nn integers bibi (1bi1061≤bi≤106), describing the list of the player B.

    Output

    Output the difference between A's score and B's score (ABA−B) if both of them are playing optimally.

    Examples
    input
    Copy
    2
    1 4
    5 1
    output
    Copy
    0
    input
    Copy
    3
    100 100 100
    100 100 100
    output
    Copy
    0
    input
    Copy
    2
    2 1
    5 6
    output
    Copy
    -3
    Note

    In the first example, the game could have gone as follows:

    • A removes 55 from B's list.
    • B removes 44 from A's list.
    • A takes his 11.
    • B takes his 11.

    Hence, A's score is 11, B's score is 11 and difference is 00.

    There is also another optimal way of playing:

    • A removes 55 from B's list.
    • B removes 44 from A's list.
    • A removes 11 from B's list.
    • B removes 11 from A's list.

    The difference in the scores is still 00.

    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 00.

    大致题意:有两个人a,b,有两个数组,每个人可以从自己的数组选一个数当作自己的积分,这个数被除去,或者从别人的数组里除去一个数,双方都会进行这个操作,当双方数组里的没有数字的时候,游戏结束,尽可能使得自己的积分最多,最后输出两个人最终积分的差

    分析:先给两个数组从小到大排个序,使用双指针,另i=n,j=n;

             如果a[i]>b[j]时,

                                   如果轮到a来选,那么a的积分ans1+=a[i],i--;

                                   如果轮到b来选,那么b将a[i]从数组a里除去,i--;

            如果a[i]<b[j]时,

                                  如果轮到a来选,那么a将b[j]从数组b里除去,j--;

                                  如果轮到b来选,那么b的积分ans2+=b[j],j--;

            如果a[i]=b[j],

                                  跳过i,j,i--,j--

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 using namespace std;
     5 int main()
     6 {
     7     int n,a[1000005],b[1000005];
     8     while(~scanf("%d",&n))
     9     {
    10         for(int i=1;i<=n;i++)
    11             scanf("%d",&a[i]);
    12         for(int i=1;i<=n;i++)
    13             scanf("%d",&b[i]);
    14         sort(a+1,a+1+n);
    15         sort(b+1,b+1+n);
    16         a[0]=0;
    17         b[0]=0;
    18         long long k=0,ans1=0,ans2=0;
    19         for(int i=n,j=n;i>=0&&j>=0;)
    20         {
    21             if(a[i]>b[j])
    22             {
    23                 if(k==0)//使用k来标记这轮轮到谁,0是a,1是b
    24                 {
    25                     ans1+=a[i];
    26                     k=1;
    27                     i--;
    28                 }
    29                 else
    30                 {
    31                     i--;
    32                     k=0;
    33                 }
    34             }
    35             else if(a[i]<b[j])
    36             {
    37                 if(k==1)
    38                 {
    39                     ans2+=b[j];
    40                     k=0;
    41                     j--;
    42                 }
    43                 else
    44                 {
    45                      k=1;
    46                      j--;
    47                 }
    48             }
    49             else if(a[i]==b[j])
    50             {
    51                 i--;
    52                 j--;
    53             }
    54         }
    55         printf("%lld
    ",ans1-ans2);
    56     }
    57     return 0;
    58 }

     D.codeforces1040a

    A group of nn 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 nn, aa, and bb (1n201≤n≤20, 1a,b1001≤a,b≤100) — the number of dancers, the cost of a white suit, and the cost of a black suit.

    The next line contains nn numbers cici, ii-th of which denotes the color of the suit of the ii-th dancer. Number 00 denotes the white color, 11 — the black color, and 22denotes 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.

          

     题意:有n个舞者,白色衣服的价格是a,黑色衣服的价格是b,第二行有n个数字,0表穿白衣服的,1表示穿黑衣服的, 2表示还没买服装的,要使得该n个数字形成一个回文串,且花的钱最少。

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<cstring>
     4 using namespace std;
     5 int main()
     6 {
     7     int n,a,b,c[100];
     8     while(~scanf("%d %d %d",&n,&a,&b))
     9     {
    10         for(int i=1;i<=n;i++)
    11             scanf("%d",&c[i]);
    12         int t,tt,ans=0,i;
    13         tt=min(a,b);//tt表示黑白服装里便宜的那个
    14         if(n%2==0)
    15             t=n/2;
    16         else
    17             t=n/2+1;//当n是奇数的时候,中间那个数是n/2+1.
    18         for( i=1;i<=t;i++)
    19         {
    20             if(c[i]!=c[n-i+1])//当服装颜色不等于与他对称的人的颜色
    21             {
    22                 if((c[i]==0&&c[n-i+1]==1)||(c[i]==1&&c[n-i+1]==0))
    23                 {
    24                     break;//如果是0,1,1,0不可能形成回文
    25                 }
    26                 else if(c[i]==2)//如果他没有服装
    27                 {
    28                     if(c[n-i+1]==1)//与他对称的人服装是黑色
    29                         ans+=b;
    30                     else if(c[n-i+1]==0)//与他对称的人的服装是白色
    31                         ans+=a;
    32                 }
    33                 else if(c[i]==0&&c[n-i+1]==2)//如果他的服装是白色,与他对称的人没有服装
    34                     ans+=a;
    35                 else if(c[i]==1&&c[n-i+1]==2)//如果他的服装是黑色,与他对称的人没有服装
    36                     ans+=b;
    37             }
    38             else//如果服装颜色相同
    39             {
    40                 if(c[i]==2&&c[n-i+1]==2)//如果他俩都没有服装
    41                     ans+=2*tt;
    42                 if(i==t&&t==n/2+1&&c[i]==2)//如果是奇数的时候,到中间的时候,就多加了一个人的服装,要减去
    43                     ans-=tt;
    44             }
    45         }
    46         if(i==t+1)
    47         printf("%d
    ",ans);
    48         else
    49         printf("-1
    ");
    50     }
    51     return 0;
    52 }

    E - E(模拟)

     CodeForces - 1040B  Shashlik Cooking
    time limit per test
    1 second
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    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 nn skewers parallel to each other, and enumerated them with consecutive integers from 11 to nn in order from left to right. For better cooking, he puts them quite close to each other, so when he turns skewer number ii, it leads to turning kk closest skewers from each side of the skewer ii, that is, skewers number iki−k, ik+1i−k+1, ..., i1i−1, i+1i+1, ..., i+k1i+k−1, i+ki+k (if they exist).

    For example, let n=6n=6 and k=1k=1. When Miroslav turns skewer number 33, then skewers with numbers 22, 33, and 44 will come up turned over. If after that he turns skewer number 11, then skewers number 11, 33, and 44 will be turned over, while skewer number 22 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 nn skewers with the minimal possible number of actions. For example, for the above example n=6n=6 and k=1k=1, two turnings are sufficient: he can turn over skewers number 22 and 55.

    Help Miroslav turn over all nn skewers.

    Input

    The first line contains two integers nn and kk (1n10001≤n≤1000, 0k10000≤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 ll — the minimum number of actions needed by Miroslav to turn over all nn skewers. After than print llintegers from 11 to nn denoting the number of the skewer that is to be turned over at the corresponding step.

    Examples
    input
    Copy
    7 2
    output
    Copy
    2
    1 6
    input
    Copy
    5 1
    output
    Copy
    2
    1 4
    Note

    In the first example the first operation turns over skewers 11, 22 and 33, the second operation turns over skewers 44, 55, 66 and 77.

    In the second example it is also correct to turn over skewers 22 and 55, but turning skewers 22 and 44, or 11 and 55 are incorrect solutions because the skewer 33 is in the initial state after these operations.

    题意:有n个烤串,大概就是给烤串翻个面,当你翻第i个烤串时,它左边的k个烤串和右边的k个烤串,均会被翻面,求翻转的次数最少且所有烤串都翻面,输出选择的烤串序号

    分析:其实就分两种情况,1:选择的每个烤串都在中间  2:考虑最左边和最右边的情况   ,就是找了一组数,模拟了一下过程

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cmath>
     5 using namespace std;
     6 int main()
     7 {
     8     int n,k;
     9     while(~scanf("%d %d",&n,&k))
    10     {
    11         int t,tt,kk=0;
    12         t=n/(2*k+1);
    13         tt=n%(2*k+1);
    14         if(tt==0)
    15         {
    16             printf("%d
    ",t);
    17             printf("%d",k+1);
    18             kk=2*k+1+k+1;
    19             for(int i=2;i<=t;i++)
    20             {
    21                 printf(" %d",kk);
    22                 kk+=k*2+1;
    23             }
    24             printf("
    ");
    25         }
    26         else
    27         {
    28            printf("%d
    ",t+1);
    29            if(tt<=k+1)
    30            {
    31                printf("1");
    32                kk=2*k+2;
    33                for(int i=2;i<t+1;i++)
    34                {
    35                    printf(" %d",kk);
    36                    kk+=2*k+1;
    37                }
    38                if(n>tt)
    39                 printf(" %d
    ",n-tt+1);
    40            }
    41            else
    42            {
    43                printf("%d",tt-k);
    44                kk=tt+k+1;
    45                for(int i=2;i<=t+1;i++)
    46                {
    47                    printf(" %d",kk);
    48                    kk+=2*k+1;
    49                }
    50                printf("
    ");
    51            }
    52         }
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    ajax上传文件
    nginx location指令详解
    总结php删除html标签和标签内的内容的方法
    useBuiltIns: 'usage'
    uni-app如何页面传参数的几种方法总结
    基于 schema 的数据校验
    canvas时点击事件和长按冲突
    vue 下载文件流,后台是get方式 ,并且导出出现excel乱码问题
    uni-app canvas 实现文字居中
    git reflog 回退
  • 原文地址:https://www.cnblogs.com/LLLAIH/p/9737562.html
Copyright © 2020-2023  润新知