uva 11929The Dragon of Loowater

本来只是想刷一下题,没想到提交了三遍才过。0 0的情景应该放到while里去做,分而治之,高度自治,软件工程的东西代码里也是一样的,这样才能有条理,才能更高效。

sort方法捡一下。

本题是典型的贪心,每次找能满足需要的最少花费的勇士来杀,则最终的结果肯定是最优的!



#include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;

const int MAX_SIZE=20000;

int main()
{
    int n,m;
    int heads[MAX_SIZE],soldiers[MAX_SIZE];
    while(scanf("%d%d",&n,&m)==2)
    {
            if(n==0 && m==0)break;
            for(int i = 0; i < n; i++)cin>>heads[i];
            for(int i = 0; i < m; i++)cin >> soldiers[i];
            sort(heads,heads+n);
            sort(soldiers,soldiers+m);

            int h_pos = 0,s_pos = 0;//heads和soldiers的数组当前位置



            int count = 0;
            while(h_pos < n && s_pos < m )
            {
                if(heads[h_pos] <= soldiers[s_pos])
                {
                    count += soldiers[s_pos];
                    h_pos++;
                    s_pos++;
                }
                else
                {
                    s_pos++;
                }

            }

            if(h_pos == n)cout << count<<"\n";
            else cout<<"Loowater is doomed!\n";
     }



    return 0;




}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值