[刷题]Codeforces 794C - Naming Company
http://codeforces.com/contest/794/problem/C
Description
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 = s_1s_2...s_m$ is called lexicographically smaller than a string $t = t_1t_2...t_m$ (where $s ≠ t$) if $si < ti$ where $i$ is the smallest index such that $s_i ≠ t_i$. (so $s_j = t_j$ for all $j < i$)
Input
The first line of input contains a string $s$ of length $n$ ($1 ≤ n ≤ 3·10^5$). 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.
Announcement
Problem C. Naming Company
$*****$
The two players know the letters in both sets.
Key
题意:甲乙两人各持有一个长度均为n的字符串,轮着向一个新的长也为n的字符串里放字符,甲先行。甲每一步都试图让字符串按字典序最小化,乙每一步都试图让字符串按字典序最大化。问最后这新字符串是什么。
思路:做题做到一半时出题人推送了一个Announcement(如上面所示):甲乙均知道对面的情况。这是个很重要的提示。
显然的是,先排个序,甲从小到大,乙从大到小。然后两个字符串的后一半肯定是用不上了,砍掉。最后甲字符串长度$(len + 1) / 2$,乙字符串长度$(len) / 2$。
甲乙试图让自己走的每一步最优,那就是贪心无误了。
1.当$甲最小的字符<乙最大的字符$时,两者均靠左放置最小/大字符。
2.当$甲最小的字符>=乙最大的字符$时,两者均靠右放置最大/小字符。
如何找当前最优情况想了很久,一开始想的情况很复杂,如何换个角度思考问题很不容易啊。要注意的是第2种情况的等于号,一开始我放在第1种情况上了(“<=”),这是不对的,找了好久最后才发现竟然错在了一个小小的等于号。“=”号一删,瞬间AC。
Code
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 3e5 + 10;
char s1[maxn], s2[maxn], ans[maxn];
int len;
bool cmp1(const char &a, const char &b) {
return a < b;
}
bool cmp2(const char &a, const char &b) {
return a > b;
}
int main()
{
ios::sync_with_stdio(false);
cin >> s1 >> s2;
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;
return 0;
}
[刷题]Codeforces 794C - Naming Company的更多相关文章
- CodeForces - 794C:Naming Company(博弈&简单贪心)
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...
- [刷题codeforces]650A.637A
650A Watchmen 637A Voting for Photos 点击查看原题 650A又是一个排序去重的问题,一定要注意数据范围用long long ,而且在写计算组合函数的时候注意也要用l ...
- [刷题codeforces]651B/651A
651B Beautiful Paintings 651A Joysticks 点击可查看原题 651B是一个排序题,只不过多了一步去重然后记录个数.每次筛一层,直到全为0.从这个题里学到一个正确姿势 ...
- [刷题]Codeforces 786A - Berzerk
http://codeforces.com/problemset/problem/786/A Description Rick and Morty are playing their own vers ...
- [刷题]Codeforces 746G - New Roads
Description There are n cities in Berland, each of them has a unique id - an integer from 1 to n, th ...
- CF刷题-Codeforces Round #481-G. Petya's Exams
题目链接:https://codeforces.com/contest/978/problem/G 题目大意:n天m门考试,每门考试给定三个条件,分别为:1.可以开始复习的日期.2.考试日期.3.必须 ...
- CF刷题-Codeforces Round #481-F. Mentors
题目链接:https://codeforces.com/contest/978/problem/F 题目大意: n个程序员,k对仇家,每个程序员有一个能力值,当甲程序员的能力值绝对大于乙程序员的能力值 ...
- CF刷题-Codeforces Round #481-D. Almost Arithmetic Progression
题目链接:https://codeforces.com/contest/978/problem/D 题解: 题目的大意就是:这组序列能否组成等差数列?一旦构成等差数列,等差数列的公差必定确定,而且,对 ...
- [刷题]Codeforces 785D - Anton and School - 2
Description As you probably know, Anton goes to school. One of the school subjects that Anton studie ...
随机推荐
- RunTime 给类添加属性
RunTime网上有很多人都不知道Runtime到底是干嘛的?有很多博主都是长篇大论给他们讲这个讲那个,我感觉还不如实例来的实在.很简单的一个例子:我们都知道会有这样的需求,未读消息列表的图片上要有一 ...
- [UWP]涨姿势UWP源码——适配电脑和手机
上一篇我们介绍了绘制主界面的MainPage.xaml,本篇则会结合MainPage.xaml.cs来讲一讲如何适配电脑和手机这些不同尺寸的设备. 同时适配电脑和手机存在几个麻烦的地方: 屏幕尺寸差距 ...
- Jmeter函数组件开发
插件开发方法有两种: 一.在jmeter官网下载jmeter源码,在源码里面新加函数,然后导出jar: 二.不下载源码,直接导入jmeter相应的jar包,即可开发.(推荐) 下面介绍第二种开发方法: ...
- 原生tab切换
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- VS 2017开发ASP.NET Core Web应用过程中发现的一个重大Bug
今天试着用VS 2017去开发一个.net core项目,想着看看.net core的开发和MVC5开发有什么区别,然后从中发现了一个VS2017的Bug. 首先,我们新建项目,ASP.NET Cor ...
- python_原始_web框架
创:10_4_2017 修: 什么是web框架? -- 本质上是socket,用户请求来,业务逻辑处理,返回处理结果 -- 包含socket或者不包含socket的框架 什么是wsgi? -- web ...
- B/S 和 C/S两种架构
一: 什么是B/S(Browser/Server)架构? 应用系统完全放在应用服务器上, 并通过应用服务器同数据库服务器进行通信,系统界面 是通过浏览器来展现的. T是浏览器模式. 优点: 1)客户端 ...
- yii框架数据库操作数据访问对象(DAO)简单总结
Yii提供了强大的数据库编程支持.Yii数据访问对象(DAO)建立在PHP的数据对象(PDO)extension上,使得在一个单一的统一的接口可以访问不同的数据库管理系统(DBMS).使用Yii的DA ...
- 【one day one linux】find 用法详解小记
find命令的功能很强大,查找文件的选项很多,所以这是一个很实用并且很常用的linux命令.但是他有个缺点就是搜索的时候比较慢的.而与之相对的有一个locate命令. find的命令格式 find ...
- java复习(4)---数字处理类
java本身自带一些封装好的类方便数字问题的处理,review下方便以后使用 DecimalFormat类 可格式化数字格式,控制输出格式 Math类 提供三角函数.指数函数.取整函数.最大最小函数. ...