hdu-5777 domino(贪心)

题目链接:

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 ( 1T10)
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.( 2k,n100000)
The second line has n - 1 integers, the distance of adjacent domino d, 1d100000
 
Output
For each testcase, output of a line, the smallest sum of all dominoes height
 
Sample Input
1
4 2
2 3 4
 
Sample Output
9
 
题意:
给出n个牌的距离,现在要你推k次把这些牌都推到,问这些牌的高度和最小是多少;
 
思路:
 
贪心,距离排序后把前k-1个去除,然后剩下的就是一定要越过的距离了;
 
AC代码:
 
/************************************************ 
┆  ┏┓   ┏┓ ┆    
┆┏┛┻━━━┛┻┓ ┆ 
┆┃       ┃ ┆ 
┆┃   ━   ┃ ┆ 
┆┃ ┳┛ ┗┳ ┃ ┆ 
┆┃       ┃ ┆  
┆┃   ┻   ┃ ┆ 
┆┗━┓    ┏━┛ ┆ 
┆  ┃    ┃  ┆       
┆  ┃    ┗━━━┓ ┆ 
┆  ┃  AC代马   ┣┓┆ 
┆  ┃           ┏┛┆ 
┆  ┗┓┓┏━┳┓┏┛ ┆ 
┆   ┃┫┫ ┃┫┫ ┆ 
┆   ┗┻┛ ┗┻┛ ┆       
************************************************ */  


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=(1<<8);
const double eps=1e-8;

int a[N];
int cmp(int x,int y)
{
    return x>y;
}
int main()
{       
        int t;
        read(t);
        while(t--)
        {
            int n,k;
            read(n);read(k);
            For(i,1,n-1)read(a[i]);
            sort(a+1,a+n,cmp);
            LL ans=0;
            For(i,k,n-1) ans=ans+a[i]+1;
            if(k>n)ans=n;
            else ans=ans+k;
            cout<<ans<<"
";
        }
        return 0;
}

  

原文地址:https://www.cnblogs.com/zhangchengc919/p/5722151.html