Page **1** of **1**

### 11413 - Fill the Containers

Posted: **Tue Sep 30, 2008 11:42 pm**

by **Leonid**

What is the best time complexity approach for the problem?

I've somehow managed to get AC with a very slow DP O(n^3) solution.

How is it possible to improve the algorithm?

Is there any greedy approach?

### Re: 11413 - Fill the Containers

Posted: **Wed Oct 01, 2008 7:28 pm**

by **pab2**

I used binary search in capacity.

### Re: 11413 - Fill the Containers

Posted: **Wed Oct 01, 2008 10:48 pm**

by **Leonid**

Thanks, that was obvious

### Re: 11413 - Fill the Containers

Posted: **Mon Oct 18, 2010 8:15 pm**

by **Angeh**

Whats The Problem With This Problem ... ???????????????????????????????????????????

(((((((((((

Code: Select all

```
#include<iostream>
using namespace std;
#define FOR(i,n) for(int i=0;i<n;++i)
int in[1024],n,m;
bool f(int S){
int C=0;
int s=0;
FOR(i,n){
if(s+in[i]>S )s=in[i],C++;
else s+=in[i];
}
C++;
if(C<=m)return true;
else return false;
}
int main(){
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)>0 ){
FOR(i,n)scanf("%d",&in[i] );
int Sum=0;
FOR(i,n)Sum+=in[i];
int l=2147483647,r=Sum+1;
FOR(i,n)l=min(l,in[i] );
while(l<r){
int mid=(r+l)/2;
if( f(mid)==true )r=mid;
else l=mid+1;
}
printf("%d\n",l);
}
return 0;
}
```

### Re: 11413 - Fill the Containers

Posted: **Wed Jul 27, 2011 5:04 pm**

by **lucastan**

Try this:

5 3

1 2 3 4 8

3 2

4 78 9

Your solution gives an incorrect answer of 6 whereas it should be 8

I think you're close. Just have to fix this bug

### Re: 11413 - Fill the Containers

Posted: **Sun Nov 13, 2011 12:03 am**

by **Imti**

I would like to tell respective authority of this problem to add some more data set in input file.It missed some input like:

output should be 5 but previous accepted code gave 4.after fixing that I got accepted which means absence of data set like that.For those who getting WA

Try This Input:

Code: Select all

```
2 3
12 3
2 3
3 12
5 4
1 2 3 5 4
3 100
4 78 9
```

Output:

### Re: 11413 - Fill the Containers

Posted: **Sat Jul 21, 2012 6:49 am**

by **kurniawan1101**

can anyone help me with my program?

Code: Select all

```
#include <cstdio>
#include <cstring>
#define MAX 5000005
#define min(a,b) a < b ? a : b
int order[MAX];
int cek (int m, int v, int c)
{
int sum = 0;
for (int i = 0; i < v; i ++)
{
if ((sum + order[i]) <= m)
{
sum += order[i];
}else
{
c--, sum = 0, i --;
if (c == 0 || order[i] > m) return 0;
}
}
return 1;
}
void in_o ()
{
int ves, con;
while (scanf ("%d %d", &ves, &con) == 2)
{
int sum = 0, ans = 1 << 25;
for (int i = 0; i < ves; i++)
{
scanf ("%d", &order[i]);
sum += order[i];
}
int l = 1, r = sum;
while (l <= r)
{
int m = (l + r) / 2, x = cek (m, ves, con);
if (x)
{
ans = min (ans, m);
r = m - 1;
}else
{
l = m + 1;
}
}
printf ("%d\n", ans);
}
}
int main ()
{
in_o ();
return 0;
}
```

### Re: 11413 - Fill the Containers

Posted: **Tue Jul 24, 2012 3:30 am**

by **brianfry713**

On line 20 you might be testing order[-1].

### Re: 11413 - Fill the Containers

Posted: **Sun Dec 23, 2012 8:59 am**

by **sith**

Hi could anybody clarify some issues with this problem

Problem says

Whenever milk from a vessel is poured into a container, the milk in the vessel must be completely poured into that container only. That is milk from same vessel can not be poured into different containers.

So, this is "one container many vessels" relationship.

why does containers limit greater than vessels limit?

(1 <= m <= 1000000)

Ho does it possible?

Also, I don't understand this constraint

The ith container must be filled with milk only from those vessels that appear earlier to those that fill jth container, for all i < j

Does it mean that for first container we always must have to vessels?

### Re: 11413 - Fill the Containers

Posted: **Sun Dec 23, 2012 10:34 pm**

by **brianfry713**

You don't have to put milk in each container. If there are more containers then vessels you will end up with empty containers.

I don't really understand your second question. Did you mean "two" instead of "to"? The first container must be filled from the first vessel. The second vessel can either be poured into the first or the second container. Each of the remaining vessels can either be poured into the current container or the next.

### Re: 11413 - Fill the Containers

Posted: **Mon Dec 24, 2012 2:44 am**

by **sith**

I meant two

Somebody posted input example

and answer is 82.

Why? I think if we have a lot of containers we can set one container per each vessel, so the answer has to be 78?

Why 82?

### Re: 11413 - Fill the Containers

Posted: **Tue Dec 25, 2012 9:22 pm**

by **brianfry713**

Yes the correct output for that input is 78. You can generate I/O at

http://www.uvatoolkit.com/problemssolve.php

### Re: 11413 - Fill the Containers

Posted: **Sun Jan 05, 2014 4:02 pm**

by **Alim14**

Input:

Code: Select all

```
5 9
1 9 2 3 5
10 9
1 2 3 4 9 12 34 45 34 2
5 2
12 12 45 5 5
```

Output:

### Re: 11413 - Fill the Containers

Posted: **Thu Dec 11, 2014 6:40 pm**

by **Kingreey**

10 11

1 1 1 1 1 1 1 1 1 1

10 10

1 1 1 1 1 1 1 1 1 1

5 3

1 2 3 4 5

3 2

4 78 9

1 2

1

1 1

1

5 1

1 2 3 4 5

5 2

1 2 3 4 5

10 3

9 5 1 4 2 8 7 3 6 5

21 6

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1