度度熊的午饭时光
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
Problem Description
度度熊最期待每天的午饭时光,因为早饭菜品清淡,晚饭减肥不敢吃太多(胖纸的忧伤T.T)。
百度食堂的午餐超级丰富,祖国各大菜系应有尽有,度度熊在每个窗口都有爱吃的菜品,而且他还为喜爱的菜品打了分,吃货的情怀呀(>.<)。
但是,好吃的饭菜总是很贵,每天的午饭预算有限,请帮度度熊算一算,怎样打饭才能买到的最好吃的饭菜?(不超过预算、不重样、午餐等分最高的情况下,选择菜品序号加和最小,加和相等时字典序最小的组合)
Input
第一行一个整数T,表示T组数据。
每组测试数据将以如下格式从标准输入读入:B
N
score_1 cost_1
score_2 cost_2
:
score_N cost_N 第一行,正整数B(0 <= B <= 1000),代表午餐的预算。第二行,正整数N (0 <= N <= 100),代表午餐可选的菜品数量
从第三行到第 (N + 2) 行,每行两个正整数,以空格分隔,score_i表示菜品的得分,cost_i表示菜品的价格 (0 <= score_i, cost_i <= 100)。
Output
对于每组数据,输出两行:
第一行输出:”Case #i:”。i代表第i组测试数据。
第二行输出菜品的总得分和总花费,以空格分隔。
第三行输出所选菜品的序号,菜品序号从1开始,以空格分隔。
Sample Input
2
29
6
9 10
3 4
6 5
7 20
10 9
15 11
0
2
2 23
10 12
Sample Output
Case #1:
34 29
2 3 5 6
Case #2:
0 0
思路
01背包 + 记录路径 + 字典序
感想
数据很水,所以就算不好好考虑字典序也一样能AC。反而我考虑了字典序后因为格式WA和其他原因了N次。
AC代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<map>
using namespace std;
#define LL long long
#define LD long double
#define cl(a,b) memset(a,b,sizeof(a));
#define s score
#define c cost
#define ts total_score
#define tc total_cost
const int INF=1<<30;
const int MAXB = 1005; //max B
const int MAXN = 105; //max N
int t, b, n;//in
int score[MAXN], cost[MAXN];//in
int total_score, total_cost;//out
int n2;// number of order number
int sum;//sum of order number
int dp[MAXN][MAXB];
bool path[MAXN][MAXB];
bool num[MAXN];//order number
int main(){
cin>>t;
for(int ti=1;ti<=t;ti++){
//init
tc = 0, n2 = 0, sum = 0;
cl(dp,0);
cl(path,false);
cl(num,false);
//input
scanf("%d%d",&b,&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&s[i],&c[i]);
}
//dp & record path
for(int i=1;i<=n;i++){
for(int j=0;j<=b;j++){
if(j<c[i]){
dp[i][j]=dp[i-1][j];
}
else{
if(dp[i-1][j] < dp[i-1][j-c[i]]+s[i]){
dp[i][j] = dp[i-1][j-c[i]]+s[i];
path[i][j] = true;
}
else{
dp[i][j]=dp[i-1][j];
path[i][j] = false;
}
}
}
}
//total_score
ts = dp[n][b];
//search path & record order number & sum
for(int i=n,j=b;i>0;i--){
if(path[i][j]){
j -= c[i];
num[i] = true;
sum += i;
n2++;
}
}
//deal with order number
for(int i=1;i<=n;i++){
if(num[i]){
tc += c[i];
}
}
//output 1
printf("Case #%d:\n",ti);
cout<<ts<<" "<<tc<<endl;
//output 2
for(int i=1,j=0;i<=n;i++){
if(num[i]){
if(j!=0)printf(" ");
else j = 1;
printf("%d",i);
}
}
if(n2>0)printf("\n");
}
return 0;
}