zoukankan      html  css  js  c++  java
  • 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 n letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by n question 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
    Note

    One way to play optimally in the first sample is as follows :

    • Initially, the company name is ???????.
    • Oleg replaces the first question mark with 'f'. The company name becomes f??????.
    • Igor replaces the second question mark with 'z'. The company name becomes fz?????.
    • Oleg replaces the third question mark with 'f'. The company name becomes fzf????.
    • Igor replaces the fourth question mark with 's'. The company name becomes fzfs???.
    • Oleg replaces the fifth question mark with 'i'. The company name becomes fzfsi??.
    • Igor replaces the sixth question mark with 'r'. The company name becomes fzfsir?.
    • Oleg replaces the seventh question mark with 'k'. The company name becomes fzfsirk.

    For the second sample, no matter how they play, the company name will always be xxxxxx.

    分析:

    由题意,第一个人要构造最小的字符串,而第二个人要构造最大的字符串。

    当n为奇数时 集合一中要用到n/2+1个字母 ,n为偶数时用到n/2个字母,那么我们可以只保留字典序较小的字母

    集合二则只保留字典序较大的字母轮到集合一取数的时候

    当集合一中的最小的字母小于集合二中最大的字母时,则将该字母放在字符串的下一个位置,否则,将集合一中最大的字母放在字符串从后往前数的下一个位置。轮到集合二取数的时候

    当集合二中最大的字母大于集合一中最小的字母时,将该字母放在字符串的下一个位置,否则,将集合二中最小的字母放在字符串从后往前数的下一个位置。

    这样就能构造出符合的字符串。

    代码如下:

    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include<cstring>
    using namespace std;
    char a[300010];
    char b[300010];
    char res[300010];
    int cmp(int x,int y)
    {
      return x>y;
    }
    int main()
    {
    	int na,nb,cnt,pa,qa,pb,qb,l,r,L;
    	while(scanf("%s%s",a,b)!=EOF)
    	{
    		cnt=0;
    		na=strlen(a);
    		L=na;
    		nb=strlen(b);
    		sort(a,a+na);
    		sort(b,b+nb,cmp);
    		if(na%2==1)
    		na=na/2+1;
    		else
    		na=na/2;
    		nb=L-na;
    		pa=0;
    		qa=na-1;
    		pb=0;
    		qb=nb-1;
    		l=0;
    		r=L-1;
    		for(int i=0;i<L;i++)
    		{
    		   if(i%2==0)
    		   {
    		   	 if(a[pa]>=b[pb])
    		   	  res[r--]=a[qa--];
    		   	  else
    		   	  res[l++]=a[pa++];
    		   }
    		   else
    		   {
    		   	  if(a[pa]>=b[pb])
    		   	  	res[r--]=b[qb--];
    		   	  else
    		   	  res[l++]=b[pb++];
    		   }
    		}
    		res[L]='';
    		printf("%s",res);
    		printf("
    ");
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    weblogic复现利用系列-2019
    weblogic复现利用系列-2020
    Python3编码解码与数据类型转换
    weblogic复现利用系列-2017
    weblogic从ssrf到redis获取shell
    fastjson反序列化复现
    shiro721复现
    Shiro550复现和挖掘思路
    RMI反序列化
    手写一个admin 组件------STARK
  • 原文地址:https://www.cnblogs.com/a249189046/p/6880420.html
Copyright © 2011-2022 走看看