问题
Problem Description
Input
Output
Sample Input
3
5 10 1
3 4 5 2 7
3 7 0
2 3 4
10 20 3
6 12 9 4 10 1 3 4 2 1
Sample Output
4
2
8
Hint
思路
如果烟雾弹的数量大于等于敌人的数量,直接输出敌人的数量。否则先定义一个优先队列q(自动降序排列),从左往右依次把第i个敌人的伤害输入进去,n减去当前受到的伤害,当n<=0时,对q.top()使用烟雾弹,n+q.top()。如果烟雾弹为零并且n<=0。输出i-1。如果一直到最后n依然大于0,输出敌人的数量。
代码
#include<iostream>
#include<queue>
#define int long long
using namespace std;
int a[100007];
signed main()
{
int t;
cin>>t;
while(t--)
{
priority_queue<int> q;
int flag=0;
//敌人的数量n,故障机器人的初始生命值x,烟雾弹的数量k
int n,x,k;
cin>>n>>x>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
if(k>=n)
{
cout<<n<<endl;
continue;
}
for(int i=1;i<=n;i++)
{
x-=a[i];
q.push(a[i]);
if(x<=0&&k>0)
{
while(x<=0&&k>0&&!q.empty())
{
x+=q.top();
q.pop();
k--;
}
}
if(x<=0)
{
flag=1;
cout<<i-1<<endl;
break;
}
}
if(flag==0)
cout<<n<<endl;
}
}