struct node
{
int left;
int right;
};
node line[maxn];
结构体
区间重叠问题
(以区间数最多为例,左右端点重合也算重叠)
bool compare(const node &a,const node &b)
{
if(a.left!=b.left)
return a.left<b.left;
return a.right<b.right;
}
int main()
{
sort(line+1,line+1+n,compare);
int tot=0;
int last=line[1].right;
for(int i=1;i<=n;i++)
{
if(line[i].left>last)
{
last=line[i].right;
tot+=1;
}
}
printf("%d",tot);
}
区间(全)覆盖
bool compare(const node &a,const node &b)
{
if(a.left!=b.left)
return a.left<b.left;
return a.right<b.right;
}
int main()
{
sort(line+1,line+1+n,compare);
int last=1;
int tot=0;
int i=1;
while(last<=n)
{
int t=0;
while(line[i].left<=last)
t=max(t,line[i++].right);
last=t+1;
tot+=1;
}
printf("%d",tot);
}
区间选点(选出最少的点,使这些点都在所有线段中)
bool compare(const node &a,const node &b)
{
if(a.left!=b.left)
return a.left<b.left;
return a.right<b.right;
}
int main()
{
sort(line+1,line+1+n,compare);
int last=line[1].right,tot=0;
for(int i=1;i<=n;i++)
{
if(line[i].left>last)
{
last=line[i].right;
tot+=1;
}
last=min(last,line[i].right);
}
printf("%d",tot);
}