HDU 5777 Domino (sort, water problem)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.