训练3 习题24

原创 2016年05月31日 12:54:46

题目:


Problem Description
Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world. As a result, the people in this country are still very proud even if their nation hasn’t been so wealthy any more.<br>The merchants were the most typical, each of them only sold exactly one item, the price was Pi, but they would refuse to make a trade with you if your money were less than Qi, and iSea evaluated every item a value Vi.<br>If he had M units of money, what’s the maximum value iSea could get?<br><br>
 

Input
There are several test cases in the input.<br><br>Each test case begin with two integers N, M (1 ≤ N ≤ 500, 1 ≤ M ≤ 5000), indicating the items’ number and the initial money.<br>Then N lines follow, each line contains three numbers Pi, Qi and Vi (1 ≤ Pi ≤ Qi ≤ 100, 1 ≤ Vi ≤ 1000), their meaning is in the description.<br><br>The input terminates by end of file marker.<br><br>
 

Output
For each test case, output one integer, indicating maximum value iSea could get.<br><br>
 

Sample Input
2 10 10 15 10 5 10 5 3 10 5 10 5 3 5 6 2 7 3
 

Sample Output
5 11
 


思路:本来是简单的背包问题  不过有价格限制  需要满足 价格 差值越小使用它的价值越大(所以先排序)

#include<cstring>  
#include<cstdio>  
//#include<algorithm>  
struct things{  
 int p,q,v;  
}thing[510];  
bool cmp(things a,things b){  
 return (a.q-a.p)<(b.q-b.p);  
}  
using namespace std;


int max(int a1,int a2)
{


if(a1>a2)
return a1;
return a2;
}
int main()  
{  
    int n,maxv,i,j,dp[5010];  
    while(scanf("%d%d",&n,&maxv)!=EOF)  
    {  
        memset(dp,0,sizeof(dp));  
        for(i=0;i<n;i++)  
        {  
            scanf("%d%d%d",&thing[i].p,&thing[i].q,&thing[i].v);  
        }  
        sort(thing,thing+n,cmp);  
        for(i=0;i<n;i++)  
        {  
          for(j=maxv;j>=thing[i].q;j--)  
           {  
  
             dp[j]=max(dp[j],dp[j-thing[i].p]+thing[i].v);  
           }  
        }  
        printf("%d\n",dp[maxv]);  
    }  
    return 0;  
}  

版权声明:本文为博主原创文章,未经博主允许不得转载。

训练3 习题11

思路: Problem Description 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行。请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数。其中,蜂房的结构如下所示。   ...

训练3 习题6

题目:Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的:有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数...

Visual C++基础实践训练2(24)

  • 2009年03月11日 12:54
  • 1.95MB
  • 下载

数据结构与算法(24)——堆习题

题目:请说明有N个元素的堆的高度为logN 题目:给定一个最大堆,查找最小元素...

笨方法学Python 习题 24: 更多练习

#!usr/bin/python # -*-coding:utf-8-*- print ("Let's practice everything.") print ("You\'d need to k...

异常处理与文本IO 习题12_24&12_25

12_24:创建大的数据集:创建一个具有1000行的数据文件。文件中的每行包含了一个教职员工的姓、名、级别以及薪水,第i行的教职员工的姓和名为FristNamei 和LastNamei。级别随机产生为...

习题24 更多练习

Zed 写一首小诗

UVa #10366 Faucet Flow (习题8-24)

#define UVa "8-24.10366.cpp" //Faucet Flow char fileIn[30] = UVa, fileOut[30] = UVa; #include #in...

华为机试在线训练-牛客网(24)迷宫问题

题目描述 定义一个二维数组N*M(其中2 int maze[5][5] = {         0, 1, 0, 0, 0,         0, 1, 0, ...

PAT乙级真题及训练集(24)--1031. 查验身份证(15)(小感触)

1031. 查验身份证(15) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:训练3 习题24
举报原因:
原因补充:

(最多只允许输入30个字)