codeforces Naming Company 很思维题最优想法

来源:互联网 发布:如何找淘宝达人推广 编辑:程序博客网 时间:2024/05/20 15:36

C. Naming Company
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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的字符串里放字符,甲先行。甲每一步都试图让字符串按字典序最小化,乙每一步都试图让字符串按字典序最大化。问最后这新字符串是什么。

第一个串从小到大排序,第二个串从大到小排序。
如果第一个串里面还有比第二个串小的。那么就从左边开始一个一个来很正常即可。但是如果第一个串里面剩下的都比第二个串大。那么就从后面开始,从后往前。这个只要想想就好,如果你是第一个人,轮到你了,但是对方的都比你的小,你当然想放最后,如果你是第二个人,如果对方的都比你大,你当然想把自己的放最后。所以这个模式就一样了。

贴上大佬代码

#include <bits/stdc++.h>using namespace std;const int maxn=3e5+10;char s1[maxn],s2[maxn],ans[maxn];int len;int cmp1(const char &a,const char &b){    return a<b;}int cmp2(const char &a,const char &b){    return a>b;}int main(){    cin>>s1>>s2;    int len=strlen(s1);    sort(s1,s1+len,cmp1);    sort(s2,s2+len,cmp2);    int i=0;    char *pl[2]={s1,s2};    for(;(i<len)&&(*pl[0]<*pl[1]);i++)    {        ans[i]=*(pl[i&1]++);    }    int j=len-1;    char *pr[2]={s1+(len+1)/2-1,s2+(len)/2-1};    for(;i<len;i++,j--)    {        ans[j]=*(pr[i&1]--);    }    ans[len]='\0';    cout<<ans<<endl;}
1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 网易邮箱发错了怎么办 苹果手机邮件无法连接服务器怎么办 好记星平板电脑开不了机怎么办 邮箱里的文件过期了怎么办 小米手机邮件发不了怎么办 有人加我qq邮箱怎么办 文件太大发邮件太慢怎么办 爱又米发信息怎么办 手机上电子邮件己停止运行怎么办 苹果电子邮件密码忘了怎么办 玩游戏时电脑烫怎么办 qq邮箱独立密码忘记了怎么办 qq邮箱中转站容量不足怎么办 qq邮箱忘记密码了怎么办 qq邮箱超大附件过期怎么办 忘记qq邮箱独立密码怎么办 网易邮箱账号忘了怎么办 微信登录密码忘了怎么办 微信太久没登录登录不上怎么办 邮箱独立密码忘记了怎么办 苹果设置id没有邮箱怎么办 苹果手机设置id没有邮箱怎么办 邮箱的附件过期了怎么办 邮箱里的附件过期了怎么办 邮箱中附件过期了怎么办 扣扣邮箱附件过期怎么办 公司网页版邮箱进不去了怎么办 农行客户端密码忘记了怎么办 中国银行客户端密码忘记了怎么办 建行客户端登录密码忘记了怎么办 中国移动客户端密码忘记了怎么办 再歪一点授权码绑定怎么办 网易邮箱号忘了怎么办 忘记支付宝账号和密码怎么办 支付宝账号密码忘了怎么办 发邮箱文件超2g怎么办 报考计算机二级邮箱不存在怎么办 苹果邮箱登录要imap密码怎么办 注销微信支付后怎么办 手机卡注销后支付宝怎么办 12306手机邮箱都换了怎么办