Naming Company CodeForces - 794C (博弈,构造)
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.
题意:
给你两个等长度的字符串s1,s2.,s1属于先手拥有,s2属于后手拥有
现在要新凑一个长度相等字符串,由先手后手交叉拿出一个字符串选择放在指定的位置。
先手想让新字符串字典序更小,后手想让新字符串的字典序更大。二者都绝顶聪明,请输出最终答案。
思路:
很容易产生一个错误的思路:
即s1从小到大排序,s2从大到小排序,二人从前到后依次选出。
而当s1中最小的字符比s2最大的字符大的时候,这样处理就会出现错误。
并且我们容易知道先手一定拿出s1的排序后的前半部分,s2也是。
那么我们把一定拿出的部分分别代替掉s1和s2串。
当s1中最小的字符比s2中最大的字符小的时候,在新字符串中从前往后填字符,
否则即s1中最小的字符比s2中最大的字符大的时候,
我们拿出s1中最大的字符,填在新字符串能填的最后的位置,
同时后手为了让字典序更大,应该把s2中最小的字符填在新字符串能填的最后的位置。
依次得出的新字符串就是答案,
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
string s1,s2;
int len;
char ans[maxn];
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
cin>>s1>>s2;
len=s1.length();
int l1=0;
int l2=0;
int r1=(len+1)/2-1;
int r2=len-r1-2;
sort(ALL(s1));
sort(ALL(s2),greater<char>());
int l=0;
int r=len-1;
// repd(i,0,r1)
// {
// cout<<s1[i];
// }
// cout<<endl;
//
// repd(i,0,r2)
// {
// cout<<s2[i];
// }
// cout<<endl;
repd(i,1,len)
{
if(s1[l1]>=s2[l2])
{
if(i&1)
{
ans[r--]=s1[r1--];
}else
{
ans[r--]=s2[r2--];
}
}else
{
if(i&1)
{
ans[l++]=s1[l1++];
}else
{
ans[l++]=s2[l2++];
}
}
}
ans[len]='\0';
cout<<ans<<endl;
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
Naming Company CodeForces - 794C (博弈,构造)的更多相关文章
- Naming Company CodeForces - 794C
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...
- [刷题]Codeforces 794C - Naming Company
http://codeforces.com/contest/794/problem/C Description Oleg the client and Igor the analyst are goo ...
- codeforces 1041 e 构造
Codeforces 1041 E 构造题. 给出一种操作,对于一棵树,去掉它的一条边.那么这颗树被分成两个部分,两个部分的分别的最大值就是这次操作的答案. 现在给出一棵树所有操作的结果,问能不能构造 ...
- CodeForces - 794C:Naming Company(博弈&简单贪心)
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...
- 【codeforces 794C】Naming Company
[题目链接]:http://codeforces.com/contest/794/problem/C [题意] 有n个位置; 两个人; 每个人都有n个字符组成的集合s1,s2(可以有重复元素); 然后 ...
- 【贪心+博弈】C. Naming Company
http://codeforces.com/contest/794/problem/C 题意:A,B两人各有长度为n的字符串,轮流向空字符串C中放字母,A尽可能让字符串字典序小,B尽可能让字符串字典序 ...
- Codeforces Round #414 C. Naming Company
http://codeforces.com/contest/794/problem/C 题意: 有两个人要为公司起名字,每个人手中都有n个字符,现在要取一个n个字符长度的公司名.两人轮流取名,每次选择 ...
- codeforces 794 C. Naming Company(贪心)
题目链接:http://codeforces.com/contest/794/problem/C 题意:有两个人每个人都有一个长度为n的字符串,两人轮流拿出一个字符串,放在一个长度为n的字符串的指定位 ...
- 【贪心】【multiset】Tinkoff Challenge - Final Round (Codeforces Round #414, rated, Div. 1 + Div. 2) C. Naming Company
考虑两个人,先把各自的集合排个序,丢掉一半,因为比较劣的那一半一定用不到. 然后贪心地放,只有两种决策,要么把一个最优的放在开头,要么把一个最劣的放在结尾. 如果我的最优的比对方所有的都劣(或等于), ...
随机推荐
- 【计算机视觉】背景建模之PBAS
本文是根据M. Hofmann等人在2012年的IEEE Workshop on Change Detection上发表的"Background Segmentation with Feed ...
- J-流浪西邮之寻找火石碎片 【经典背包变形】
题目来源:2019 ACM ICPC Xi'an University of Posts & Telecommunications School Contest 链接:https://www. ...
- PYTHON 100days学习笔记007-3:字符串和常用数据结构
目录 Day007:字符串和常用数据结构 1.使用字符串 2.使用列表 3.使用元组 4.使用字典 4.练习 4.1:在屏幕上显示跑马灯文字 4.2 设计一个函数产生指定长度的验证码,验证码由大小写字 ...
- 并发-synchronized
线程并发-synchronized和Lock简单认知 前几天刚加深了线程的了解,期间在验证各种方法及多线程时遇到一些疑问,在高并发的情况下,怎么做才能保证程序还能按照我们预期的正常运行下去,这就是我们 ...
- c++面向对象程序设计第四章课后习题
这是书上的习题,我使用的是VS2010运行编译的 原习题: 4.有两个矩阵a和b,均为两行三列.求两个矩阵之和.重载运算符“+”,使之能用于矩阵相加.如c=a+b. #include<iostr ...
- 2019牛客暑期多校训练营(第五场)- G subsequence 1
题目链接:https://ac.nowcoder.com/acm/contest/885/G 题意:给定字符串s,t,求s中满足字典序大于t的子序列的个数. 思路:组合数学+dp.当子序列长度大于m时 ...
- [Comet OJ - Contest #6 C][48C 2279]一道树题_树
一道树题 题目大意: 给定一棵树,边的编号为读入顺序.现在规定,区间$[L, R]$的贡献$S(L,R)$为把编号在该区间里的边都连上后,当前形成的森林中点数大于等于$2$的联通块个数. 求$\sum ...
- [POI2011]ROT-Tree Rotations 题解
题面 这道题咋看都是无法从dp入手,那么就从数据结构入手!: 首先你要会权值线段树和线段树合并. 然后你要知道: 对于任意一个节点,交换左右子树对当前节点和前面的所有节点没有影响. 因为这是前序遍历: ...
- docker-Overlay原生网络
节点1/键值存储:192.168.50.130 :192.168.50.131 1.下载Consul二进制包并启动 wget https://releases.hashicorp.com/consul ...
- B2B、B2C、C2C、O2O分别是什么意思?
1.B2B 是指进行电子商务交易的供需双方都是商家(或企业.公司),她(他)们使用了互联网的技术或各种商务网络平台,完成商务交易的过程.电子商务是现代 B2B marketing的一种具体主要的表现形 ...