POI 2017 - Containers
Author: Andi Qu
Time Complexity: .
Let's consider two naive solutions and try to combine them! (In general, this is a good idea for square-root-decomposition problems.)
Naive solution 1 - slow update; fast query
Keep an array that stores the number of containers on each position. When we process a crane, we can simply increment the positions that it puts containers on.
Querying the final answer only takes time, but processing all of the cranes can potentially take time in total.
This solution would work well if all were large, as processing each crane takes time.
Naive solution 2 - fast update; slow query
Let denote the prefix sum of the number of cranes with and . When we process a crane, we change exactly two values in the DP array. The answer for position is simply
Processing all of the cranes now only takes only time, but querying the final answer can potentially take time.
This solution would work well if all were small, as we querying the solution takes time.
Model solution - fast(ish) update; fast(ish) query
When , then apply solution 1. Otherwise, apply solution 2.
After processing all the cranes, we can simply add the results from the two solutions.
#include <bits/stdc++.h>#define FOR(i, x, y) for (int i = x; i < y; i++)using namespace std;const int sqrt_n = 300;int containers[100001];int dp[100001 + sqrt_n][sqrt_n];int main() {iostream::sync_with_stdio(false);
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!