Meet In The Middle
Authors: Chongtian Ma, Mihnea Brebenel
Problems involving dividing the search space into two.
Meet In The Middle
Focus Problem – try your best to solve this problem before continuing!
Tutorial
Resources | ||||
---|---|---|---|---|
CPH | ||||
Errichto |
Pro Tip
Meet in the Middle technique can take advantage of the smaller constraint and calculate a naive solution in two halves. Therefore the constraints tend to be doubled.
Naive Solution
Loop through all subsets in the array and if the sum is equal to , then increase our answer. Worst case this does about operations, which is way too slow.
Meet in the Middle Solution
We can divide the given array into two separate arrays. Let's say that the array runs from indexes to , and the array runs from indexes to . Both arrays will have at most elements, so we can loop through all subsets of these two arrays in at most operations, which is perfectly fine.
Now that we've got the subset sums of these two separate arrays, we need to recombine them to search for our answer. For every in the , we can simply check how many elements of there are in . This can be done using simple binary search.
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;using ll = long long;int main() {int n, x;cin >> n >> x;vector<int> a(n);for (int i = 0; i < n; i++) { cin >> a[i]; }
Java
Warning: Tight Time Limit
CSES has very tight time constraints for java, so the following code will barely pass in around 1 second. It is not guaranteed to pass on first submits, so you might need to submit multiple times.
import java.io.*;import java.util.*;public class Main {public static void main(String[] args) {Kattio io = new Kattio();int n = io.nextInt(), x = io.nextInt();int[] a = new int[n];for (int i = 0; i < n; i++) { a[i] = io.nextInt(); }
Meet In The Middle On Trees
Focus Problem – try your best to solve this problem before continuing!
We apply meet in the middle technique by splitting the continuous chain of songs into chains of four songs and the center one. We'll compute for each node all the subsets of four different artists such that there is a path going into the this node. Similarly, by reversing the graph, compute the paths going out of the node. In the combining step, loop through all nodes and fix one as the center node. Having precomputed the two groups of subsets (ingoing and outgoing paths), we want to find two sets that are disjoint, i.e no common element. Using Inclusion-Exclusion count for a given set in the first group the number of sets in the second group with one common element. The upperbound for the number of sets is .
C++
#include <functional>#include <iostream>#include <unordered_map>#include <vector>using namespace std;int main() {int n;cin >> n;
Problems
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
CF | Easy | Show TagsBinary Search, Meet in the Middle | |||
Silver | Easy | Show Tags2P, Meet in the Middle | |||
CF | Easy | Show TagsBinary Search, DFS, Meet in the Middle | |||
PrepBytes | Normal | Show TagsDP, Meet in the Middle | |||
YS | Normal | Show TagsBitmasks, DP, Meet in the Middle | |||
CF | Hard | Show TagsDFS, Meet in the Middle, NT | |||
CF | Hard | Show TagsBinary Search, DFS, Meet in the Middle |
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!