GitBook

原文地址:http://www.boiltask.com/blog/?p=1989

domino

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 131072/131072 K (Java/Others) Problem Description

Little White plays a game.There are n pieces of dominoes on the table in a row. He can choose a domino which hasn’t fall down for at most k times, let it fall to the left or right. When a domino is toppled, it will knock down the erect domino. On the assumption that all of the tiles are fallen in the end, he can set the height of all dominoes, but he wants to minimize the sum of all dominoes height. The height of every domino is an integer and at least 1.

Input

The first line of input is an integer T ( 1 \leq T \leq 101≤T≤10) There are two lines of each test case. The first line has two integer n and k, respectively domino number and the number of opportunities.( 2\leq k, n \leq 1000002≤k,n≤100000) The second line has n – 1 integers, the distance of adjacent domino d, 1 \leq d \leq 1000001≤d≤100000

Output

For each testcase, output of a line, the smallest sum of all dominoes height

Sample Input

1 4 2 2 3 4 1 2 3 1 4 2 2 3 4 Sample Output

9 1 9 中文题意:

小白在玩一个游戏。桌子上有n张多米诺骨牌排成一列。它有k次机会,每次可以选一个还没有倒的骨牌,向左或者向右推倒。每个骨 牌倒下的时候,若碰到了未倒下的骨牌,可以把它推倒。小白现在可以随意设置骨牌的高度,但是骨牌高度为整数,且至少为1,并且 小白希望在能够推倒所有骨牌的前提下,使所有骨牌高度的和最小。

官方题解:

首先骨牌只要考虑都往右推,其次能带倒骨牌的前提是高度大于等于距离+1。所以如果推一次,那么就是骨牌高度=离下一块骨牌距离+1. 把第一块左边距离设为无穷大,能推nk次,那么就是找nk块左边距离最大的向右推倒即可,所以只需要排序找到前nk-1大的距离。 有个小trick,推的次数可能大于骨牌数量 复杂度 O(nlogn)

由题意可知需要尽量使距离大的浪费掉

所以把累加(距离+1)再尽量减去最大距离

就可以取得最小高度

PS:注意数据范围 int 存不下sum

#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int a,int b) {
    return a>b;
}
__int64 a[100200];
int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        int n,k;
        scanf("%d %d",&n,&k);
        n--;
        __int64 sum=0;
        for(int i=0; i<n; i++) {
            scanf("%I64d",&a[i]);
            sum+=a[i]+1;
        }
        sum++;
        sort(a,a+n,cmp);
        for(int i=0; i<k-1&&i<n; i++)
            sum=sum-a[i];
        printf("%I64d\n",sum);
    }
    return 0;
}

题目地址:【杭电】[5777]domino

原文地址:http://www.boiltask.com/blog/?p=1989