当前位置:网站首页>Sorting out interval greed problems
Sorting out interval greed problems
2022-07-22 16:01:00 【lovesickman】
I've been studying for sevenoreight months , Although I did brush some questions , But the strength is still the same lj, Some of the topics written at that time were not well sorted out , There are many classic topics that haven't gone deep , How to improve ?
Problem 905. Interval selection
brush 112. Radar equipment We need to use the greedy strategy of selecting points in the interval , At that time, I remember that most of the greedy strategies about interval problems in the basic course were sorted by the right endpoint , When learning, I don't quite understand why I don't use the left endpoint to sort , After seven 、 When I write again in eight months , I found a template problem is not very clear , It's time to be beaten
It's written in left endpoint sort , When analyzing later, it is obvious that there are counter examples , 2 / 10 ( Only two test points can be passed ) It's written in left endpoint sort , When analyzing later, it is obvious that there are counter examples ,2/10( Only two test points can be passed ) It's written in left endpoint sort , When analyzing later, it is obvious that there are counter examples ,2/10( Only two test points can be passed )
Counter example
3
1 4
2 6
3 5 The answer is 2
/*Love coding and thinking!*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo2(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
const int N=2e5+10;
int n,m,_,x;
int a[N],c[N];
int res=1,aver;
struct node
{
int l,r;
}Node[N];
bool cmp(node a,node b)
{
return a.l<b.l;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d%d",&Node[i].l,&Node[i].r);
sort(Node+1,Node+1+n,cmp);// Default left endpoint
int last=Node[1].r;
for(int i=2;i<=n;i++)
{
if(Node[i].l>Node[i-1].r)
res++,last=max(last,Node[i].r);
else
{
last=max(last,Node[i].r);
}
}
cout<<res;
return 0;
}
Later, it was changed blindly in view of the above situation , e m m m , It 's a long story , obvious n = 1 , Output 0 Later, it was changed blindly in view of the above situation ,emmm, It 's a long story , obvious n=1, Output 0 Later, it was changed blindly in view of the above situation ,emmm, It 's a long story , obvious n=1, Output 0
/*Love coding and thinking!*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo2(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
const int N=2e5+10;
int n,m,_,x;
int a[N],c[N];
int res=1,aver;
struct node
{
int l,r;
}Node[N];
bool cmp(node a,node b)
{
return a.l<b.l;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d%d",&Node[i].l,&Node[i].r);
sort(Node+1,Node+1+n,cmp);// Default left endpoint
int last=Node[1].r;s
for(int i=2;i<=n;i++)
{
if(Node[i].l>Node[i-1].r)
res++,last=max(last,Node[i].r);
else
{
res++;
last=Node[i].r;
}
}
cout<<res-1;
return 0;
}
s o r t There is still a problem with the right endpoint , There's no one left sort There is still a problem with the right endpoint , There's no one left sort There is still a problem with the right endpoint , There's no one left
/*Love coding and thinking!*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo2(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
const int N=2e5+10;
struct node
{
int l,r;
}Node[N];
bool cmp(node a,node b)
{
return a.r<b.r;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d%d",&Node[i].l,&Node[i].r);
sort(Node+1,Node+1+n,cmp);// Default left endpoint
int last=Node[1].r;
for(int i=2;i<=n;i++)
{
if(Node[i].l>last)
res++,last=Node[i].r;
else
{
last=Node[i-1].r;
}
}
cout<<res;
return 0;
}
I'm a fool
/*Love coding and thinking!*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo2(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
const int N=2e5+10;
struct node
{
int l,r;
}Node[N];
int res=1,n;
bool cmp(node a,node b)
{
return a.r<b.r;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d%d",&Node[i].l,&Node[i].r);
sort(Node+1,Node+1+n,cmp);// Default left endpoint
// Greedy strategy , If the first i It's an interval l> The right end of the previous interval , answer ++
// Otherwise, the right endpoint is updated .
int last=Node[1].r;
for(int i=2;i<=n;i++)
{
if(Node[i].l>last)
res++,last=Node[i].r;
}
cout<<res;
return 0;
}
Radar equipment
/*Love coding and thinking!*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo2(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
const int N=2e5+10;
struct node
{
int l,r;
}Node[N];
int cnt=0;
struct seg
{
double l,r;
}Seg[N];
bool cmp(seg a,seg b)
{
return a.r<b.r;
}
int res=1,n,D;
int main()
{
cin>>n>>D;
for(int i=1;i<=n;i++)
scanf("%d%d",&Node[i].l,&Node[i].r);
for(int i=1;i<=n;i++)
{
if(Node[i].r>D)
{
cout<<"-1";
return 0;
}
else
{
double d=sqrt(D*D-Node[i].r*Node[i].r);
Seg[cnt].l=Node[i].l-d;
Seg[cnt++].r=Node[i].l+d;
}
}
sort(Seg,Seg+cnt,cmp);// Default left endpoint
// for(int i=0;i<cnt;i++)
// {
// cout<<Seg[i].l<<" "<<Seg[i].r<<endl;
// }
// Greedy strategy , If the first i It's an interval l> The right end of the previous interval , answer ++
// Otherwise, the right endpoint is updated .
double last=Seg[0].r;
for(int i=1;i<cnt;i++)
{
if(Seg[i].l>last)
res++,last=Seg[i].r;
}
cout<<res;
return 0;
}
边栏推荐
- LastWordLen
- [error] solution: not creating XLA devices, TF_ xla_ enable_ xla_ devices not set
- 多米诺骨牌上演:三箭资本崩盘始末
- Enregistrer un résumé de la mesure de pression jmeter
- QT notes - customized qlistwidget
- 单片机外围器件学习攻略,小bai必看
- Design of SKU database for commodity information
- Advertising is everywhere. How can we use advertising to promote our products?
- BookPageCnt
- JS数组对象排序(es6)
猜你喜欢
元宇宙赋能场景金融:商业银行竞争发展新赛道
How to improve the efficiency of test case review?
Statistics, calculate the data ratio of each department in SQL
C language to write 99 multiplication table to realize the output of tables with different triangle shapes
你了解Lumen和Nanite吗?在ue5场景制作中如何使用呢?
Industry digitalization has accelerated in an all-round way, and Intelligent Cloud 2.0 redefines digital transformation
QT笔记——QTimer 和 QTimerEvent简单介绍
【leetcode】
Don't look for it, it's all sorted out for you -- complete collection of SQL statements
SSTI簡單總結和CISCN 2019華東南]Double Secret
随机推荐
Scala变量和数据类型(2)
ENVI_IDL: 批量制作专题地图
C language outputs the number of all daffodils
Monopoly of Web3 social protocol and soul binding token
BookPageCnt
Ssti Summary and ciscn 2019 South East China] double secret
Flask对数据库的查询以及关联
JS array object sorting (ES6)
C语言动态分配内存
[error] solution: not creating XLA devices, TF_ xla_ enable_ xla_ devices not set
Information system project manager must recite the core examination site (48) selection of contract type
img.shape[-2:]/len(img.shape[-2:]):GeneralizedRCNN:original_ image_ Torch in sizes_ assert
拉动日活,使用云函数群发微信小程序订阅消息
QT笔记——QTimer 和 QTimerEvent简单介绍
[js]: splice(), charCodeAt()
Codeforces Round #719 (Div. 3)
2022社交电商模式怎么裂变营销?—分享购
Helm理解和使用
CutNoodles
Collagen protease loaded albumin composite nanoparticles / bovine serum albumin coated ceria nano artificial enzyme