UVA - 11292 Dragon of Loowater ( 排序 贪心 )
2017-11-12 08:56
363 查看
//思路:先将表示龙头的数组 head 和表示雇佣骑士所需金币的数组 coin 都进行排序,之后,从最小的龙头开始,每次贪心选取可以砍下当前龙头,且所需金币最少的骑士,每次砍下龙头后,更新当前以砍下的龙头总数 cut,最后判断 cut 是否与龙头总数 head 相等
#include <iostream>
#include <algorithm>
#define rep(i, k, n) for (int i = k; i < (n); i++)
using namespace std;
const int N = 2e4 + 5;
int head
, coin
, n, m;
int main()
{
while (cin >> n >> m && n && m)
{
rep(i, 0, n) cin >> head[i];
rep(i, 0, m) cin >> coin[i];
sort(head, head + n);
sort(coin, coin + m);
int cut = 0; //当前需要砍下的龙头,在 head 数组中的下标
int sum = 0; //所需金币总数
rep(i, 0, m) //遍历所有的骑士, 进行砍龙头
{
if ( coin[i] >= head[cut] )
{
sum += coin[i];
cut++;
if (cut == n) break;
}
}
if (cut == n) cout << sum << endl;
else cout << "Loowater is doomed!" << endl;
}
return 0;
}
#include <iostream>
#include <algorithm>
#define rep(i, k, n) for (int i = k; i < (n); i++)
using namespace std;
const int N = 2e4 + 5;
int head
, coin
, n, m;
int main()
{
while (cin >> n >> m && n && m)
{
rep(i, 0, n) cin >> head[i];
rep(i, 0, m) cin >> coin[i];
sort(head, head + n);
sort(coin, coin + m);
int cut = 0; //当前需要砍下的龙头,在 head 数组中的下标
int sum = 0; //所需金币总数
rep(i, 0, m) //遍历所有的骑士, 进行砍龙头
{
if ( coin[i] >= head[cut] )
{
sum += coin[i];
cut++;
if (cut == n) break;
}
}
if (cut == n) cout << sum << endl;
else cout << "Loowater is doomed!" << endl;
}
return 0;
}
相关文章推荐
- uva11292 The Dragon of Loowater (排序+贪心)
- UVa 11292 - Dragon of Loowater(排序贪心)
- [ACM_水题] UVA 11292 Dragon of Loowater [勇士斗恶龙 双数组排序 贪心]
- Uva11292--------------(The Dragon of Loowater)勇者斗恶龙 (排序后贪心)
- UVa 11292 Dragon of Loowater 贪心+排序
- UVaThe Dragon of Loowater-11292(排序+贪心)
- UVa - 11292 - Dragon of Loowater ( 排序 + 贪心 )
- UVa 11292 Dragon of Loowater(排序+贪心)
- JAVA-勇者斗恶龙-The Dragon of Loowater,Uva 11292-排序后贪心
- UVA 11292 Dragon of Loowater(贪心+排序)
- The Dragon of Loowater UVa 11292(排序后贪心)
- UVa 11292 - Dragon of Loowater(排序贪心)
- uva11292 The Dragon of Loowater (排序+贪心)
- UVa-11292 Dragon of Loowater (贪心)
- UVA 11292 Dragon of Loowater(简单贪心)
- uva11292 - The Dragon of Loowater (贪心)
- UVa 11292 - Dragon of Loowater(贪心)
- Uva 11292 Dragon of Loowater (贪心)
- UVa 11292 / POJ 3646 / HDU 1902 Dragon of Loowater (贪心)
- UVA 11292 Dragon of Loowater(贪心)