当前位置:网站首页>上课作业(5)——#576. 饥饿的牛(hunger)
上课作业(5)——#576. 饥饿的牛(hunger)
2022-07-23 11:21:00 【xyc20120615】
目录
Description
牛在饲料槽前排好了队。饲料槽依次用 1 到 N(1≤N≤2000) 编号。每天晚上,一头幸运的牛根据约翰的规则,吃其中一些槽里的饲料。
约翰提供 B 个区间的清单。一个区间是一对整数 start−end(1≤start≤end≤N),表示一些连续的饲料槽,比如 1-3,7-8,3-4 等等。牛可以任意选择区间,但是牛选择的区间不能有重叠。
当然,牛希望自己能够吃得越多越好。给出一些区间,帮助这只牛找一些区间,使它能吃到最多的东西。
在上面的例子中,1-3 和 3-4 是重叠的;聪明的牛选择 {1-3,7-8},这样可以吃到 5 个槽里的东西。
Format
Input
第 1 行,整数 B(1≤B≤1000)。
第 2∼B+1 行,每行两个整数,表示一个区间,较小的端点在前面。
Output
仅一个整数,表示最多能吃到多少个槽里的食物。
Samples
输入数据 1
3
1 3
7 8
3 4
输出数据 1
5
Limitation
1s, 1024KiB for each test case
题解:
类似于演讲大厅安排,思路也差不多,就是开个结构体变为0/1背包,循环时注意要从下标位0开始遍历。
状态转移方程:
dp[j]=max(dp[j],dp[a[i].s-1]+a[i].h);//状态转移方程 程序:
#include <bits/stdc++.h>
using namespace std;
int n,dp[2010];
struct H{
int s,e,h;//s是区间的第一个食槽下标,e是区间的最后一个食槽下标,h是区间的食槽数
}a[2010];
bool cmp(H x,H y){//排序判断条件
return x.e<y.e;
}
int main(){
cin>>n;
for(int i=0;i<n;i++){//输入区间的第一个食槽下标和区间的最后一个食槽下标并计算出区间的食槽数
cin>>a[i].s>>a[i].e;
a[i].h=a[i].e-a[i].s+1;
}
sort(a,a+n,cmp);//排序
for(int i=0;i<n;i++){//遍历每个食槽区间
for(int j=a[n-1].e;j>=a[i].e;j--)
dp[j]=max(dp[j],dp[a[i].s-1]+a[i].h);//状态转移方程
}
cout<<dp[a[n-1].e];
return 0;
}边栏推荐
- Redis Key没设置过期时间,为什么被主动删除了
- 【论文学习】《Source Mixing and Separation Robust Audio Steganography》
- 【HiFlow】定期发送腾讯云短信发送群
- Part II how to design an RBAC authority system
- C语言书籍推荐
- select......for update 语句的功能是什么? 会锁表还是锁行?
- C语言经典例题-求最少数量钞票
- Find the source code of the thesis
- RTA is a new way to accurately launch advertisements?
- IDEA 提高效率的5大免费插件
猜你喜欢

C语言经典例题-商品检验码

什么是真正的 HTAP ?(二)挑战篇

Gear 月度更新|6 月

【论文学习】《Source Mixing and Separation Robust Audio Steganography》

Safe operation 7.22

C语言经典例题-将输入的两位数转成英文
![[ctfhub] the data of JWT header and payload are transmitted in clear text. If sensitive information is contained in it, sensitive information will be leaked. Try to find the flag. Format is flag{}](/img/d0/133d628a304f5c6b5f0d514c9fe222.jpg)
[ctfhub] the data of JWT header and payload are transmitted in clear text. If sensitive information is contained in it, sensitive information will be leaked. Try to find the flag. Format is flag{}

记一次SQL优化

Quickly master QML Chapter 5 components

C语言经典例题-switch case语句转换日期格式
随机推荐
Part V Druid data source introduction
任务切换的细节
day1
C语言书籍推荐
【Try to Hack】sql注入 Less7 (into outfile和布尔盲注)
Safety 7.18 operation
Analysis of data governance
Gear 月度更新|6 月
Clickhouse, let the query fly!!!
【攻防世界WEB】难度三星9分入门题(上):simple_js、mfw
AWS篇1
专访|开源之夏新星牛学蔚
Conda设置代理
复现各种对抗攻击方法
什么是真正的 HTAP ?(二)挑战篇
Application of ERP management system in equipment manufacturing enterprise management
2022最NB的JVM基础到调优笔记,吃透阿里P6小case
[hiflow] regularly send Tencent cloud SMS sending group
uniapp路由跳转的六种方式
printf函数-转换说明