[刷题]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=s1s2...sm is called lexicographically smaller than a string t=t1t2...tm (where st ) if si<ti where i is the smallest index such that siti. (so sj=tj for all j<i )

Input

The first line of input contains a string s of length n ( 1n3105 ). 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值