cf#401(Div. 2)B. Game of Credit Card(田忌赛马类贪心)
題干:
After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and decided to continue their competitions in peaceful game of Credit Cards.
Rules of this game are simple: each player bring his favourite?n-digit credit card. Then both players name the digits written on their cards one by one. If two digits are not equal, then the player, whose digit is smaller gets a flick (knock in the forehead usually made with a forefinger) from the other player. For example, if?n?=?3, Sherlock's card is?123?and Moriarty's card has number?321, first Sherlock names?1?and Moriarty names?3?so Sherlock gets a flick. Then they both digit?2?so no one gets a flick. Finally, Sherlock names?3, while Moriarty names?1?and gets a flick.
Of course, Sherlock will play honestly naming digits one by one in the order they are given, while Moriary, as a true villain, plans to cheat. He is going to name his digits in some other order (however, he is not going to change the overall number of occurences of each digit). For example, in case above Moriarty could name?1,?2,?3?and get no flicks at all, or he can name?2,?3?and?1?to give Sherlock two flicks.
Your goal is to find out the minimum possible number of flicks Moriarty will get (no one likes flicks) and the maximum possible number of flicks Sherlock can get from Moriarty. Note, that these two goals are different and the optimal result may be obtained by using different strategies.
InputThe first line of the input contains a single integer?n?(1?≤?n?≤?1000)?— the number of digits in the cards Sherlock and Moriarty are going to use.
The second line contains?n?digits?— Sherlock's credit card number.
The third line contains?n?digits?— Moriarty's credit card number.
OutputFirst print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty.
Examples Input3 123 321 Output0 2 Input2 88 00 Output2 0 NoteFirst sample is elaborated in the problem statement. In the second sample, there is no way Moriarty can avoid getting two flicks.
題目大意:
? ? 夏洛克和她玩起了彈腦殼的游戲。。。他們都拿到n個數字,并且夏洛克只能按照順序念,她耍賴,可以挑著念(改變順序)。然后,對于他們每一次念的數字,數字大的人可以給數字小的人一個腦瓜崩。問狡猾的她少可以被彈幾次,&最多可以彈夏洛克幾次?
解題報告:
? ? 都是從小到大同序排序,然后挨個比較,類似田忌賽馬,所以叫田忌賽馬貪心。(從大到小亦可解?)
ac代碼:
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; int a[1000+5]; int b[1000+5];int main() {int n;int ans1=0,ans2=0;cin>>n;for(int i = 0; i<n; i++) {scanf("%1d",&a[i]);}for(int i = 0; i<n; i++) {scanf("%1d",&b[i]);} // for(int i = 0; i<3; i++) { // printf("%d %d\n",a[i],b[i]); // }sort(a,a+n);sort(b,b+n);int i=0,j=0;//a[i],b[j]對應 //解ans1 while(i<n&&j<n) {if(b[j]>=a[i]) {i++;j++;ans1++;}else if(b[j]<a[i]){j++;}}ans1=n-ans1;i=0;j=0;//解ans2 while( i < n && j < n) {if(b[j]>a[i]) {ans2++;i++;j++;}if(b[j]<=a[i]) {j++;}}printf("%d\n%d",ans1,ans2);return 0 ;} 雙端隊列ac #include<cstdio> #include<queue> #include<deque> #include<algorithm> using namespace std; int main() {char str1[1000+11],str2[1000+11];int a,b,n;while(~scanf("%d",&n)){deque<int> q1,q2;deque<int> s1,s2;scanf("%s %s",str1,str2);for(int i=0;i<n;i++){a=str1[i]-'0';q1.push_back(a);b=str2[i]-'0';q2.push_back(b); }sort(q1.begin() ,q1.end() );sort(q2.begin() ,q2.end() );int k1,k2,v1,v2;int num1=n,num2=0;for(int i=1;i<=n;i++){k1=q1.back() ;k2=q2.back() ;if(k2>k1){num2++;s1.push_back(k1);s2.push_back(k2);q1.pop_back() ;q2.pop_back() ;continue; }v1=q1.front() ;v2=q2.front() ;if(v2>v1){num2++;s1.push_back(v1);s2.push_back(v2);q1.pop_front() ;q2.pop_front() ; }else{s1.push_back(k1);s2.push_back(v2);q1.pop_back() ;q2.pop_front() ; }}sort(s1.begin() ,s1.end() );sort(s2.begin() ,s2.end() );for(int i=1;i<=n;i++){if(s2.back() >=s1.back() ){num1--;s1.pop_back() ;s2.pop_back() ;continue;}v1=s1.front() ;v2=s2.front() ;if(v2>=v1){num1--;s1.pop_front() ;s2.pop_front() ;}else{s1.pop_back() ;s2.pop_front() ;}}printf("%d\n%d\n",num1,num2);}return 0;}總結
以上是生活随笔為你收集整理的cf#401(Div. 2)B. Game of Credit Card(田忌赛马类贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自研GPU看齐GTX1050!景嘉微JM
- 下一篇: 建行信用卡最低还款额及利息怎么计算 算清