Maximize the sum of profits for a subset of jobs S:
such that if job i is picked, then job p(i) is also picked:
and there exists an order σ for the jobs in S in which no debt is incurred for all prefixes of the order:
project by varun
Maximize the sum of profits for a subset of jobs S:
such that if job i is picked, then job p(i) is also picked:
and there exists an order σ for the jobs in S in which no debt is incurred for all prefixes of the order:
Codeforces Round 1040 (Div. 2) Problem C
Observations
Given n ranges, the problem involves maximally covering the range [1, 2n] with those ranges, while jointly minimizing cycles between the endpoints of those ranges/ undirected edges. [1, 2n] will be maximally covered by taking all ranges, but this does not necessarily avoid cycles. The important observation is that all cycles have a minimum and maximum point, e.g. 1 and 4 in S = {(1, 2), (2, 4), (1, 4)}. There are always two "paths" to get from the minimum to the maximum. In the case of S, {(1, 2), (2, 4)} or {(1, 4)}. Getting from the minimum to the maximum while avoiding a cycle is then just a matter of take one path and only one path.
Take ranges/ undirected edges greedily. Only take an edge (a, b) if b is not currently reachable from a, i.e. the other "path" has not already been taken
#include<iostream>
#include<unordered_map>
using namespace std;
long long binpow(long long a, long long b, long long m) {
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
int main() {
const long long MOD = 1000000007;
int n;
cin >> n;
unordered_map<int, int> freq;
long long count = 1;
for (int i = 0;i < n;++i) {
int x;
cin >> x;
count = count + (count * binpow(freq[x] + 1, MOD - 2, MOD) % MOD) % MOD;
++freq[x];
}
count = count - 1 % MOD;
cout << count << endl;
}
observations
1. The number of subarrays of an array of length n is n(n + 1)/2. If each number in the array appears once, then this is the answer.
2. What if you find a covering of the array into disjoint segments each with distinct numbers, e.g. [1, 2, 3, 1] becomes [1, 2, 3], [1]. Can you apply the formula independently to the segments and sum the results? No, this fails to count segments that cross the split and would underestimate the correct answer. Also, such a covering might not be unique.
3. What if you forego disjoint-ness and expand the segments as much as possible while keeping the numbers distinct: [1, 2, 3, 1] becomes [1, 2, 3], [2, 3, 1]. This covering is unique, but now you overestimate the correct answer because the segments overlap. , that the amount of overshoot is equal to the number of subarrays of [2, 3]. A nice expression for the correct number of subarrays follows from this observation resembling the inclusion-exclusion principle formula.
4. If x1, ..., xk are distinct, but x1, ..., xk, xk+1 are not distinct, then one of the numbers in x1, ..., xk is xk+1. If xj = xk+1 with 1 ≤ j ≤ k, then xj+1, ..., xk+1 is distinct. This means that storing the index of incidence for each number in x1, ..., xk is enough to find out the start of the second segment in the construction from observation three.
You can compute the expression from observation three in a sliding window fashion by exploiting the structure from observation four.