CEOI 2018 - Global Warming
Authors: Albert Ye, Kevin Sheng
We can view the temperatures as an array . We want to decrement one contiguous interval by a value such that the length of the longest increasing subsequence is longest possible. Note that we don't need to consider incrementing as well because every interval's decrease corresponds to another interval's increase.
Subtasks 1-3: Brute Force
One key observation is that it is useless to subtract from any interval as opposed to just for any . Additionally, observe that it is optimal to always subtract from the interval no matter what.
An algorithm would involve brute forcing for all prefixes. Subtract from each interval for all and then find the LIS after each subtraction.
Subtask 4: One pass
Take the LIS of the array. Any algorithm to find the LIS will pass.
General Solution
For each , let be the length of the longest increasing subsequence that ends at and contains and be the length of the longest increasing subsequence starting at after is decremented. We can compute each by storing the length of the longest decreasing subsequence for each prefix of the reverse of the input array.
The final answer is .
Implementation
In the implementation is and is the variable
in the second for
loop.
C++
#include <bits/stdc++.h>using namespace std;int temps[200005];int pref_longest[200005];int main() {int n;int x;cin >> n >> x;
Java
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.*;/*** this code here treats the change as incrementing the suffix instead of* decrementing the prefix as in the editorial but it's basically the same thing*/public class glo {
Python
"""this code here treats the change as incrementing the suffix instead ofdecrementing the prefix as in the editorial but it's basically the same thing"""from bisect import bisect_lefttemp_num, max_cheating = [int(i) for i in input().split()]temps = [int(i) for i in input().split()]assert temp_num == len(temps)
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!