题解:B4201 [常州市程序设计小能手 2020] 勇士斗恶龙
第一眼看这个题感觉很简单,但是调了好久才过……
题目大意:
恶龙有一个攻击力
解题思路:
首先用结构体储存每个勇士的攻击力和生命值:
struct node{
long long a,h;//a是攻击力,h是生命值
}w[100005];
因为现在我们已经知道了恶龙的攻击力和每个勇士的生命值,所以可以算出每个勇士能够坚持的回合数:
round=(w[i].h+att-1)/att;//round是回合数
这样一来每个勇士能够造成的最大伤害也可以算出来:
e[i]=w[i].a*round;//e数组储存每个勇士的最大贡献(即能够造成的最大伤害)
根据题意,越靠后上场的勇士得到的攻击加成越高,所以为了最大化伤害,我们肯定要让贡献大的勇士排在后面上场。
于是我们按照这个思路排序:
sort(e+1,e+n+1,greater<int>());
接下来计算前缀和数组:
for(int i=1;i<=n;i++){
tot+=e[i];
sum[i]=sum[i-1]+tot;
}
最后遍历前缀和数组,一旦发现当前的伤害总值大于恶龙的生命值,就输出当前编号。
for(int i=1;i<=n;i++){
if(sum[i]>=hp){
cout<<i;
return 0;
}
}
最后不要忘了考虑无法打败恶龙的情况:
cout<<"Fail";
进食后人:
十年 OI 一场空,不开 long long 见祖宗!
#include<bits/stdc++.h>
using namespace std;
long long n,att,hp;
long long e[100005],sum[100005];
struct node{
long long a,h;
}w[100005];
int main(){
cin>>n;
cin>>att>>hp;
for(int i=1;i<=n;i++){
cin>>w[i].a>>w[i].h;
long long round=(w[i].h+att-1)/att;
e[i]=w[i].a*round;
}
sort(e+1,e+n+1,greater<int>());
long long tot=0;
for(int i=1;i<=n;i++){
tot+=e[i];
sum[i]=sum[i-1]+tot;
}
for(int i=1;i<=n;i++){
if(sum[i]>=hp){
cout<<i;
return 0;
}
}
cout<<"Fail";
return 0;
}