Problem Description
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup" competition,you must have seem this title.If you haven't seen it before,it doesn't matter,I will give you a link:

Here is the link:http://acm.hdu.edu.cn/showproblem.php?pid=2602

Today we are not desiring the maximum value of bones,but the K-th maximum value of the bones.NOTICE that,we considerate two ways that get the same value of bones are the same.That means,it will be a strictly decreasing sequence from the 1st maximum , 2nd maximum .. to the K-th maximum.

If the total number of different values is less than K,just ouput 0.

 
Input
The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, K(N <= 100 , V <= 1000 , K <= 30)representing the number of bones and the volume of his bag and the K we need. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
 
Output
One integer per line representing the K-th maximum of the total value (this number will be less than 231).
 
Sample Input
3
5 10 2
1 2 3 4 5
5 4 3 2 1
5 10 12
1 2 3 4 5
5 4 3 2 1
5 10 16
1 2 3 4 5
5 4 3 2 1
 
Sample Output
12
2
0
 
思路:
对于求最优解的情况,我们对每一种状态只保存了该状态下的最优解,忽略了其他解,进而实现状态之间的转移,而对于求第K优解的情况呢?其实只需要保存每一种状态下的前K优解,从这K个状态进行状态间的转移,同时去重,保存当前状态的K优解即可。(感觉时间复杂度还是挺高的)
 
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
using namespace std; const int N = ;
const int INF = 0x3fffffff;
const long long MOD = ;
typedef long long LL;
#define met(a,b) (memset(a,b,sizeof(a))) int dp[N][];
int a[N], b[N], c[N];
///dp[j][k] 代表容量为 j 的背包的第 k+1 优解 int cmp(int a, int b)
{
return a > b;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int i, j, k, n, v; scanf("%d%d%d", &n, &v, &k); met(a, );
met(b, );
met(dp, ); for(i=; i<=n; i++)
scanf("%d", &a[i]);
for(i=; i<=n; i++)
scanf("%d", &b[i]); for(i=; i<=n; i++)
{
for(j=v; j>=b[i]; j--)
{
int w = ;
for(int z=; z<k; z++) ///每次只需考虑前 k 优解的状态转换即可
{
c[w++] = dp[j][z];
c[w++] = dp[j-b[i]][z]+a[i];
} sort(c, c+w, cmp);
w = unique(c, c+w) - c;
for(int t=; t<k && t<w; t++) ///t的范围, 既不能大于 k,也不能大于 w
dp[j][t] = c[t];
}
} printf("%d\n", dp[v][k-]); }
return ;
}

最新文章

  1. 如何通过Git GUI将自己本地的项目上传至Github
  2. Linux C 学习
  3. 第一个有点作用的PHP扩展
  4. Qt Designer怎样加入资源文件
  5. [转]如何让div中的内容垂直居中
  6. B+树的特点
  7. [HDOJ5877]Weak Pair(DFS,线段树,离散化)
  8. DP——最优三角形剖分
  9. iOS开发之四张图说明GCD(Grand Central Dispatch)附Test源码
  10. NSIS脚本调用C语言写的插件
  11. C++头文件#include&lt;bits/stdc++.h&gt;
  12. 深度理解div+css布局嵌套盒子
  13. AJAX学习前奏----JS基础加强
  14. mac os x 触摸板点击无效
  15. LeetCode 34 - 在排序数组中查找元素的第一个和最后一个位置 - [二分][lower_bound和upper_bound]
  16. springboot +mybatis 搭建完整项目
  17. huawei USG防火墙子接口技术的应用案例
  18. springboot(二十一):SpringBoot使用Mybatis注解开发教程-分页-动态sql
  19. jQuery安装
  20. 【转】Git 安装和使用教程

热门文章

  1. 遇到返回键会退到页面的问题(window.location)
  2. PHP 根据两点的坐标计算之间的距离
  3. devexpress之barManager 使用
  4. 24.Mysql高级安装和升级
  5. 计算器类(C++&JAVA——表达式转换、运算、模板公式)
  6. CentOS6.2网卡绑定配置
  7. JSTL(JSP标准标签库)
  8. 爬虫初窥day4:requests
  9. [转]Firefox+Burpsuite抓包配置(可抓取https)
  10. RecyclerView错误