您的位置:首页 > 其它

【贪心】 大天使之剑

2016-11-05 15:42 127 查看
大天使之剑

【问题描述】

⼩A在游戏⾥打怪。有⼀次,他⼀下⼦遇到了n个怪物。

每个怪物有⼀个生命值,第i个怪物的生命值是h_i。而⼩A除了生命值之外,

还有⼀个属性是魔法值m。

⼩A和怪物们依次⾏动。每⼀回合,⼩A先⾏动,然后怪物们同时⾏动。

⼩A每次可以选择以下⾏动之⼀:

•普通攻击:令某个怪物的生命值减少1。

•重击:消耗1魔法值,令某个怪物的生命值减少2。

•群体攻击:消耗1魔法值,令全体怪物的生命值减少1。

而每个存活的怪物(生命值严格大于0)每次会令⼩A的生命值减少1。

假设⼩A有足够的生命值来维持存活,⼩A想知道自⼰⾄少需要被消耗多少生

命值。

【输入文件】

输入文件为zhijian.in。

第⼀⾏为两个数n和m。

第⼆⾏为n个整数,第i个数为h_i。

【输出文件】

输出文件为zhijian.out。

输出⼀个整数,即⼩A⾄少被消耗的生命值。

【输入样例1】

2 1

2 1

【输出样例1】

1

【输入样例2】

3 4

2 4 4

【输出样例2】

6

【数据规模和约定】

对于20%的数据,m≤0;

对于30%的数据,m≤1;

对于50%的数据,m≤18;

存在30%的数据,n≤50,h_i≤50;

第2 9 8 7共5 6页

m=0与m=18各存在1个测试点,n≤1000,h_i≤1000;

对于100%的数据,1≤n≤100,000,0≤m≤100,0

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<stack>
#define M (100005)
#define N ()
#define LL long long
using namespace std;
int n,m,h[M],tot,p;
bool cmp(const int &a,const int &b){
if(a!=0&&b!=0)
return a<b;
}
void Init()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
scanf("%d",&h[i]);
sort(h+1,h+1+n,cmp);
tot=n;
}
void normal(){                    //魔法用完
int k=tot;
if(k==0) return;
for (int i=n-k+1;i<=n;i++){
p+=tot*h[i]-1;
h[i]=0;
tot--;
}
}
int main()
{
Init();
while(tot>=3&&m>=1){  //先用群攻技能把怪物打到只剩两个。
m--;
for (int i=1;i<=n;i++)
if(h[i]>=2) h[i]--;
else if(h[i]==1) {
h[i]--;
tot--;
}
p+=tot;
}
if(m==0) normal();      //没有魔法普通攻击。
else{           //只有两个的话全部重击。除了 1,1
if(tot==1){
while(tot>=1&&m>=1){
h
-=2;
if(h
<=0)
break;
else p++;
}
if(m==0) normal();
}
if(tot==2){
if((h
==h[n-1])&&(h
==1)&&(m!=0)){
cout<<p;
return 0;
}
else{
while(tot>=1&&m>=1){
if(h[n-1]==1){
tot--;p++;h[n-1]=0;break;
}
m--;h[n-1]-=2;
if(h[n-1]<=0) {
tot--;p+=tot;break;
}
else p+=tot;;
}
if(m==0) normal();
while(tot>=1&&m>=1){
if(h
==1){
tot--;p+=tot;h
=0;p++;break;
}
m--;h
-=2;
if(h
<=0) {
tot--;break;
}
else p+=tot;
}
if(m==0) normal();
}
}
}
printf("%d",p);
return
9c7a
0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息