话说这道题是一道01背包了,纠结的就是那个糟烂的输入,因为输入,我纠结了得有一个小时,但是最后还是成功了…… 还好样例吧所有的陷阱都给了,否则不知道要WA多少次,可恨的是我CE了一次,TMD复制粘贴的时候搞错了,这要是正式比赛,那个罚时啊…… view code #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std; int c[500], i, v, num[500], j, n, tot, ju[3]; double cc, vv, sum, x, dp[4000000]; char c1, c2, c3; bool f; int max(int a, int b) { if (a > b) return a; else return b; } int main() { while (cin >> vv >> n && n != 0) { v = (int)(vv * 100); tot = 0; memset(c, 0, sizeof(c)); memset(dp, 0, sizeof(dp)); for (i = 1; i <= n; i++) { cin >> num[i]; f = 1; sum = 0; memset(ju, 0, sizeof(ju)); for (j = 1; j <= num[i]; j++) { scanf(" %c:%lf", &c1, &cc); if (c1 == 'A') ju[0] += cc; else if (c1 == 'B') ju[1] += cc; else if (c1 == 'C') ju[2] += cc; else f = 0; sum += cc; } if (sum <= 1000 && ju[0] <= 600 && ju[1] <= 600 && ju[2] <= 600 && f == 1) { tot++; c[tot] = sum * 100; } } for (i = 1; i <= tot; i++) for (j = v; j >= c[i]; j--) dp[j] = max(dp[j], dp[j - c[i]] + c[i]); x = (double)(dp[v] / 100); printf("%.2lf\n", x); } return 0; }
|