Carrot Distribution I

Chris has N N bags labeled from 1 to N N arranged from left to right. Each bag has some carrots in it. The villagers queue up to collect Chris' bags of carrots. Whenever a villager asks for carrots, Chris must give the bag with the lowest label. However, a villager can receive more than one bags; each villager will ask for carrots until he receives at least the same number as the previous villlager. The first villager will receive exactly one bag of carrots.

This file enlist the number of carrots in 1000 bags labelled from 1 to 1000, in that order. What is the maximum number of villagers can Chris satisfy?

Sample Input

1
2
3
4
1 2 3
1 2 1 2
2 2 2 2
2 1 3

Sample Output

1
2
3
4
3
3
4
2

Explanation

For the first input, the first villager takes the first bag, the second villager takes the second and the third villager takes the third bag.

For the second input, the first villager takes the first bag, and the second villager takes​ the second bag. The third villager takes the third bag, but it's not enough (he only got one carrot, while the previous villager got two), so he also takes the fourth bag.


Try the next version.


The answer is 53.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <bits/stdc++.h>
using namespace std;

int main(){

queue<int> q;

int N;
cin >> N;

for(int i=0; i<N; i++){
    int a;
    cin >> a;
    q.push(a);
}

int man_counter = 0;
int prev_value = 0;
int current_value = 0;
while(!q.empty()){
    auto front = q.front();
    q.pop();
    current_value += front;
    if(current_value>=prev_value){
        man_counter++;
        prev_value = current_value;
        current_value = 0;
    }
}

cout << "Maximum Villager: " << man_counter << endl;

}

Christopher Boo
Jul 16, 2016

Intuitively, you might be doing this :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from urllib2 import urlopen
source = urlopen("https://pastebin.com/raw/fpbCnELC")

curr = 0    # the amount of carrots you give to the current villager
prev = 0    # the amount of carrots you gave to the previous villager
villagers = 0
for i in range(1000):
    curr += int(source.readline())
    if curr >= prev:    # move to the next people, the curr becomes prev
        prev = curr
        curr = 0
        villagers += 1

print villagers

In other words, each time you give a villager the number of bags until the amount of carrots is more than or equal to the one in front, but that's not always the case. Consider the following input : 2 1 3 3 .

Using the algorithm above, you will give the first villager the first bag, the second villager the second, third and forth bag (so the sum 7 > 2). You can only satisfy 2 people.

However, the optimal solution is to give the first villager the first and second bag, the second villager the third bag and the last villager the forth bag. Like this you can satisfy 3 people.

In conclusion, you don't always greedily give the carrots until it exceeds the one in front of you. Instead, we should use Dynamic Programming.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
dp[0] = 0; last[0] = 0;
for (int i = 1; i <= N; i++) {
    for (int j = i-1; j >= 0; j--) {
        if (ps[i] - ps[j] >= last[j]) {
            dp[i] = dp[j] + 1;
            last[i] = ps[i] - ps[j];
            break;
        }
    }
}

dp[i] stores the maximum number of villagers you can satisfy until the i i -th bag. last[i] stores the number of carrots the i i -th villager got.

The transition is simple. First observe that dp[i] <= dp[j] for all i < j i < j (how can you satisfy less people when you have more bags right?). Hence when we are computing dp[k] we move down until the sum is larger than the previous one. Now, we can break because going down further will not give us a higher dp[k] but larger last[k] . Finally, print dp[N] .

The first villager will receive exactly one bag of carrots.

Mehdi K. - 4 years, 11 months ago

Log in to reply

I am terribly sorry for that, someone edited the problem

Christopher Boo - 4 years, 11 months ago

Log in to reply

Thanks. I've updated the answer from 59 to 53. Those who previously answered 53 will be marked correct.

In future, if you spot any errors with a problem, you can “report” it by selecting ""report problem"" in the “line line line” menu in the top right corner. This will notify the problem creator who can fix the issues.

Brilliant Mathematics Staff - 4 years, 11 months ago

bro that's ok u post a lot of cool problems

Mehdi K. - 4 years, 11 months ago

Sorry, I edited the problem incorrectly. I thought the phrasing was weird, so I changed it to be more clear, but apparently I accidentally changed the meaning as well.

Ivan Koswara - 4 years, 11 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...