I was able to fully understand the question but couldn't able to understand how the solution (given below) is able to manage to solve the question? How applying this solutions leads to pass all test cases ?
The solution provided is the best solution
Question
Agon Packers and Movers specialize in moving large number luggages from one place to another.
The luggages are packed by the Packers, and after they're done with the job, the Movers take over.
First, the Packers put the packages they have packed in a single line, one behind the other, and then the Movers come one by one to pick the packages. A Mover can come only once, can only pick the first few packages in the line, and then take them with him. He might also choose not to pick any. Another Mover comes, and repeats the process, till all the packages are taken. The Movers need to carry of all the packages.
The Movers are a friendly lot, and care about each other - so their goal is to minimize the maximum load that any Mover gets.
You are given the number of movers M in the first line. The next line contains the number of packages P. The next line contains the weight of the P package (in Kilograms) in order - first weight representing of first package in the line, separated by space. You need to print the maximum load any mover carries in the best possible case, in accordance with their goal.
Constraints:
1 <= M <= 15 1 <= P <= 100 Weight of each Package (W): 1 <= W <= 10000 Examples:
1) INPUT 5 5 1 1 1 2 1
OUTPUT 2
Explanation: There are 5 movers, and doesnt matter what the split is, one mover needs to carry at least 2 kilograms
2) INPUT 2 4 5 10 21 20
OUTPUT 36
Explanation: 36 is the best case among all the splits possible (happens when the first guy picks 5, 10 and 21)
Best Solution
#include <iostream>
using namespace std;
int ans;
/*int f(int a[],int p,int index)
{
if(index>=n)
return 0;
if(p>=m)
return 0;
int ans=0;
for(int i=index+1;i<n;i++)
{
ans=min(ans,f(a,p+1,i))
}
return ans;
}*/
int m,n,a[10005];
int check(int x)
{
int sum=a[0];
if(sum>x)
return 0;
int count=1;
for(int i=1;i<n;i++)
{
if(sum+a[i]<=x)
sum+=a[i];
else
{
sum=a[i];
count++;
}
if(sum>x)
return 0;
}
if(count<=m)
return 1;
return 0;
}
void f(int l,int r)
{
while(l<=r)
{
int mid=l+(r-l)/2;
if(check(mid))
{
ans=mid;
r=mid-1;
}
else
l=mid+1;
}
}
int main()
{
int maxi=-1,mini=999999;
cin>>m>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
maxi=max(a[i],maxi);
mini=min(a[i],mini);
}
f(maxi,1000001);
cout<<ans;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire