PrevNext
Very Frequent
 0/10

Ad Hoc Problems

Authors: Michael Cao, Aarav Sharma

Contributor: Ryan Chou

Problems that do not fall into standard categories with well-studied solutions.

Edit This Page

According to USACO Training section 1.2:

Ad hoc problems are those whose algorithms do not fall into standard categories with well-studied solutions. Each ad hoc problem is different; no specific or general techniques exist to solve them.

In this module, we'll go over some general tips that may be useful in approaching problems that appear to be ad hoc.

  • Draw lots of small cases to gain a better understanding of the problem. If you're having trouble debugging, draw more cases. If you don't know how to start with a problem, draw more cases. Whenever you don't know how to further approach a problem, you're probably missing an important observation, so draw more cases and make observations about properties of the problem.
  • Whenever you find an observation that seems useful, write it down! Writing down ideas lets you easily come back to them later, and makes sure you don't forget about ideas that could potentially be the solution.
  • Don't get stuck on any specific idea, unless you see an entire solution.
  • Try to approach the problem from a lot of different perspectives. Try to mess around with formulas or draw a visual depiction of the problem. One of the most helpful things you can do when solving ad hoc problems is to keep trying ideas until you make progress. This is something you get better at as you do more problems.

In the end, the best way to get better at ad hoc problems (or any type of problem) is to do a lot of them.

Example - Milking Order

Focus Problem – try your best to solve this problem before continuing!

Don't be afraid to give up on some approach if you aren't making progress!

Hint 1

Solution

Official Analysis (C++)

What if we tried placing cow 11 at every possible position?

Then, we'll have some hierarchy we have to fit in and some free cows which can go anywhere. Let's just handle the hierarchy, since we can fit in the free cows at the end.

As we sweep through the hierarchy, we'll also store a pointer that indicates our current position. Greedily, we should try to place these cows as early as possible to make sure that we have room to fit in all of them. As we go through the list, we have to make sure that this pointer never outruns some previous cow in our hierarchy.

This check takes O(N+M+K)\mathcal{O}(N + M + K), which brings our total time complexity to O(N(N+M+K))\mathcal{O}(N(N + M + K)).

C++

#include <bits/stdc++.h>
using namespace std;
int n, m, k;
/**
* @return whether it's possible to construct a
* valid ordering with given fixed elements
*/
bool check(vector<int> order, vector<int> &hierarchy) {

Java

import java.io.*;
import java.util.*;
public class MilkOrder {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("milkorder.in"));
PrintWriter pw = new PrintWriter("milkorder.out");
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());

Python

import sys
sys.stdin = open("milkorder.in", "r")
sys.stdout = open("milkorder.out", "w")
n, m, k = map(int, input().split())
hierarchy = [i - 1 for i in list(map(int, input().split()))]
order = [-1] * n

Casework

A casework problem is a type of ad hoc problem that needs to be broken down into different cases that each need to be accounted for.

These usually require drawing out a lot of cases and making observations about each. We can try to spot similarities and differences between cases and their solutions as well.

Example - Sleepy Cow Herding

Focus Problem – try your best to solve this problem before continuing!

View Internal Solution

Explanation

There are 3 cases for the minimum amount of moves:

  1. The 3 positions are already consecutive.
  2. Two elements are already in-position consecutively (including gaps), but the other is not.
  3. Any other case that did not satisfy the two above.

For the first case, the answer would be 00 because the elements are already consecutive.

For the second case, the answer would be 11 because the only swap required is the one that would insert the isolated element into the gap between the two other elements.

The third case would output 22 because for any other test case, the optimal solution would be to take the minimum element to max2\text{max}-2, and then the new minimum to fit right in between the gap.

The maximum will always be finite because these operations group the cows closer together over time, as mentioned in the problem statement. The best approach to maximize the amount of moves is to place each element as close to a gap as possible (while not remaining an endpoint). Therefore, the maximum is the largest gap between two adjacent elements minus 1. Try it yourself to see that this is indeed the case.

Implementation

Time Complexity: O(1)\mathcal{O}(1)

C++

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
freopen("herding.in", "r", stdin);
freopen("herding.out", "w", stdout);

Java

import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("herding.in"));
PrintWriter pw = new PrintWriter(new File("herding.out"));
int[] cows = new int[3];
cows[0] = sc.nextInt();
cows[1] = sc.nextInt();
cows[2] = sc.nextInt();

Python

with open("herding.in", "r") as file_in:
a, b, c = map(int, file_in.readline().split())
"""
The minimum number of moves can only be 0, 1, or 2.
0 is if they're already consecutive,
1 is if there's a difference of 2 between any 2 numbers,
and 2 is for all other cases.
"""
if c == a + 2:

Problems

Of course, ad hoc problems can be quite easy, but the ones presented below are generally on the harder side.

StatusSourceProblem NameDifficultyTags
BronzeHard
Show TagsComplete Search
BronzeHard
BronzeVery Hard
BronzeVery Hard
SilverVery Hard
Show TagsGreedy

Quiz

What is most useful when solving ad hoc problems?

Question 1 of 1

Module Progress:

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

PrevNext