当前位置:网站首页>Class homework (5) -- 576. Hungry cattle
Class homework (5) -- 576. Hungry cattle
2022-07-23 15:55:00 【xyc20120615】
Catalog
Description
The cattle line up in front of the feed trough . The feed trough is used in turn 1 To N(1≤N≤2000) Number . Every night , A lucky cow according to John's rules , Eat some of the feed in the trough .
John offers B A list of intervals . An interval is a pair of integers start−end(1≤start≤end≤N), It means some continuous feed tanks , such as 1-3,7-8,3-4 wait . Cattle can choose any range , But the range chosen by cattle cannot overlap .
Of course , Cattle hope they can eat as much as possible . Give some intervals , Help the cow find some ranges , Make it eat the most .
In the example above ,1-3 and 3-4 It's overlapping ; Smart cattle choose {1-3,7-8}, So you can eat 5 Something in a slot .
Format
Input
The first 1 That's ok , Integers B(1≤B≤1000).
The first 2∼B+1 That's ok , Two integers per line , Represents an interval , The smaller endpoint is in front .
Output
Only one integer , It indicates the maximum number of food in the trough .
Samples
input data 1
3
1 3
7 8
3 4
Output data 1
5
Limitation
1s, 1024KiB for each test case
Answer key :
Be similar to Arrangement of the lecture hall , The idea is similar , That is to open a structure and change it into 0/1 knapsack , When looping, pay attention to the subscript 0 To traverse the .
State transition equation :
dp[j]=max(dp[j],dp[a[i].s-1]+a[i].h);// State transition equation Program :
#include <bits/stdc++.h>
using namespace std;
int n,dp[2010];
struct H{
int s,e,h;//s Is the subscript of the first trough of the interval ,e Is the subscript of the last trough of the interval ,h Is the number of slots in the interval
}a[2010];
bool cmp(H x,H y){// Sorting criteria
return x.e<y.e;
}
int main(){
cin>>n;
for(int i=0;i<n;i++){// Input the subscript of the first trough of the interval and the subscript of the last trough of the interval, and calculate the number of trough of the interval
cin>>a[i].s>>a[i].e;
a[i].h=a[i].e-a[i].s+1;
}
sort(a,a+n,cmp);// Sort
for(int i=0;i<n;i++){// Traverse each trough section
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);// State transition equation
}
cout<<dp[a[n-1].e];
return 0;
}边栏推荐
- Redis中 LRU和LFU的淘汰策略区别
- Bug modification
- Guangzhou held a competition for quality and safety supervisors of agricultural products in the town and street
- php:filter伪协议之[BSidesCF 2020]Had a bad day
- 如何成为一个优雅的硬件工程师?
- 奔驰新能源产品线:豪华新能源市场或将改变格局
- Pydensecrf installation
- Six ways of uniapp route jump
- 超详细MP4格式分析
- (Zset)Redis底层是如何用跳表进行存储的
猜你喜欢

Expression du suffixe (une question par jour pendant les vacances d'été 4)

没有了华为,高通任意涨价,缺乏核心技术的国产手机只能任由宰割

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

云服务器ECS远程监控

Application of ERP management system in equipment manufacturing enterprise management

Time series data in industrial Internet of things

Gear 月度更新|6 月

C语言注释的方法

Analysis of data governance

A quietly rising domestic software is too strong!
随机推荐
C# 关闭当前电脑指令
xxl-job 实现email发送警告的代码解析(一行一行代码解读)
Cookie和Session的区别
As a tester, you cannot fail to understand ADB commands and operations
Clickhouse, let the query fly!!!
ten thousand and one hundred
Gear 月度更新|6 月
[try to hack] SQL injection less7 (into outfile and Boolean blind annotation)
什么是真正的 HTAP ?(二)挑战篇
pydensecrf安装
第三篇 RBAC权限管理 数据库设计详解
aws篇6 aws iot
Six ways of uniapp route jump
Harbor image warehouse
Find a specific number in an ordered array (binary search or half search)
(Zset)Redis底层是如何用跳表进行存储的
云服务器ECS远程监控
C语言书籍推荐
专访|开源之夏新星牛学蔚
C # calculate the number of times a character appears in the string