How do you solve these problems using algorithms

I've been in this USACO training camp, and there are quite a few problems that I don't know how to solve and I want to know how to solve as USACO is coming up soon. Since I'm still a beginner, I don't know much about algorithms and how to use to solve a problem and how you reason out which algorithms to use to solve a problem. That's why I coming to this forum to ask about three of the problems I have that involve algorithms. (And if you can, comments would really help me to understand a code)

Problem 1. Berry Picking
Bessie and her little sister Elsie are picking berries in Farmer John's berry patch. Farmer John's patch has exactly N berry trees (1≤N≤1000); tree i contains exactly Bi berries (1≤Bi≤1000). Bessie has exactly K baskets (1≤K≤1000, K even). Each basket can hold as many berries from a single tree as Bessie wants, but cannot contain berries from two different trees as their flavors will clash with each other. Baskets may remain empty.
Bessie wants to maximize the number of berries she collects. However, Farmer John wants Bessie to share with her little sister, and so Bessie will have to give Elsie the K/2 baskets with the largest number of berries. This means that Elsie may even end up with more berries than Bessie, which is very unfair, but unfortunately, sibling dynamics are not always fair.

Help Bessie figure out the maximum number of berries she can collect.

SCORING:
Test cases 1-4 satisfy K≤10.
Test cases 5-11 satisfy no additional constraints.
INPUT FORMAT (file berries.in):
The first line of input contains space-separated integers N and K.
The second line contains N space-separated integers B1,B2,…,BN.

OUTPUT FORMAT (file berries.out):
A single line with the answer.
SAMPLE INPUT:
5 4
3 6 8 4 2
SAMPLE OUTPUT:
8
If Bessie fills

one basket with 6 berries from tree 2
two baskets, each with 4 berries from tree 3
one basket with 4 berries from tree 4
then she receives two baskets each with 4 berries, giving her 8 berries in total.

Problem 2. Loan Repayment
Farmer John owes Bessie N gallons of milk (1≤N≤1012). He has to give her the milk within K days. However, he doesn't want to give the milk away too quickly. On the other hand, he has to make forward progress on the loan, so he must give Bessie at least M gallons of milk each day (1≤M≤1012).
Here is how Farmer John decides to pay back Bessie. He first picks a positive integer X. He then repeats the following procedure every day:

Assuming that Farmer John has already given Bessie G gallons, compute N−GX rounded down. Call this number Y.
If Y is less than M, set Y to M.
Give Bessie Y gallons of milk.
Determine the largest X such that if Farmer John follows the above procedure, Farmer John gives Bessie at least N gallons of milk after K days (1≤K≤1012).

SCORING:
Test cases 2-4 satisfy K≤105.
Test cases 5-11 satisfy no additional constraints.
INPUT FORMAT (file loan.in):
The only line of input contains three space-separated positive integers N, K, and M satisfying K⋅M<N.
OUTPUT FORMAT (file loan.out):
Output the largest positive integer X such that Farmer John will give Bessie at least N gallons using the above procedure.
SAMPLE INPUT:
10 3 3
SAMPLE OUTPUT:
2
For the first test case, when X=2 Farmer John gives Bessie 5 gallons on the first day and M=3 gallons on each of the next two days.

Problem 3. Wormhole Sort
Farmer John's cows have grown tired of his daily request that they sort themselves before leaving the barn each morning. They have just completed their PhDs in quantum physics, and are ready to speed things up a bit.
This morning, as usual, Farmer John's N cows (1≤N≤105), conveniently numbered 1…N, are scattered throughout the barn at N distinct locations, also numbered 1…N, such that cow i is at location pi. But this morning there are also M wormholes (1≤M≤105), numbered 1…M, where wormhole i bidirectionally connects location ai with location bi, and has a width wi (1≤ai,bi≤N,ai≠bi,1≤wi≤109).

At any point in time, two cows located at opposite ends of a wormhole may choose to simultaneously swap places through the wormhole. The cows must perform such swaps until cow i is at location i for 1≤i≤N.

The cows are not eager to get squished by the wormholes. Help them maximize the width of the least wide wormhole which they must use to sort themselves. It is guaranteed that it is possible for the cows to sort themselves.

SCORING:
Test cases 3-5 satisfy N,M≤1000.
Test cases 6-10 satisfy no additional constraints.
INPUT FORMAT (file wormsort.in):
The first line contains two integers, N and M.
The second line contains the N integers p1,p2,…,pN. It is guaranteed that p is a permutation of 1…N.

For each i between 1 and M, line i+2 contains the integers ai, bi, and wi.

OUTPUT FORMAT (file wormsort.out):

A single integer: the maximum minimal wormhole width which a cow must squish itself into during the sorting process. If the cows do not need any wormholes to sort themselves, output −1.

SAMPLE INPUT:
4 4
3 2 1 4
1 2 9
1 3 7
2 3 10
2 4 3
SAMPLE OUTPUT:
9
Here is one possible way to sort the cows using only wormholes of width at least 9:

Cow 1 and cow 2 swap positions using the third wormhole.
Cow 1 and cow 3 swap positions using the first wormhole.
Cow 2 and cow 3 swap positions using the third wormhole.
SAMPLE INPUT:
4 1
1 2 3 4
4 2 13
SAMPLE OUTPUT:
-1
No wormholes are needed to sort the cows.
Last edited on
The questions are stupid. Just reading them is painful. Forget about this gay little camp. If you want to learn how to program, get a good book and work your way through it. Then you can ask some reasonable questions.
@dutch
It is a very common question format, used almost everywhere.

@DatKidBoris
You simply need to practice. All these questions are very simple programming tasks. Start by trying to solve the examples yourself on paper. Then you can get an idea of how to tell the computer to do it.
@Duthomhas I understand that I need to practice and I know the process of writing these problems out on paper (as I've done many similar problems like these, but they required the use of more beginner methods). The problem here is that I kind of don't really understand exactly how to solve these problems, and exactly how I should write a program to do it as they require the use of C++ algorithms (which I'm not very familiar with). That's why I came here to ask for some help. So if you have any ideas on how to solve it or even have a code to do it, posting it would mean very much to me.
Last edited on
Yeah, do the work for him, duthomhas.
Can anyone help with these problems?
These problems come in a few categories.
1 -- relatively simple problems with intentionally confusing wording
2 -- relatively simple problems with a gimmick (usually a math or bit-logic type trick) that you have to find

not always, once in a while the problem is interesting rather than just a 'find the trick', but usually.

so just like they said above, to code these, you have to unravel what it is asking on paper. That advice is about as good as it gets. If you did this and know the algorithm and solution on paper, then try to code it and if you get stuck, explain what you want to do and what you don't know how to do, and you WILL get a ton of great help here.
If these are the first ones you've come to, then you might be setting yourself too high an initial task. There is no "big book of algorithms" containing the algorithms for every possible problem. This is not about "which algorithm to use"; this is about you understanding the problem and creating the algorithm yourself.

This begins with understanding the problem.

There are N trees. Each try has some berries on it. From 1 to 1000 berries.
You have K baskets.

You can take as many berries as you like from any tree, and put them in one basket.

When done, half of your baskets are going to be removed. Which half? The top 50% of them, by how many berries are in them.

The aim is to have the remaining baskets contain as many berries as possible.


What would the answer be if you had two trees, and two baskets, and each tree had 100 berries on it? 100. Do you understand why that's the answer?

What would the answer be if you had two trees, and two baskets; one tree has 5 berries and one tree has 1000 berries? 5. Do you understand why that's the answer?

If you must, draw the trees on paper, draw the baskets, work it out by thinking about specific cases.

This, by the way, is programming. Understanding the problem, understanding how your chosen programming langage can manipulate data, and thinking about it in such a way that you can express the solution in code. This is programming. All the rest is just memorising syntax and typing.
Last edited on
> What would the answer be if you had two trees, and two baskets;
> one tree has 5 berries and one tree has 1000 berries?
> 5.
no, the answer is 500
this is why it's important to understand the problem.

but the advice is sound
here's in another case: one tree, ten berries, one thousand baskets
Last edited on
Nuts, so it is. I did not notice that while we can put only berries from one tree in each basket, we may put berries from any one tree in more than one basket. Interstingly (perhaps), that is something that can be inferred from the "noise" around the question involving berries and trees and other such, that is not specifically stated.

Off-topic, I wonder if my post is being reported by the same clown who doesn't like me discussing the Cpp Podcasts as they get posted. I hope so! I would say they're welcome to message me directly, but they can't! Ha.
Last edited on
I think it is normal to question if you just start to learn programming, and the forum exists withe the point to share the experience. I am also a beginner and a lot of topics are still difficult for me. But if you need some help, I remember my friend suggested to me at college this https://ca.edubirdie.com/coursework-writing-services site which I used once, but the paper I got was great. It is coursework writing services, I guess you can ask for a paper about programming and you will get it to read and figure out some question you need.
Last edited on
Please do not resurrect month-old topics.
is robot posting spam link with gibberish AI generated nonsense floating around the link.
it looks like it was translated by conan the barbarian from russian.
Last edited on
Topic archived. No new replies allowed.