C. Naming Company
来源:互联网 发布:mysql查看root密码 编辑:程序博客网 时间:2024/06/06 03:20
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 nletters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by nquestion 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)
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.
The output should contain a string of n lowercase English letters, denoting the company name if Oleg and Igor plays optimally.
这个题是个贪心。
首先就是我们把两个串排序,第一个串从小到大,第二个串从大到小。
然后选的个数是确定的,第一步策略就是:
1.第一个串选择(n+1)/2个,这里是整除,n为串的长度。第二个串就自然选择n/2个了。
然后是对于第二步策略。
2.当s[l1]<t[l2]或t[l2]>s[l1]时,这里的l指的是每个串的左端点的指针。这时候那么肯定是直接放在ans串的左边的。没有比这更优的了。
3.然后剩下一个比较重要的策略就是当s[l1]>=t[l2]或t[l2]<=s[l1]的情况的时候,我们肯定不能响当然的往左边放了,这时候我们应该尽量的让第二个往左放,所以此时我们应该占取右边的位置,在放置的时候当然是选择最大的放。
因为对于s来说就是小的往左放,大的往右放,而对于t来说就是正好相反的。
思路基本就是这样,然后看一下代码就懂了。
#include <bits/stdc++.h>using namespace std;const int MAXN=3e5+7;char s[MAXN],t[MAXN],ans[MAXN];int n,l1,l2,l,r1,r2,r;bool cmp(char a,char b){ return a>b;}int main(){ scanf("%s%s",s+1,t+1); n=strlen(s+1); l1=l2=l=1; r1=(n+1)/2; r2=n/2; r=n; sort(s+1,s+1+n); sort(t+1,t+1+n,cmp); while(l<=r) { if(s[l1]<t[l2])ans[l++]=s[l1++]; else ans[r--]=s[r1--]; if(l>r)break; if(t[l2]>s[l1])ans[l++]=t[l2++]; else ans[r--]=t[r2--]; } for(int i=1;i<=n;++i)printf("%c",ans[i]); return 0;}
- C. Naming Company
- C. Naming Company 几何
- C. Naming Company 贪心
- C. Naming Company
- C. Naming Company 贪心
- Codeforces 794C. Naming Company
- codeforces 794C C. Naming Company
- Codeforces 794C Naming Company 贪心
- Codeforces 794 C. Naming Company (贪心)
- 【Codeforces Round #414 C Naming Company】
- codeforces C. Naming Company 贪心+博弈
- codeforces 794C Naming Company(贪心)
- Codeforces Round #414 C. Naming Company 贪心
- codeforces Naming Company 很思维题最优想法
- naming
- naming
- company
- company
- cypher语言使用
- 蓝桥杯 第六届决赛 奇怪的数列 JAVA
- Python 的lambda 以及 filter , map , reduce
- 我与python约个会:12程序编程基础6:选择和循环
- 单应矩阵 基本矩阵 本质矩阵的区别与联系
- C. Naming Company
- [标签:标题]
- 截取地址后的参数 乱码转化为汉字
- 观察者模式与事件驱动模式实例比较
- QT支持XP
- js轮播广告
- 其他信息: 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集
- Gradle中的buildScript代码块
- 自己写了一个js切割方法