当前位置:网站首页>辰辰采草药
辰辰采草药
2022-04-22 05:35:00 【lxt1101】
辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师。为此,他想拜附近最有威望的医师为师。医师为了判断他的资质,给他出了一个难题。医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个山洞里有一些不同的草药,采每一株都需要一些时间,每一株也有它自身的价值。我会给你一段时间,在这段时间里,你可以采到一些草药。如果你是一个聪明的孩子,你应该可以让采到的草药的总价值最大。”
如果你是辰辰,你能完成这个任务吗?
输入格式
第一行有两个整数 TT(1 \le T \le 10001≤T≤1000)和 MM(1 \le M \le 1001≤M≤100),用一个空格隔开,T代表总共能够用来采药的时间,MM 代表山洞里的草药的数目。
接下来的 MM 行每行包括两个在 11 到 100100 之间(包括 11 和 100100 )的整数,分别表示采摘某株草药的时间和这株草药的价值。
输出格式
包括一行,这一行只包含一个整数,表示在规定的时间内,可以采到的草药的最大总价值。
数据范围
对于 30\%30% 的数据,M \le 10M≤10;
对于全部的数据, M \le 100M≤100。
input:
70 3 71 100 69 1 1 2
output:
3
题解:这是一个经典的背包问题,也是一个贪心问题,在选择是否才这珠草药都先从局部最优解入手。只有采这珠草药和不采这珠草药就是01背包问题,所以直接按照01背包的模板来就行
#include<iostream>
#include<algorithm>
using namespace std;
int t, n;
int x[1010], y[110];//x表示时间。y表示药草价值
int v[110][1010];//表示最大价值
int main() {
cin >> t >> n;
//读入数据
for (int i = 1; i <= n; i++) {
cin >> x[i] >> y[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= t; j++) {
if (x[i] > j) {
v[i][j] = v[i - 1][j];
}
else {
int value1 = v[i - 1][j - x[i]] + y[i];//挖这株草药
int value2 = v[i - 1][j];//不挖这株草药
if (value1 > value2) {
v[i][j] = value1;
}
else {
v[i][j] = value2;
}
}
}
}
cout << v[n][t] << endl;
return 0;
}
版权声明
本文为[lxt1101]所创,转载请带上原文链接,感谢
https://blog.csdn.net/lxt1101/article/details/123460508
边栏推荐
- OBDUMPER常用导出命令
- Mycat的读写分离
- 多线程Executors的使用
- C language mid-term review training
- MySQL Chapter 6 installation and use of Navicat
- 1.计算a+b
- The unreal engine uses loadclass to load the blueprint class
- General compilation methods of third-party libraries such as libcurl
- Strong connected component of "tarjan" undirected graph
- MySQL 第6章 Navicat 的安装与使用
猜你喜欢

MySQL Chapter 6 installation and use of Navicat

Application and selection of it power distribution and fire current limiting protector

MySQL JDBC programming

Method for coexistence of Keil-C51 and keil arm

Enquête JVM

C language version: establishment and basic operation of chain team

JVM探究

JVM探究

JUC concurrent programming

认识和安装MySQL
随机推荐
生成excel模板(下拉选、多级联动)
LockSupport.park和unpark,wait和notify
Using easyexcel to export excel forms
IDEA值得推荐的20款优秀的插件
单机部署redis主从与哨兵模式(一主两从三哨兵)
POI 和 EasyExcel练习
Do you know the three implementation methods of strlen?
2022.4.21-----leetcode. eight hundred and twenty-four
5.The Simple Problem
二叉树五个重要性质(附图理解)
常见的状态码
再见2020,2021我来了
MySQL数据库基础
Method for coexistence of Keil-C51 and keil arm
MySQL基础命令及练习题(一)
剑指 Offer 22. 链表中倒数第k个节点
线性筛法(素数筛)
Do you know the implementation of strcpy?
Advanced part of MySQL
[nanny installation tutorial] download MySQL 5 from the source code of Linux operating system seven