码迷,mamicode.com
首页 > 其他好文 > 详细

小白进阶之路-田忌赛马问题-HDU1052

时间:2020-04-27 22:31:00      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:++   scan   include   style   思考   algo   string   mat   main   

 

 

反思:田忌赛马的故事都熟悉,可我栽坑了,我只想到三局两胜的情况下怎么取比赛,或者说我只是当田忌赛马为故事,并没有深入思考问题本质,这题给我想的头皮发麻。

 

田忌赛马思想:① 我的最快的比另一个人的最快的快 ,我就赢下这一局,ans1++;R1--;R2--;

                         ② 我的最快的比另一个人的最快的,我就用最慢的输掉对方最快的,ans2++;L1++;R2--;

                         ③ 我的最慢的比另一个人的最慢的,我就用最慢的赢掉对方最慢的,ans1++;L1++;L2++;

                         ④ 以上都不满足,用我的最慢的输掉对方最快的,L1++,R2--; 判断我的最慢的和对方最快的关系,如果不等,ans2++;

 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<map> 
#include<list>
#include<string>
#include<cstring>
#include<set>
#include<vector>
#define ll long long
#define memset(a,n) memset(a,n,sizeof(a))
#define mp make_pair 
#define pb push_back
using namespace std;
const int maxn = 2e6 + 100;




int main()
{
    
    int n;
    while(~scanf("%d",&n) & n){
        int a[2000],b[2000];
        for(int i =  0;i < n;i++) scanf("%d",&a[i]);
        for(int i = 0 ;i < n;i++) scanf("%d",&b[i]);
        sort(a,a+n);sort(b,b+n);
        int L1,L2,R1,R2,ans1,ans2;
        L1 = L2 = 0;R1 = R2 = n - 1;ans1 = ans2 = 0;
        for(int i = 0 ;i < n;i++){
            if(a[R1] > b[R2]){ // 最快较大,则A赢
                ans1++;R1--;R2--;
            }else if(a[R1] < b[R2]){ // 最快较小,则B赢
                ans2++;L1++;R2--;
            }else if(a[L1] > b[L2]){ // 最快相等,最慢较快,A赢
                ans1++;L1++;L2++;
            }else{ // 最快相等,最慢较慢,用A的最慢跟B的最快对比,
                if(a[L1] != b[R2]) ans2++;
                L1++;R2--;
            }
        }
        printf("%d\n",200 * (ans1 - ans2));
    }
    return 0;
}

 

小白进阶之路-田忌赛马问题-HDU1052

标签:++   scan   include   style   思考   algo   string   mat   main   

原文地址:https://www.cnblogs.com/Wise-XiaoWei4/p/12790355.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!