题解:P2813 母舰
解题思路
首先要用2个数组存下来,然后从小到大排序,因为要用尽量小的来打敌方的防御系统,留大的来打母舰
创建一个变量t,用来存储敌方现有的,最小的防御系统的防御力的索引
写个循环遍历我方的攻击系统,先判断敌方的防御系统是否为0,如果是,直接跳过:
if (a[t]==0){
t++;
}
再判断一下当前的攻击系统能打破敌方现有的,最小的防御系统,如果能就清0做标记:
if (b[i]>a[t] && a[t]!=0){
b[i]=0;
t++;
}
循环外判断如果打不完防御系统就一点伤害都没有,如果打完了,就把剩下的攻击系统的攻击力加起来输出
代码
#include<bits/stdc++.h>
using namespace std;
long long n,m,a[1001000],b[1001000],ans,t=1;
int main(){
cin>>m>>n;
for (int i=1;i<=m;i++){
cin>>a[i];
}
for (int i=1;i<=n;i++){
cin>>b[i];
}
sort(a+1,a+m+1);
sort(b+1,b+n+1);//从小到大排序
for (int i=1;i<=n;i++){
if (a[t]==0){//判断敌方的防御系统是否为0
t++;
}
if (b[i]>a[t] && a[t]!=0){//判断一下当前的攻击系统能打破敌方现有的,最小的防御系统
b[i]=0;//作标记
t++;
}
}
if (t<=m){//判断打不打得完防御系统
cout<<0;
return 0;
}
for (int i=1;i<=n;i++){//把剩下的攻击系统的攻击力加起来输出
ans+=b[i];
}
cout<<ans;
return 0;
}