CSES - Coding Company
Authors: Benjamin Qi, Andi Qu
Appears In
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.
#include <bits/stdc++.h>using namespace std;using ll = long long;using ld = long double;using db = double;using str = string; // yay python!using pi = pair<int,int>;using 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!