zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    iOS最笨的办法实现无限轮播图(网络加载)
    iOS 动画基础总结篇
    ios 深入讲解iOS键盘一:控制键盘隐藏显示
    iOS开发之提交App中断出现:Cannot proceed with delivery: an existing transporter instance is currently uploading this package
    ios -仿微信有多级网页时,显示返回跟关闭按钮
    IOS启动页动画(uiview 淡入淡出效果 )2
    数量加减
    Swift泛型协议的N种用法
    一个swift版简单的用户名和密码输入textField
    Mac电脑上怎么设置环境变量
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5722151.html
Copyright © 2011-2022 走看看