#include
#include
using namespace std;
const int maxn=20005;
int A[maxn]; //恶龙头的直径
int B[maxn]; //勇士的能力值
int main()
{
int n,m; // 恶龙头数,勇士个数
while(cin>>n>>m)
{
if(n0&&m0)
break;
for(int i=0;i<n;i++)
cin>>A[i];
for(int i=0;i<m;i++)
cin>>B[i];
sort(A,A+n);
sort(B,B+m);
int cur=0; //当前要砍头的编号
int cost=0; //当前总费用
for(int i=0;i<m;i++)
{
if(B[i]>=A[cur]) //注意是 >=
{
cost+=B[i];
if(++cur==n) //cur指向下一个头,如果头已砍完,退出循环
break;
}
}
if(cur<n)
cout<<“Loowater is doomed!\n”;
else
cout<<cost<<endl;
}
return 0;
}
本文描述了一个算法问题,其中勇士必须用他们的能力值来砍掉恶龙头部,每个头都有特定的直径。通过排序和比较,算法确定了砍掉所有头所需的最低成本,或者判断是否不可能完成任务。
501

被折叠的 条评论
为什么被折叠?



