1. 程式人生 > >【hdu 5527】 [2015ACM/ICPC亞洲區長春站] Too Rich 貪心

【hdu 5527】 [2015ACM/ICPC亞洲區長春站] Too Rich 貪心

Too Rich
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 673 Accepted Submission(s): 173

Problem Description
You are a rich person, and you think your wallet is too heavy and full now. So you want to give me some money by buying a lovely pusheen sticker which costs p dollars from me. To make your wallet lighter, you decide to pay exactly p dollars by as many coins and/or banknotes as possible.

For example, if p=17 and you have two 10coins,four5 coins, and eight 1coins,youwillpayitbytwo5 coins and seven $1 coins. But this task is incredibly hard since you are too rich and the sticker is too expensive and pusheen is too lovely, please write a program to calculate the best solution.

Input
The first line contains an integer T indicating the total number of test cases. Each test case is a line with 11 integers p,c1,c5,c10,c20,c50,c100,c200,c500,c1000,c2000, specifying the price of the pusheen sticker, and the number of coins and banknotes in each denomination. The number ci means how many coins/banknotes in denominations of i dollars in your wallet.

1≤T≤20000
0≤p≤109
0≤ci≤100000

Output
For each test case, please output the maximum number of coins and/or banknotes he can pay for exactly p dollars in a line. If you cannot pay for exactly p dollars, please simply output ‘-1’.

Sample Input

3
17 8 4 2 0 0 0 0 0 0 0
100 99 0 0 0 0 0 0 0 0 0
2015 9 8 7 6 5 4 3 2 1 0

Sample Output

9
-1
36

題意
選出湊成N的最多的錢數;

思路
從大面值到小面值貪心,剩下的只要可以被取到便取最少的大面值的;因為500,200等非倍數的存在,
反例:150,用50,20取,故取時500,50,等可能多取一個即可;

程式碼

#include<iostream>
#include<stdio.h>
using namespace std;
int v[11]={0,1,5,10,20,50,100,200,500,1000,2000};
int s[11];
int ans;int N;
long long T[11];
void dfs(int p,int n,int d)
{

    if(p<0) return ;
    if(d==0)
    {
        if(!p)
        ans=max(ans,n);
        return ;
    }
    long long tmp=max((long long)0,p-T[d-1]);
    int k=tmp/v[d]+(tmp%v[d]?1:0);

    if(k>s[d]) return ;
    dfs(p-k*v[d],k+n,d-1);

    k++;
    if(k<=s[d])
    dfs(p-k*v[d],k+n,d-1);
}

int main()
{
    int c;
    scanf("%d",&c);
    while(c--)
    {
        scanf("%d",&N);
        for(int i=1;i<=10;i++)
        scanf("%d",&s[i]);
        T[0]=0;
        for(int i=1;i<=10;i++)
        {
            T[i]=T[i-1]+(long long)s[i]*(long long)v[i];
        }
        ans=0;
        dfs(N,0,10);

        if(!ans) printf("-1\n");
        else printf("%d\n",ans);
    }

}

相關推薦

hdu 5527 [2015ACM/ICPC亞洲區長春站] Too Rich 貪心

Too Rich Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 673 Accept

套題2015ACM/ICPC亞洲區長春站 HDU5532 5533 5534 5536 5538

水幾個題,熟悉一下鍵盤。。。 HDU5532AlmostSortedArray 題意:ASA定義為,僅去掉一個(OneandOnlyOne)元素後陣列是非降序或者非升序。 題解:很明顯,判斷一個序列是否有序可以通過判斷其LongestNonDecreasing

解題報告2015ACM/ICPC亞洲區瀋陽站

題目連結 B. Bazinga(HDU5510) 思路 設第i個字串儲存在ss[i][]中。本題最直觀最樸素的做法是列舉兩個字串ss[i]和ss[j] (i+1≤j),再用KMP演算法匹配這兩個字串。首先從大到小列舉j,若對某個ss[j]存在某個s

hdu55382015ACM/ICPC亞洲區長春站 House Building 題意&題解&程式碼

題目連結: http://acm.hdu.edu.cn/showproblem.php?pid=5538 題意: 給你積木圖的俯檢視,輸入資料表示(i,j)位置放了一個高度為h[i][j]的積木,

hdu55342015ACM/ICPC亞洲區長春站Partial Tree 題意&題解&程式碼

題目連結: http://acm.hdu.edu.cn/showproblem.php?pid=5534 題意: 構造一棵有n個節點的數,f[i]表示度數(入度+出度)為i的節點的點權,給出所有的

HDU 5532 Almost Sorted Array(最長非遞減子序列 模板題)——2015ACM/ICPC亞洲區長春站

Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Problem Description We are

[hdu 5521][2015ACM/ICPC亞洲區長春站] House Building

House Building Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 624

HDU 5531 Rebuild(三分)——2015ACM/ICPC亞洲區長春站

傳送門 RebuildTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2483    Accep

[hdu 5534]2015ACM/ICPC亞洲區長春站 Partial Tree  完全揹包

Partial Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 674 Ac

HDU 5512 Pagodas(2015ACM/ICPC亞洲區瀋陽站-重現賽(感謝東北大學))

Pagodas Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3502 &nb

@2015ACM/ICPC亞洲區長春站-重現賽(感謝東北師大) @HDU5534 (Dp,完全揹包)

題目描述 In mathematics, and more specifically in graph theory, a tree is an undirected graph in which any two nodes are connected by exactly

2015ACM/ICPC亞洲區上海站solved:4 / 12

A是。抽空要寫掉的bronze-silver題!!! D是左偏樹並查集。以後有空再看吧。 -update:2017年10月25日14:59:07。 A - An Easy Physics Problem HDU - 5572(計算幾何) //先

HDU 5976 Detachment 貪心 (2016ACM/ICPC亞洲區大連站)

Detachment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 570    Accepted

2015ACM/ICPC亞洲區瀋陽站 HDU

Ladies and gentlemen, please sit up straight.Don't tilt your head. I'm serious. For n given strings S1,S2,⋯,Sn, labelled from 1 to n, you should find

HDU 5538 House Building(2015ACM/ICPC亞洲區長春&&幾何體表面積)

Have you ever played the video game Minecraft? This game has been one of the world's most popular game in recent years. The world of Minecraft is made up o

HDU 5305Friends 多校第二場(雙向DFS)

tor typedef type clu name article using ring eof 依據題意的話最多32條邊,直接暴力的話 2 ^ 32肯定超時了。我們能夠分兩次搜索時間復雜度降低為 2 * 2 ^ 16 唯一須要註意的就是對眼下狀態的哈希處理。 我採用

HDU 2669Romantic

%d instead col row left tro pac ati pan Problem Description The Sky is Sprite.The Birds is Fly in the Sky.The Wind is Wonderful.Blew Thro

hdu 6000Wash

long -- 含義 影響 cnblogs 再次 require sample 隊列 【鏈接】 我是鏈接,點我呀:) 【題意】 在這裏輸入題意 【題解】 因為每件衣服都是沒有區別的。 只有洗衣機不同會影響洗衣時間。 那麽我們把每臺洗衣機洗衣的時間一開始都加入

HDU 1856More is better (裸並查集+記錄秩)

gin main 並查集 keep better sele scan ted dia Description Mr Wang wants some boys to help him with a project. Because the project is rather

HDU 1272小希的迷宮(並查集無向圖之回路問題)

problem 沒有 現在 new 一行 回路 問題 ima namespace 描述 上次Gardon的迷宮城堡小希玩了很久(見Problem B),現在她也想設計一個迷宮讓Gardon來走。但是她設計迷宮的思路不一樣,首先她認為所有的通道都應該是雙向連通的,就是說如果