标签:n+1 namespace desc 条件 int -- and ant rip
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2969
题意:
\(Question1:\)
求覆盖一段区间所需的最少区间数量。
\(Question2:\)
求最少的区间数,使任意取区间都能覆盖整段区间
首先离散化
\(Question1:\)
普通贪心,将区间按左端点排序,在满足左端点条件下贪心取右端点最大值
\(Question2:\)
差分,寻找被区间覆盖最少的那个点,根据容斥原理,必须用区间总数减去覆盖那个点的区间数量再加\(1\)
\(C++ Code:\)
#include<cstdio>
#include<iostream>
#include<algorithm>
#define ll long long
#define N 400005
using namespace std;
struct node
{
int x,y;
bool operator < (const node b) const
{
return (x<b.x)||(x==b.x&&y<b.y);
}
}a[N];
ll c[N],d[N][2];
int n,cnt,t,last,ans,ans2,q[N];
ll l;
int main()
{
scanf("%d%lld",&n,&l);
for (int i=1;i<=n;i++)
{
scanf("%lld%lld",&c[i*2-1],&c[i*2]);
d[i][0]=c[i*2-1],d[i][1]=c[i*2];
}
sort(c+1,c+n+n+1);
cnt=unique(c+1,c+n+n+1)-c-1;
for (int i=1;i<=n;i++)
{
a[i].x=lower_bound(c+1,c+cnt+1,d[i][0])-c;
a[i].y=lower_bound(c+1,c+cnt+1,d[i][1])-c;
}
l=lower_bound(c+1,c+cnt+1,l)-c;
sort(a+1,a+n+1);
int s,t=1;
int g=1;
while (t<l)
{
s=t;
while (a[g].x<=s&&g<=n)
{
t=max(t,a[g].y);
g++;
}
ans++;
}
for (int i=1;i<=n;i++)
{
q[a[i].x]++;
q[a[i].y]--;
}
ans2=n;
s=0;
for (int i=1;i<l;i++)
{
s+=q[i];
ans2=min(s,ans2);
}
printf("%d %d\n",ans,n-ans2+1);
}
Aizu2969 Universal and Existential Quantifiers
标签:n+1 namespace desc 条件 int -- and ant rip
原文地址:https://www.cnblogs.com/GK0328/p/13376572.html