当前位置:网站首页>Greedy (1) interval complete coverage problem
Greedy (1) interval complete coverage problem
2022-07-29 17:13:00 【seal ink】
(1)区间完全覆盖问题
问题描述:给定一个长度为m的区间,再给出n条线段的起点和终点(注意这里是闭区间),求最少使用多少条线段可以将整个区间完全覆盖
样例:
区间长度8,可选的覆盖线段[2,6],[1,4],[3,6],[3,7],[6,8],[2,4],[3,5]
解题过程:
1将每一个区间按照左端点递增顺序排列,拍完序后为[1,4],[2,4],[2,6],[3,5],[3,6],[3,7],[6,8]
2设置一个变量表示已经覆盖到的区域.再剩下的线段中找出所有左端点小于等于当前已经覆盖到的区域的右端点的线段中,Add the line segment with the largest right endpoint,直到已经覆盖全部的区域
3过程:
假设第一步加入[1,4],那么下一步能够选择的有[2,6],[3,5],[3,6],[3,7],由于7最大,所以下一步选择[3,7],最后一步只能选择[6,8],这个时候刚好达到了8退出,所选区间为3
4贪心证明:
需要最少的线段进行覆盖,那么选取的线段必然要尽量长,而已经覆盖到的区域之前的地方已经无所谓了,(可以理解成所有的可以覆盖的左端点都是已经覆盖到的地方),Then the real thing that can make the line segment longer is the right endpoint,左端点没有太大的意义,所以选择右端点来覆盖
代码如下
“`
# include< cstdio >
#include< iostream >
#include< algorithm >
#define N 100
using namespace std;
struct aa{
int l;
int r;
}a[N];
int cc(aa x,aa y);
int main()
{
int m,n,d=1,e,index=0,k=1,sum=0;
cin>>m>>n;
for(int i=0 ;i < n; i++)
cin>>a[i].l>>a[i].r;
sort(a,a+n,cc);
while(d < m)
{
e=d;
for(int i=index; i < n;i++)
{
if(a[i].l<=e)
{
if(a[i].r>=e)
{
d=a[i].r;
}
}
else
{
index=i;
break;
}
}
if(e>=d)
{
k=0;
break;
}
else
{
sum++;
}
}
if(k) cout<< sum << endl;
else cout<<”-1” << endl;
return 0;
}
int cc(aa x,aa y)
{
if(x.l==y.l)
return x.r < y.r;
else
return x.l < y.l;
}

边栏推荐
猜你喜欢

Twin all things digital visual | join the real world and the digital space

澜舟孟子轻量化预训练模型技术实践

Quantitative Finance

Flutter动态化 | Fair 2.6.0 新版本特性

Google Play 政策更新 | 2022 年 7 月

Groeb - "gramm, explicit and complete n -" gramm mask language model, implements the explicit n - "gramm semantic unit modeling knowledge.
![[Server Storage Data Recovery] A data recovery case of a RAID 5 crash caused by the failure of a certain model of Huawei OceanStor storage RAID 5 hard disk and the failure to synchronize data with the](/img/86/b664e56dff66c57c9f24c850919743.png)
[Server Storage Data Recovery] A data recovery case of a RAID 5 crash caused by the failure of a certain model of Huawei OceanStor storage RAID 5 hard disk and the failure to synchronize data with the

传输层 TCP的连接管理-释放连接四次握手

贪心(1)区间完全覆盖问题

58 security - image quality assessment technology practice
随机推荐
2020年Mobileye收入近10亿美元,EyeQ芯片出货1930万颗
【翻译】设备管理器—英特尔网卡属性设置高级选项的功能
[Server Storage Data Recovery] A data recovery case of a RAID 5 crash caused by the failure of a certain model of Huawei OceanStor storage RAID 5 hard disk and the failure to synchronize data with the
Groeb - "gramm, explicit and complete n -" gramm mask language model, implements the explicit n - "gramm semantic unit modeling knowledge.
1802. 有界数组中指定下标处的最大值【贪心 +二分】
召回 i2i
MySQL外键约束怎么创建
ByteArrayOutputStream class source code analysis
数据库项目01文档:软件测试需要的数据库技能
特殊的类——集合与泛型(C#)
[网络]LAN技术MSTP
Quantitative primary - akshare get the stock code, the minimalist strategy
Talking about the memory layout of the program
sorting and searching 二分查找法
When to use UserCF and when to use ItemCF?
【微信小程序】零基础学 | 小程序语法
Interviewer: What are the design principles?What is the Lie Substitution Principle?
两军交锋
【Leetcode】200. 岛屿数量(中等)
zabbix email sends alert information