/*
ID:15521201
LANG:C++
PROG:milk2
*/
#include <iostream>
#include <cstdio>
using namespace std;
int milk[1000010];
int main()
{
freopen("milk2.in","r",stdin);
freopen("milk2.out","w",stdout);
int i,n,a,b,begin,in,off,temp,start=1000010,end=0,k;
for(i=0;i<1000010;i++)
milk[i]=0;
cin>>n;
k=n;
while(n--)
{
cin>>a>>b;
milk[a]++;
milk[b]--;
end=(end>b)?end:b;
start=(start>a)?a:start;
}
for(i=start;i<end+1;i++)
{
milk[i]+=milk[i-1];
}
for(i=start,begin=0,temp=in=off=0;i<end+2;i++)
{
if(begin==0&&milk[i]==0)
{
temp++;
}
else if(begin==1&&milk[i]!=0)
{
temp++;
}
else if(begin==0&&milk[i]!=0)
{
off=(off>temp)?off:temp;
temp=1;
begin=1;
}
else if(begin==1&&milk[i]==0)
{
in=(in>temp)?in:temp;
temp=1;
begin=0;
}
}
cout<<in<<' '<<off<<endl;
return 0;
}
前后头定标志和叠加的妙用,把a坐标的值变为1,把b坐标的值变为-1,从a+1开始,每一项叠加前一项,当遇到b坐标时才为0,
这里把a坐标变为1,要用++,不能直接用=1,因为当两组a,b有重复区段时,会有重叠(例如300 500和500 600),接下来就是
求非0区段长度最大值和0区段长度最大值(即in和off)
ID:15521201
LANG:C++
PROG:milk2
*/
#include <iostream>
#include <cstdio>
using namespace std;
int milk[1000010];
int main()
{
freopen("milk2.in","r",stdin);
freopen("milk2.out","w",stdout);
int i,n,a,b,begin,in,off,temp,start=1000010,end=0,k;
for(i=0;i<1000010;i++)
milk[i]=0;
cin>>n;
k=n;
while(n--)
{
cin>>a>>b;
milk[a]++;
milk[b]--;
end=(end>b)?end:b;
start=(start>a)?a:start;
}
for(i=start;i<end+1;i++)
{
milk[i]+=milk[i-1];
}
for(i=start,begin=0,temp=in=off=0;i<end+2;i++)
{
if(begin==0&&milk[i]==0)
{
temp++;
}
else if(begin==1&&milk[i]!=0)
{
temp++;
}
else if(begin==0&&milk[i]!=0)
{
off=(off>temp)?off:temp;
temp=1;
begin=1;
}
else if(begin==1&&milk[i]==0)
{
in=(in>temp)?in:temp;
temp=1;
begin=0;
}
}
cout<<in<<' '<<off<<endl;
return 0;
}
前后头定标志和叠加的妙用,把a坐标的值变为1,把b坐标的值变为-1,从a+1开始,每一项叠加前一项,当遇到b坐标时才为0,
这里把a坐标变为1,要用++,不能直接用=1,因为当两组a,b有重复区段时,会有重叠(例如300 500和500 600),接下来就是
求非0区段长度最大值和0区段长度最大值(即in和off)