CSES - Coding Company

Authors: Benjamin Qi, Andi Qu

Table of Contents


Edit on Github

First, sort the people. This allows us to express the contribution of each team as . Let denote the skill of the -th person.

The number of ways we can form teams from the first people such that there are "unfinished" teams and the total penalty so far is .

There are 4 cases when we transition from to :

  • We make a team consisting of only person .
    • The number of ways to do this is , since the penalty from this team is 0.
  • We append person to an unfinished team.
    • The number of ways to do this is , since there are teams we can choose from and the penalties don't change.
  • We use person to "finish" an unfinished team.
    • The number of ways to do this is , since person contributes to the cost and there were teams to choose from.
  • We start a new unfinished team with person .
    • The number of ways to do this is , since person contributes to the cost.

Two more things:

  • in our DP array can be negative, so just add 5000 to each .
  • depends only on , so we can drop the first dimension that we store (to save memory).

I believe that this is called "open and close interval trick". See zscoder's blog post for more information and problems.

1#include <bits/stdc++.h>
2using namespace std;
3
4using ll = long long;
5using ld = long double;
6using db = double;
7using str = string; // yay python!
8
9using pi = pair<int,int>;
10using pl = pair<ll,ll>;

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!

Give Us Feedback on CSES - Coding Company!