hiho一下 第195周 奖券兑换[C solution][Accepted]
描述
小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]的更多相关文章
- 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point
// 圆内,求离圆心最远的整数点 hiho一下第111周 Farthest Point // 思路:直接暴力绝对T // 先确定x范围,每个x范围内,离圆心最远的点一定是y轴两端的点.枚举x的范围,再 ...
- hiho一下 第115周:网络流一•Ford-Fulkerson算法 (Edmond-Karp,Dinic,SAP)
来看一道最大流模板水题,借这道题来学习一下最大流的几个算法. 分别用Edmond-Karp,Dinic ,SAP来实现最大流算法. 从运行结过来看明显SAP+当前弧优化+gap优化速度最快. hi ...
- 【hiho一下第77周】递归-减而治之 (MS面试题:Koch Snowflake)
本题是一道微软面试题,看起来复杂,解出来会发现其实是一个很简单的递归问题,但是这道题的递归思路是很值得我们反复推敲的. 原题为hihocoder第77周的题目. 描述 Koch Snowflake i ...
- hiho一下 第207周
题目1 : The Lastest Time 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is latest time you can make with ...
- hiho一下第128周 后缀自动机二·重复旋律5
#1445 : 后缀自动机二·重复旋律5 时间限制:10000ms 单点时限:2000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为一段数构成的数 ...
- 【hiho一下】第一周 最长回文子串
题目1:最长回文子串 题目原文:http://hihocoder.com/contest/hiho1/problem/1 [题目解读] 题目与 POJ 3974 palindrome 基本同样.求解最 ...
- Solution: 最近公共祖先·一 [hiho一下 第十三周]
题目1 : 最近公共祖先·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho最近发现了一个神奇的网站!虽然还不够像58同城那样神奇,但这个网站仍然让小Ho乐在其中 ...
- hiho一下十六周 RMQ-ST算法
RMQ-ST算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在美国旅行了相当长的一段时间之后,终于准备要回国啦!而在回国之前,他们准备去超市采购一些当 ...
- hiho一下 第九十七周 数论六·模线性方程组
题目1 : 数论六·模线性方程组 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:今天我听到一个挺有意思的故事! 小Hi:什么故事啊? 小Ho:说秦末,刘邦的将军 ...
随机推荐
- fzu2202 犯罪嫌疑人
Problem Description 福尔摩斯是个大侦探,他总是在解决疑难案件.这一次的案件也不例外,案件是这样的:有编号为1到N的N位嫌疑犯,他们其中有一个犯了罪,然后每个嫌疑犯都被询问,&quo ...
- L2-013 红色警报 (25分) 并查集复杂度
代码: 1 /* 2 这道题也是简单并查集,并查集复杂度: 3 空间复杂度为O(N),建立一个集合的时间复杂度为O(1),N次合并M查找的时间复杂度为O(M Alpha(N)), 4 这里Alpha是 ...
- HDU 3605 Escape 最大流
题意: 如果这是2012年世界末日怎么办?我不知道该怎么做.但是现在科学家们已经发现,有些星球上的人可以生存,但有些人却不适合居住.现在科学家们需要你的帮助,就是确定所有人都能在这些行星上生活.输入多 ...
- Harbor 镜像仓库搭建
安装 Docker # 下载 Docker 二进制包 [root@k8s-master01 ~]# wget https://download.docker.com/linux/static/stab ...
- 【非原创】sg函数模板
学习博客:戳这里 解题模型: 1.把原游戏分解成多个独立的子游戏,则原游戏的SG函数值是它的所有子游戏的SG函数值的异或. 即sg(G)=sg(G1)^sg(G2)^...^sg(Gn) ...
- flagcounter 被禁用...
源地址 https://s11.flagcounter.com/count2/arWz/bg_FFFFFF/txt_000000/border_CCCCCC/columns_2/maxflags_14 ...
- 读js DOM编程艺术总结
第一章主要介绍一些历史性问题,javascript是Netcape和sun公司合作开发的. 第二章JavaScript语法: 1,数据类型:(弱类型)字符串,数值,布尔值(只有true和false,不 ...
- 图像处理中Stride的理解
一行有 11 个像素(Width = 11), 对一个 32 位(每个像素 4 字节)的图像, Stride = 11 * 4 = 44. 但还有个字节对齐的问题, 譬如: 一行有 11 个像素(Wi ...
- Dart Generic All In One
Dart Generic All In One Dart 泛型 https://dart.dev/guides/language/language-tour#generics /** * * @aut ...
- free useful skills videos courses & tutorials
free useful skills videos courses & tutorials website video courses https://realpython.com/ http ...