正文
题目描述:你正想出去走走,但是突然来了一只怪物,幸运的,你有N把剑,每把剑有两个属性,分别是ai和bi。ai表示握着这把剑的攻击力,你可以握着这把剑攻击怪物任意次数,bi表示把这把剑扔向怪物的攻击力,这把剑扔向怪物后,怪物会受bi点伤害,然后你就失去了这把剑这个怪物最多能承受H点攻击,即在受到H点攻击或以上,就会消失请问让怪物消失的最少攻击次数。
那么我们就会想到贪心,肯定是优先用攻击力大的剑,那么我们是应该用ai大的呢?还是bi大的呢?
很明显是合起来最大的。首先我们要知道,先扔再A,和先A(普攻)再扔是一个道理,因为就算你在程序里默认先扔再A,现实生活中还是可以反过来的对不啦~。
然后我们知道,找出最大那把ai的剑,然后bk大于它的都先扔出去,然后再不断用这把剑A。明显,没有什么其他的方案比这个更优。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n;
long long h;
struct node{//1为a,2为b
int d,x;
bool operator<(const node y)const{
return d>y.d;
}
}s[200010];
int main(){
freopen("b.in","r",stdin);
freopen("b.out","w",stdout);
scanf("%d %lld",&n,&h);
for(register int i=1;i<=n;i++) {
int x,y;
scanf("%d %d",&x,&y);
s[2*i-1].d=x;s[2*i-1].x=1;
s[2*i].d=y;s[2*i].x=2;
}
n*=2;
sort(s+1,s+1+n);
long long tot=0;
int ans=0;
for(register int i=1;i<=n;i++){
if(s[i].x==2){
tot+=s[i].d;
ans++;
}
else break;
if(tot>=h) break;
}
if(tot>=h) {
printf("%d\n",ans);
return 0;
}
h-=tot;
if(h%s[ans+1].d==0) ans+=h/s[ans+1].d;
else ans+=h/s[ans+1].d+1;
printf("%d\n",ans);
}