hdu-5777 domino(貪心)

來源:互聯網
上載者:User

標籤:

題目連結:

domino

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

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


Problem DescriptionLittle 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. 

 

InputThe first line of input is an integer T ( 1≤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≤k,n≤100000)
The second line has n - 1 integers, the distance of adjacent domino d, 1≤d≤100000 

 

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

 

Sample Input14 22 3 4 

 

Sample Output9 題意:給出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(‘\n‘);}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<<"\n";        }        return 0;}

  

hdu-5777 domino(貪心)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.