CF - An impassioned circulation of affection
Authors: Neo Wang, Qi Wang, Jesse Choe, Kevin Sheng
Solution (Two Pointers)
Time Complexity:
For each query we can keep a sliding window where no more than letters in the window are not .
Implementation
C++
#include <bits/stdc++.h>using namespace std;int main() {int garland_len, query_num;string garland;cin >> garland_len >> garland >> query_num;for (int i = 0; i < query_num; i++) {int max_repaint;
Alternative Solution (Two Pointers with Prefix Sums)
Time Complexity:
For each query we can use prefix sums to check the number of occurrences of a given letter and use two pointers to find the maximum Koyomity ending at a given pointer from .
C++
#include <bits/stdc++.h>using namespace std;const int MAX_N = 1500;int dp[26][MAX_N + 1];int main() {int garland_len;string garland;
Alternative Solution (Dynamic Programming)
Warning!
Dynamic programming is a Gold topic.
Time Complexity:
We are performing up to queries, so it may be optimal to have some way to quickly precompute our answer in constant time.
We can use dynamic programming to cache such answers.
Define to be the best possible score of the first characters, with amount painted of character . Either two things can happen when we add an additional character.
We append the character and paint an additional square.
We append the character, but since it is the same as our previous character we get to "save" a paint.
Since this only gives us the best possible score that ends with length , we can just take the current best and translate it using .
Now, we can query the max painting score for any substring containing the first character. However, this is not required; we only have to query the optimal paintings with size (the full string) with .
Implementation
C++
#include <bits/stdc++.h>using namespace std;const int MAX_N = 1500;int dp[MAX_N + 1][MAX_N + 1][26];int garland[MAX_N + 1];int main() {int garland_len;
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!