【贪心】 大天使之剑
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
【问题描述】
⼩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; }
相关文章推荐
- 【ACMICPC Chengdu Online】1009 贪心
- HDU-2037 今年暑假不AC 贪心
- 二分与贪心-Aggressive cows(算法基础 第9周)
- CSUOJ 1922 Irony Ring 线段树 or 贪心
- HDOJ 3979 贪心 水题
- SGU 171 Sarov zones【贪心】
- 贪心算法之——阶乘之和(nyoj91)
- hdu 5055(贪心)
- 贪心算法基础之活动时间安排(一)安排 51nod 贪心教程
- POJ 1456 Supermarket 【贪心】
- 贪心算法-Huffman算法
- Sunscreen (poj 3614 贪心+优先队列)
- poj 1700 过河贪心
- 删数问题(贪心)
- Codeforces 888G Xor-MST - 分治 - 贪心 - Trie
- SGU 257 Debt 贪心 + 模拟
- 浙工大2015年校赛 Problem G: You you rhythm master(贪心题)
- 算法导论(第三版)-复习16贪心算法
- [CF797B] Odd sum(贪心)
- 杭电1789贪心java实现