时间限制:20000ms
单点时限:1000ms
内存限制:256MB

描述

小Hi在游乐园中获得了M张奖券,这些奖券可以用来兑换奖品。

可供兑换的奖品一共有N件。第i件奖品需要Wi张奖券才能兑换到,其价值是Pi。

小Hi使用不超过M张奖券所能兑换到的最大奖品总价值是多少?

输入

第一行两个整数N,M。

接下来N行,每行两个整数Wi,Pi。

对于 50%的数据: 1≤N,M≤1000

对于 100%的数据: 1≤N,M≤105,1≤Pi,Wi≤10。

输出

一行一个整数,表示最大的价值。

样例输入
3 10
2 3
8 8
10 10
样例输出
11 
 1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<math.h>
4 int main(void){
5 int n = 0, m = 0;
6 int ret = 0;
7 scanf("%d %d", &n, &m);
8 int **array = (int**)malloc((n+1)*sizeof(int*));
9 array[0] = (int*)malloc((m+1)*sizeof(int));
10 for (int j = 0; j < m+1; j++){
11 array[0][j] = 0;
12 }
13 int **map = (int**)malloc(11*sizeof(int*));
14 for (int i = 0; i < 11; i++){
15 map[i] = (int*)calloc(11,sizeof(int));
16 }
17 int value = 0, weight = 0;
18 for (int i = 1; i < n + 1; i++){
19 scanf("%d %d", &weight, &value);
20 map[weight][value]++;
21 }
22 int count = 1;
23 for (int temp_weight = 1; temp_weight < 11; temp_weight++){
24 for (int temp_value = 1; temp_value < 11; temp_value++){
25 if (map[temp_weight][temp_value] != 0){
26 int temp = 1;
27 int temp_sum = 0;
28 while (temp_sum + temp <= map[temp_weight][temp_value]){
29 temp_sum = temp_sum + temp;
30 array[count] = (int*)malloc((m + 1)*sizeof(int));
31 array[count][0] = 0;
32 weight = temp*temp_weight;
33 value = temp*temp_value;
34 for (int j = 1; j < m + 1; j++){
35 if (j - weight<0){
36 array[count][j] = array[count - 1][j];
37 }
38 else{
39 array[count][j] = array[count - 1][j]>array[count - 1][j - weight] + value ? array[count - 1][j] : array[count - 1][j - weight] + value;
40 }
41 }
42 free(array[count - 1]);
43 count++;
44 temp = temp * 2;
45 }
46 if (map[temp_weight][temp_value] - temp_sum >0){
47 temp = map[temp_weight][temp_value] - temp_sum;
48 array[count] = (int*)malloc((m + 1)*sizeof(int));
49 array[count][0] = 0;
50 weight = temp*temp_weight;
51 value = temp*temp_value;
52 for (int j = 1; j < m + 1; j++){
53 if (j - weight<0){
54 array[count][j] = array[count - 1][j];
55 }
56 else{
57 array[count][j] = array[count - 1][j]>array[count - 1][j - weight] + value ? array[count - 1][j] : array[count - 1][j - weight] + value;
58 }
59 }
60 free(array[count - 1]);
61 count++;
62 }
63 }
64 }
65 }
66 printf("%d", array[count-1][m]);
67 return 0;
68 }

参考:https://hihocoder.com/discuss/question/5199

hiho一下 第195周 奖券兑换[C solution][Accepted]的更多相关文章

  1. 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point

    // 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point // 思路:直接暴力绝对T // 先确定x范围,每个x范围内,离圆心最远的点一定是y轴两端的点.枚举x的范围,再 ...

  2. hiho一下 第115周:网络流一•Ford-Fulkerson算法 (Edmond-Karp,Dinic,SAP)

    来看一道最大流模板水题,借这道题来学习一下最大流的几个算法. 分别用Edmond-Karp,Dinic ,SAP来实现最大流算法. 从运行结过来看明显SAP+当前弧优化+gap优化速度最快.   hi ...

  3. 【hiho一下第77周】递归-减而治之 (MS面试题:Koch Snowflake)

    本题是一道微软面试题,看起来复杂,解出来会发现其实是一个很简单的递归问题,但是这道题的递归思路是很值得我们反复推敲的. 原题为hihocoder第77周的题目. 描述 Koch Snowflake i ...

  4. hiho一下 第207周

    题目1 : The Lastest Time 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is latest time you can make with ...

  5. hiho一下第128周 后缀自动机二·重复旋律5

    #1445 : 后缀自动机二·重复旋律5 时间限制:10000ms 单点时限:2000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数 ...

  6. 【hiho一下】第一周 最长回文子串

    题目1:最长回文子串 题目原文:http://hihocoder.com/contest/hiho1/problem/1 [题目解读] 题目与 POJ 3974 palindrome 基本同样.求解最 ...

  7. Solution: 最近公共祖先·一 [hiho一下 第十三周]

    题目1 : 最近公共祖先·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho最近发现了一个神奇的网站!虽然还不够像58同城那样神奇,但这个网站仍然让小Ho乐在其中 ...

  8. hiho一下十六周 RMQ-ST算法

    RMQ-ST算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在美国旅行了相当长的一段时间之后,终于准备要回国啦!而在回国之前,他们准备去超市采购一些当 ...

  9. hiho一下 第九十七周 数论六·模线性方程组

    题目1 : 数论六·模线性方程组 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:今天我听到一个挺有意思的故事! 小Hi:什么故事啊? 小Ho:说秦末,刘邦的将军 ...

随机推荐

  1. Codeforces Round #668 (Div. 2) B. Array Cancellation (思维,贪心)

    题意:有一个长度为\(n\)并且所有元素和为\(0\)的序列,你可以使\(a_{i}-1\)并且\(a_{j}+1\),如果\(i<j\),那么这步操作就是免费的,否则需要花费一次操作,问最少操 ...

  2. C#(winform)button去掉各种边框

    仔细读完,主要在FlatAppearance属性里 1.既然是添加背景图片 所以这里应该使用 Button.BackgroudImage = "" ;来设置图片 而不应该使用  B ...

  3. Nginx基础 - Nginx+Lua实现灰度发布与WAF

    1.Nginx加载Lua环境默认情况下Nginx不支持Lua模块, 需要安装LuaJIT解释器, 并且需要重新编译Nginx, 建议使用openrestry 1)环境准备 [root@localhos ...

  4. codeforces 1010 C. Border【exgcd】

    题目链接:戳这里 学习博客:戳这里 题意:给n种数,n种数取任意个任意组合相加为sum,求sum%k有哪些值. 解题思路: 由exgcd可知(具体用到的是贝祖定理),ax + by = c,满足gcd ...

  5. redis字符串-sds

    redis自己实现了一种名为简单动态字符串的抽象类型(simple dynamic string)作为字符串的表示.下面将简单介绍sds的实现原理. 一.sds的结构

  6. DC1(msf drupal7+suid-find提权)

    这边我们靶机是仅主机模式,IP是192.168.56.101,,直接上msf拿到shell,  不过payload要改一下 改成php/meterperter/bind_tcp 拿到shell了 ,采 ...

  7. 深入 Python 解释器源码,我终于搞明白了字符串驻留的原理!

    英文:https://arpitbhayani.me/blogs/string-interning 作者:arpit 译者:豌豆花下猫("Python猫"公众号作者) 声明:本翻译 ...

  8. Github markdown页面内跳转

    基本操作: 请看这里 最典型的就是[alt_content](#jump) 但有时, jump是不太好直接看出来的, 比如下面这个标题, 格式复杂, 那如何获取相应的jump呢? 在Github中, ...

  9. css scroll text without wrap & webkit-scrollbar

    css scroll text without wrap hidden webkit-scrollbar .tabs-title-box::-webkit-scrollbar, .tabs-conte ...

  10. Linux & change username & computer name & .bashrc

    Linux & change username & computer name ubuntu change username and computer name https://ask ...