24h購物| | PChome| 登入
2013-05-24 08:35:56| 人氣1,193| 回應0 | 上一篇 | 下一篇

[UVA][背包dp] 12621 - On a Diet

推薦 0 收藏 0 轉貼0 訂閱站台

 B - On a Diet 

Background

Alarm bells are ringing: Summer is rapidly approaching and our worst enemy is our mirror.

The Problem

We've got a kitchen recipe book including the calories associated to each course. We want to select some courses, without repeating, that match the amount of calories given or exceed them minimaly.

The Input

The first line of the input contains an integer, t, indicating the number of test cases. For each test case, three lines appear, the first one contains a number n, 100 ≤ n ≤ 2500, representing the minimun amount of calories we want to eat (n is multiple of 10). The second line contains a number p, 5 ≤ p ≤ 100, representing the number of courses we have. The third line of each test case contains p numbers, representing the amount of calories of the p courses (these p numbers are larger or equal to 50, smaller or equal to 2500, and multiple of 10).

The Output

For each test case the output should contain a single line, that consists of the amount of calories of our selection (i.e., the selection that matches the amount of calories given or exceeds them minimaly) or the string NO SOLUTION if no solution is possible.

Sample Input

4
2480
5
1230 1050 820 890 1150
2140
4
450 150 120 50
1200
5
320 570 610 1560 890
1810
6
2340 780 940 310 660 790

Sample Output

2760
NO SOLUTION
1210
1880

OMP'13
Facultad de Informatica
Universidad de Murcia (SPAIN)


題目要求至少吃 n 卡路里,但超過的量越少越好。
問這個值為多少?會給 p 個食物,這 p 個食物有個別的卡路里。

做個背包 dp 把所有可能運算出來,從 n 開始搜索有沒有可能。


#include <stdio.h>
#include <string.h>
int main() {
    int testcase;
    int n, p, x;
    int i, j;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d %d", &n, &p);
        n = n/10;
        int dp[1000] = {};
        dp[0] = 1;
        for(i = 0; i < p; i++) {
            scanf("%d", &x), x /= 10;
            for(j = 999-x; j >= 0; j--) {
                if(dp[j])
                    dp[j+x] = 1;
            }
        }
        int ret = n;
        while(ret < 1000 && dp[ret] == 0)
            ret++;
        if(ret == 1000)
            puts("NO SOLUTION");
        else
            printf("%d\n", ret*10);
    }
    return 0;
}

台長: Morris
人氣(1,193) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][模擬] 947 - Master Mind Helper
此分類上一篇:[UVA][dp] 10721 - Bar Codes

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文