codeforces 794 C. Naming Company(贪心)
题目链接:http://codeforces.com/contest/794/problem/C
题意:有两个人每个人都有一个长度为n的字符串,两人轮流拿出一个字符串,放在一个长度为n的字符串的指定位置中,第一个
人他想让最后组成的字符串尽可能小,另一个人想要字符串尽可能的大。他们互相知道各自手中有什么字符串,最后输出组成的
字符串
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <deque>
using namespace std;
const int M = 3e5 + 10;
char s1[M] , s2[M] , s[M];
deque<char>Ol , Ig;
bool cmp(char x , char y) {return x > y;}
int main() {
cin >> s1 >> s2;
int n = strlen(s1);
sort(s1 , s1 + n) , sort(s2 , s2 + n , cmp);
if(n % 2 == 0) {
for(int i = 0 ; i < n / 2 ; i++) {
Ol.push_back(s1[i]);
}
for(int i = 0 ; i < n / 2 ; i++) {
Ig.push_back(s2[i]);
}
}
else {
for(int i = 0 ; i <= n / 2 ; i++) {
Ol.push_back(s1[i]);
}
for(int i = 0 ; i < n / 2 ; i++) {
Ig.push_back(s2[i]);
}
}
int sta = 0 , end = n - 1;
while(!Ol.empty()) {
if(Ig.empty()) {
s[sta++] = Ol.front();
break;
}
char cp1 = Ol.front() , cp2 = Ig.front();
int flag = 0;
if(cp1 < cp2) {
s[sta++] = cp1;
flag = 1;
}
else {
s[end--] = Ol.back();
flag = 2;
}
if(flag == 1) {
Ol.pop_front();
}
else {
Ol.pop_back();
}
if(Ol.empty()) {
s[sta++] = cp2;
break;
}
else {
flag = 0;
char cp3 = Ol.front();
if(cp2 > cp3) {
s[sta++] = cp2;
flag = 1;
}
else {
s[end--] = Ig.back();
flag = 2;
}
if(flag == 1) {
Ig.pop_front();
}
else {
Ig.pop_back();
}
}
}
cout << s << endl;
return 0;
}
codeforces 794 C. Naming Company(贪心)的更多相关文章
- 【codeforces 794C】Naming Company
[题目链接]:http://codeforces.com/contest/794/problem/C [题意] 有n个位置; 两个人; 每个人都有n个字符组成的集合s1,s2(可以有重复元素); 然后 ...
- [刷题]Codeforces 794C - Naming Company
http://codeforces.com/contest/794/problem/C Description Oleg the client and Igor the analyst are goo ...
- CodeForces 794 G.Replace All
CodeForces 794 G.Replace All 解题思路 首先如果字符串 \(A, B\) 没有匹配,那么二元组 \((S, T)\) 合法的一个必要条件是存在正整数对 \((x,y)\), ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- CodeForces - 794C:Naming Company(博弈&简单贪心)
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...
- 【贪心】【multiset】Tinkoff Challenge - Final Round (Codeforces Round #414, rated, Div. 1 + Div. 2) C. Naming Company
考虑两个人,先把各自的集合排个序,丢掉一半,因为比较劣的那一半一定用不到. 然后贪心地放,只有两种决策,要么把一个最优的放在开头,要么把一个最劣的放在结尾. 如果我的最优的比对方所有的都劣(或等于), ...
- 【贪心+博弈】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个字符长度的公司名.两人轮流取名,每次选择 ...
随机推荐
- 一看就懂的K近邻算法(KNN),K-D树,并实现手写数字识别!
1. 什么是KNN 1.1 KNN的通俗解释 何谓K近邻算法,即K-Nearest Neighbor algorithm,简称KNN算法,单从名字来猜想,可以简单粗暴的认为是:K个最近的邻居,当K=1 ...
- codeforces 318 A.Even Odds B.Sereja and Array
A.Even Odds 给你n和k, 把从1到n先排奇数后排偶数排成一个新的序列,输出第k个位置的数. 比如 10 3 拍好后就是 1 3 5 7 9 2 4 6 8 10 第3个数是5. // ...
- ZK安装、ZK配置、ZK集群部署
今天心血来潮,想搞一下zookeeper集群.具体步骤记录下吧~嘻嘻
- 【NOI 2015】程序自动分析 并查集与离散化处理
题目描述 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,-代表程序中出现的变量,给定n个形如xi=xj或xi≠xj的变量 ...
- 1. 源码分析---SOFARPC可扩展的机制SPI
这几天离职在家,正好没事可以疯狂的输出一下,本来想写DUBBO的源码解析的,但是发现写DUBBO源码的太多了,所以找一个写的不那么多的框架,所以就选中SOFARPC这个框架了. SOFARPC是蚂蚁金 ...
- XAMPP/LAMPP到底在哪里启用APACHE2的rewrite
XAMPP/LAMPP是一套我们在个人建站过程中非常便捷常用的集成环境.特别是对于学习PHP开发和建站非常便捷. 最近在使用CentOS7环境下的XAMPP过程中,遇到了一个问题,也就是apache2 ...
- python3 how to creat alphabet
Python: How To Generate a List Of Letters In The Alphabet Method 1# First we need to figure out the ...
- react父组件调用子组件中方法
- 重读《学习JavaScript数据结构与算法-第三版》- 第5章 队列
定场诗 马瘦毛长蹄子肥,儿子偷爹不算贼,瞎大爷娶个瞎大奶奶,老两口过了多半辈,谁也没看见谁! 前言 本章为重读<学习JavaScript数据结构与算法-第三版>的系列文章,主要讲述队列数据 ...
- springboot --AopLog
在项目 pom.xml 文件中添加依赖: <!-- aop 依赖 --> <dependency> <groupId>org.springframework.boo ...