Test instructions: Xiao Bai is playing a game. There are n dominoes in a row on the table. It has a K-chance, each time you can choose a domino that has not fallen, to the left or to the right to tear down. Every time a domino falls, if it touches a non-falling
Domino, can push it down. Small white can now arbitrarily set the height of the dominoes, but the domino height is an integer, and at least 1, and the small white hope to be able to tear down all the dominoes under the premise, so that all the dominoes height and minimum.
Analysis: First dominoes as long as the consideration of all to the right, and the second can bring down the domino premise is the height is greater than or equal to the distance +1. So if you push it once, then the domino height = the distance from the next dominoes is +1. Set the left distance of the first block to infinity to
Push NK Times, then is to find the largest left of the NK block to the right to tear down can, so only need to sort to find the front nk-1 large distance. With a small trick, the number of pushes may be greater than the number of dominoes complex O (NLOGN).
The code is as follows:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include < algorithm> #include <set> #include <cstring> #include <queue>using namespace Std;typedef long Long Ll;int Kase, N, K, a[100007];int main () { int T; scanf ("%d", &t); while (t--) { scanf ("%d%d", &n, &k); LL ans = n; for (int i = 0; i < n-1; ++i) { scanf ("%d", &a[i]); } Sort (A, a+n-1); for (int i = 0; i < n-k; ++i) ans + = a[i]; printf ("%i64d\n", ans);} }
HDU 5777 Domino (sort, water problem)