• (贪心)Codeforces Round #414 C-Naming Company


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

    Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company.

    To settle this problem, they've decided to play a game. The company name will consist of n letters. Oleg and Igor each have a set of nletters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by nquestion marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters c in his set and replace any of the question marks with c. Then, a copy of the letter c is removed from his set. The game ends when all the question marks has been replaced by some letter.

    For example, suppose Oleg has the set of letters {i, o, i} and Igor has the set of letters {i, m, o}. One possible game is as follows :

    Initially, the company name is ???.

    Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {i, o}.

    Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {i, m}.

    Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {i}.

    In the end, the company name is oio.

    Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally?

    A string s = s1s2...sm is called lexicographically smaller than a string t = t1t2...tm (where s ≠ t) if si < ti where i is the smallest index such that si ≠ ti. (so sj = tj for all j < i)

    Input

    The first line of input contains a string s of length n (1 ≤ n ≤ 3·105). All characters of the string are lowercase English letters. This string denotes the set of letters Oleg has initially.

    The second line of input contains a string t of length n. All characters of the string are lowercase English letters. This string denotes the set of letters Igor has initially.

    Output

    The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.

    Examples
    input
    tinkoff
    zscoder
    output
    fzfsirk
    input
    xxxxxx
    xxxxxx
    output
    xxxxxx
    input
    ioi
    imo
    output
    ioi

    为这题的贪心策略想了很久……

    首先,设字符串长度为len,则先手、后手分别取其前(len+1)/2小的,和前len/2大的数参与组成最后的字符串。

    轮到先手时,考虑其当下最小的数和后手当下最大的“数”,有两种大小关系:

    1、如果先手最小的数比后手最大的数小,那么就把最小的数填到最左(因为先手想让最后的字符串字典序更小,显然应尽可能让最左端一位小,这样做能达成此目的)

    2、若不然,即先手最小的数大于等于后手最大的数,那么就把先手最大的数填到当下的最右(先手为使最后的字符串更小,最优的解决办法一定是让自己要填的数中大的靠右,小的靠左,此时左侧自己的“最小”的,比后手的所有数都要大,所以应避免自己去填最左的,转而考虑去填自己要填的最大的数,显然应该让其尽可能靠右,即采用此策略)

    后手同理,按类似的想法填。

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <queue>
     8 #include <set>
     9 #include <map>
    10 #include <list>
    11 #include <stack>
    12 #define mp make_pair
    13 typedef long long ll;
    14 typedef unsigned long long ull;
    15 const int MAX=3e5+5;
    16 const int INF=1e9+5;
    17 const double M=4e18;
    18 using namespace std;
    19 const int MOD=1e9+7;
    20 typedef pair<int,int> pii;
    21 typedef pair<int,long long> pil;
    22 const double eps=0.000000001;
    23 char a[MAX],b[MAX],an[MAX];
    24 bool vi[MAX];
    25 int len1,len2;
    26 int cnt1[50],cnt2[50];
    27 int anum(int x)//在a中找第x小的
    28 {
    29     int cnt=0,i;
    30     for(i=0;i<26;i++)
    31     {
    32         cnt+=cnt1[i];
    33         if(cnt>=x)
    34             break;
    35     }
    36     return i;
    37 }
    38 int bnum(int x)//在b中找第x大的
    39 {
    40     int cnt=0,i;
    41     for(i=25;i;i--)
    42     {
    43         cnt+=cnt2[i];
    44         if(cnt>=x)
    45             break;
    46     }
    47     return i;
    48 }
    49 int main()
    50 {
    51     scanf("%s%s",a,b);
    52     int len=strlen(a);
    53     for(int i=0;i<len;i++)
    54     {
    55         ++cnt1[a[i]-'a'];
    56         ++cnt2[b[i]-'a'];
    57     }
    58     len1=(len+1)/2;len2=len-len1;
    59     int x,y;
    60     int l1=1,r1=len1,l2=1,r2=len2;
    61     int l=0,r=len-1;
    62     int now1,now2;
    63     for(int i=0;i<len;i++)
    64     {
    65         now1=anum(l1);now2=bnum(l2);
    66         if(now1<now2)
    67         {
    68             if(i&1)
    69             {
    70                 an[l++]='a'+now2;
    71                 ++l2;
    72             }
    73             else
    74             {
    75                 an[l++]='a'+now1;
    76                 ++l1;
    77             }
    78         }
    79         else
    80         {
    81             if(i&1)
    82             {
    83                 now2=bnum(r2--);
    84                 an[r--]='a'+now2;
    85             }
    86             else
    87             {
    88                 now1=anum(r1--);
    89                 an[r--]='a'+now1;
    90             }
    91         }
    92     }
    93     printf("%s
    ",an);
    94 }
  • 相关阅读:
    __str__
    __call__
    私有成员
    @property
    静态方法
    静态字段
    cut qcut
    hive 函数大全
    sklearn 中的Countvectorizer/TfidfVectorizer保留长度小于2的字符方法
    numpy教程:随机数模块numpy.random
  • 原文地址:https://www.cnblogs.com/quintessence/p/6862212.html
Copyright © 2020-2023  润新知