Ved started playing a new mobile game called Fighting Clans. An army of N enemies is approaching his base. The i^{th} enemy has H_i health points. An enemy gets killed if his health points become 0.
Ved defends his base using a weapon named Inferno. He can set the Inferno to one of the following two modes:
Single-Target Mode: In one second, the Inferno can target exactly one living enemy and cause damage of at most X health points.
Multi-Target Mode: In one second, the Inferno can target all living enemies and cause damage of 1 health point to each of them.
Find the minimum time required to kill all the enemies.
Note: Ved is not allowed to change the mode of the weapon once it is set initially.
EXPLANATION:
Let us calculate answer for both modes:
Single Target Mode: Here we would loop through each enemy and calculate the time required to kill him. Let us assume a function f(i) that tells the time taken to kill the i_{th} enemy then,
f(i) = \lceil \frac{H_i}{X} \rceil
single\_mode\_answer = \sum_{i=1}^{i=n} f(i)
Multi Target Mode: Here the answer would be the maximum H_i among all the enemies since each second it is reducing the health of all enemies by 1. Thus,
multi\_mode\_answer = max(H_i), 1 \leq i \leq n
Once we calculate these two, our final answer would be:
You have done mistake in loop for calculating single_shot value, if the h[i] is completely divisible
by x then you don’t need to add 1, it’s simply h[i]/x, else you can add one if it is not divisible.
Here you can refer my code to understand better :
void solve(){
int n,x;
cin>>n>>x;
int a[n],multi_mode=0;
for(int i=0;i<n;i++){
cin>>a[i];
multi_mode=max(multi_mode,a[i]);
}
int one_mode=0;
for(int i=0;i<n;i++){
if(a[i]<=x)
one_mode++;
else{
if(a[i]%x==0)
one_mode+=a[i]/x;
else
one_mode+=(a[i]/x)+1;
}
}
cout<<min(multi_mode,one_mode)<<"\n";
}
Can someone tell me why my code getting wrong and where?
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n,x,ans=0,sum=0;
cin>>n>>x;
int h[n];
for(int i=0;i<n;i++)
{
cin>>h[i];
ans=max(ans,h[i]);
sum+=ceil(h[i]/x);
}
cout<<min(sum,ans)<<endl;
}
return 0;
}