K. Split
time limit per test: 3 seconds
memory limit per test: 512 megabytes
You are given a positive integer n and a non-increasing sequence ai of length n , satisfying ∀i∈[1,n−1],.
Then, you are given a positive integer m, which represents the total number of operations.
There are two types of operations. The first type gives an integer x satisfying 1<x<n and changes to .
The second type is query operation and gives an integer k . Assuming the sequence is divided into k segments, and the length of each segment must be at least 1. The value of a segment is defined as the difference between the maximum element and the minimum element of the segment. You should print the mininum sum of the values of the k segments for all possible ways to divide the sequence into k segments.
Specifically, for each operation, you will be given the type of the operation first. If it is 0, it means the first type of operation, and if it is 1, it means the second type of operation. For the first type of operation, you will then be given a positive integer x. For the second type of operation, you will then be given a positive integer k.
Input
The first line contains a positive integer nn (3≤n≤), representing the length of the sequence.
The second line contains nn positive integers a1,a2,...,an (1≤ai≤).
The third line contains a positive integer mm (1≤m≤), representing the total number of operations.
Next mm lines, each line containing either "0 x" (1<x<n) or "1 k" (1≤k≤n), denoting an operation.
Output
Print q lines where the i-th line contains one integer — the answer for the i-th query operation.
Example
Input
5 30 20 18 13 2 3 1 2 0 3 1 3
Output
17 7
【思路分析】
差分。显然本题答案为整序列最大值-整序列最小值-(k-1)个最大差分和,对于操作1,显然不会影响差分数组最大k个数的取值,仅会使差分数组中相邻元素改变顺序。那么我们sort差分数组,用后缀和记录差分和,那么对于每个操作2,我们只需要O(1)的时间复杂度。
由于需要sort,整体的时间复杂度为O(nlogn)。
#include <iostream>
#include <vector>
#include <unordered_map>
#include <map>
#include <cmath>
#include <algorithm>
#include <climits>
#include <stack>
#include <cstring>
#include <iomanip>
#include <set>
#include <queue>#define i64 long longusing namespace std;void solve() {i64 n;cin >> n;i64 a[n + 1];for (int i = 1; i <= n; ++i) cin >> a[i];i64 m, op, tmp;i64 diff[n - 1];for (int i = 1; i < n; ++i) diff[i - 1] = a[i] - a[i + 1];sort(diff, diff + n - 1);cin >> m;i64 suf[n], suff = 0;suf[n - 1] = 0;for (int i = n - 2; i >= 0; --i) {suf[i] = diff[i] + suff;suff = suf[i];}while (m--) {cin >> op >> tmp;if (op == 0) a[tmp] = a[tmp - 1] + a[tmp + 1] - a[tmp];else {i64 total = suf[n - tmp];cout << a[1] - a[n] - total << endl;}}
}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);int t = 1;
// cin >> t;while (t--) {solve();}return 0;
}