http://codeforces.com/contest/794/problem/C
题意:
有两个人要为公司起名字,每个人手中都有n个字符,现在要取一个n个字符长度的公司名。两人轮流取名,每次选择一个字符,可以任意选择放在1~n还未放置字符的位置上,每个字符限用一次。
现在第一个人希望公司名字典序越小越好,而第二个人希望字典序越大越好。输出最后的公司名。
思路:
首先肯定是要排序的,第一个人肯定去用前面最小的几个,而第二个人肯定去用前面最大的几个。
轮到第一个人时,我们首先将他手中最小的字符和第二个人手中最大的字符相比,如果此时最小字符大于等于了第二个人的最大字符,那么此时就把最大的字符放置最末尾。否则的话就把最小的放到首位。
轮到第二个人时同理分析。
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<string> 6 #include<vector> 7 #include<queue> 8 #include<cmath> 9 using namespace std; 10 typedef long long LL; 11 12 const int maxn=3*1e5+5; 13 14 char s1[maxn],s2[maxn]; 15 char ans[maxn]; 16 17 bool cmp1(char a,char b) 18 { 19 return a<b; 20 } 21 22 bool cmp2(char a,char b) 23 { 24 return a>b; 25 } 26 27 int main() 28 { 29 //freopen("D:\input.txt","r",stdin); 30 while(cin>>s1>>s2) 31 { 32 int len=strlen(s1); 33 sort(s1,s1+len,cmp1); 34 sort(s2,s2+len,cmp2); 35 int p_left=0; 36 int p_right=len/2+(len&1)-1; 37 int q_left=0; 38 int q_right=len/2-1; 39 int l=0,r=len-1; 40 int flag=1; 41 for(int i=0;i<len;i++) 42 { 43 if(flag) 44 { 45 if(s1[p_left]>=s2[q_left]) ans[r--]=s1[p_right--]; 46 else ans[l++]=s1[p_left++]; 47 flag=0; 48 } 49 else 50 { 51 if(s2[q_left]<=s1[p_left]) ans[r--]=s2[q_right--]; 52 else ans[l++]=s2[q_left++]; 53 flag=1; 54 } 55 } 56 for(int i=0;i<len;i++) 57 printf("%c",ans[i]); 58 } 59 return 0; 60 }