#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;
bool isvalid(int a[], int n, int c, int mid)
{
int total_prata = 0;
for (int i = 0; i < c; i++)
{
int time = 0;
int count = 1;
while (time <= mid)
{
time += a[i] * count;
total_prata += count;
count++;
}
}
return total_prata >= n;
}
int main()
{
int t;
cin >> t;
int n, c;
while (t--)
{
cin >> n;
cin >> c;
int a[c];
for (int i = 0; i < c; i++)
{
cin >> a[i];
}
sort(a, a + c);
int start = 0;
int end = 0;
for (int i = 1; i <= n; i++)
{
end += a[0] * i;
}
int ans = INT_MAX;
while (start < end)
{
int mid = (start + end) / 2;
if (isvalid(a, n, c, mid))
{
ans = min(ans, mid);
end = mid - 1;
}
else
{
start = mid + 1;
}
}
cout << ans << endl;
}
return 0;
}
Not getting corect output
hey are you there???
yes your is valid function is not correct
look at this code
correct code
your code is okay… but why my logic is not working??
also in your code int e=8*(§*(P+1))/2; how can you calculate the end with sum of n terms formula? if the rank of the cook is 8 the series of time in which he will cook the ith prata will be 8multiply1,8multiply2,8multiply3,8multiply4…8multiplyn ; thus it forms Arithmetic progression
i have consider the worst case for finding e
your way is also correct for find end
what is wrong in this???
Your mistakes
- don’t sort the array
- start should be 1 because min time require to make P pratas can be 1
- in while loop condition start<=end (you forgot = condition)
- total_prata should increased by 1 not by count
- you have to increase prata only when time<=mid
Modified Code
now it got accepted
i hope this help
if you have any further doubts
feel free to ask
and don’t forgot to mark doubt as resolved
i sorted the array because to calculate end i will need the fastest cook rank in the array to calculate end
If your doubt is resolved, plz mark it as resolved from your side .
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
