Score Inflation

题意:有N组试题,每组试题得分和所需时间分别为worth[i] 和 cost[i],一组试题可以选取多次,求M时间内能得到的最高分为多少?


解题思路

  1. 典型的无界背包问题,用DP解决
  2. 详情参见http://zh.wikipedia.org/wiki/%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98

代码

/*
ID: zc.rene1
LANG: C
PROG: inflate
 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(void)
{
    FILE *fin, *fout;
    int M, N;
    int *worth, *cost, *result;
    int i, j, temp_max;
    
    fin = fopen("inflate.in", "r");
    fout = fopen("inflate.out", "w");

    fscanf(fin, "%d %d", &M, &N);
    worth = (int *)malloc(N*sizeof(int));
    cost = (int *)malloc(N*sizeof(int));

    for (i=0; i<N; i++)
    {
	fscanf(fin, "%d %d", &worth[i], &cost[i]);
    }

    result = (int *)malloc((M+1)*sizeof(int));
    memset(result, 0, (M+1)*sizeof(int));

    for (i=1; i<=M; i++)
    {
	temp_max = 0;
	for (j=0; j<N; j++)
	{
	    if (cost[j] <= i)
	    {
		if ((worth[j] + result[i - cost[j]]) > temp_max)
		{
		    temp_max = (worth[j] + result[i - cost[j]]);
		}
	    }
	}
	result[i] = (result[i-1] > temp_max)? result[i-1] : temp_max;
    }

    fprintf(fout, "%d\n", result[M]);

    return 0;
}





























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值