HDU 1052田忌赛马 贪心+动态数组

本文介绍了一种解决田忌赛马问题的贪心策略,并通过C++代码实现了该策略。具体方法是将两个参赛方的速度进行排序后,比较双方最快的马匹,如果田忌一方更快则赢得比赛;若齐王一方更快,则用田忌最慢的马匹参与比赛以输掉比赛。

http://acm.hdu.edu.cn/showproblem.php?pid=1052

就是田忌赛马,贪心策略为:

田忌的最快与齐王最快比,田忌快则比掉,赢下;
田忌慢则用田忌最慢的比齐王最快,输掉;
快马相等的话,用田忌的最慢与齐王最慢比,田忌快则赢下,田忌慢则比齐王最快输掉;
最快与最慢都相等,则用田忌最慢比掉齐王最快,输掉;


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 1005 ;
vector<int> h1;
vector<int> h2;
int main(){
    int n ;
    //freopen("in.txt","r",stdin);
    while(~scanf("%d",&n),n){
        int a;
        h1.clear();
        h2.clear();
        for(int i=0;i<n;i++){
            scanf("%d",&a);
            h1.push_back(a);
        }
        for(int i=0;i<n;i++){
            scanf("%d",&a);
            h2.push_back(a);
        }
        sort(h1.begin(),h1.end());
        sort(h2.begin(),h2.end());

        int sum = 0 ;
        for(int i=0;i<n;i++){
//            for(int i=0;i<h1.size();i++)printf("%d %d\n",h1[i],h2[i]);
//            printf("sum = %d\n",sum);
            if(h1.back()>h2.back()){
                //printf("why\n");
                sum+=200;
                h1.erase(h1.end()-1);
                h2.erase(h2.end()-1);
            }else if(h1.back()<h2.back()){
                sum-=200;
                h1.erase(h1.begin());
                h2.erase(h2.end()-1);
            }else{
                if(h1.front()>h2.front()){
                    sum+=200;
                    h1.erase(h1.begin());
                    h2.erase(h2.begin());
                }else if(h1.front()<h2.front()){
                    sum-=200;
                    h1.erase(h1.begin());
                    h2.erase(h2.end()-1);
                }else{
                    if(h1.front()==h2.back()){
                        h1.erase(h1.begin());
                        h2.erase(h2.end()-1);
                    }else{
                        sum-=200;
                        h1.erase(h1.begin());
                        h2.erase(h2.end()-1);
                    }
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值