2414. Social Distancing 2

2414. Social Distancing 2

Title description

Due to the outbreak of the highly contagious cattle infectious disease COWVID-19, Farmer John is very concerned about the health of his cows.
Although he did his best to make his N cows (1≤N≤1000) practice "social distance", many cows were unfortunately infected with diseases. The cows numbered 1 ... N are located at different positions on the long straight road (equivalent to the one-dimensional axis), and the cow i is located at position xi. Farmer John knows that there is a radius R, and any cow that is no more than R units away from an infected cow will also be infected (then it will be transmitted to cows that are within R units of it, and so on).
Unfortunately, Farmer John does not know exactly the value of R. He only knew which of his cows were infected. Given this data, find the minimum number of cows initially infected with the disease.

Input

The first line of input contains N. Each of the following N lines uses two integers x and s to describe a cow, where x is the position (0≤x≤10 ^ 6), s is 0 for healthy cows, 1 for infected cows, and all may be due to transmission The infected cows are all infected.

Output

Output the minimum number of cows that have become sick before the disease begins to spread.

Sample input

6
7 1
1 1
15 1
3 1
10 0
6 1

Sample output

3

Data range limitation

prompt

In this example, we know that R <3, otherwise the cow at position 7 will be transmitted to the cow at position 10. Therefore, at least 3 cows were initially infected: one of the two cows at positions 1 and 3, one of the two cows at positions 6 and 7, and the cow at position 15.

The idea is roughly divided into two steps

  1. Find the maximum radius of infection (only the largest radius of infection, the smallest initial infected cattle)
  2. Determine the initial infected cow by the infection radius.

Method one (barrel row):
Although this method is simple, but the space is a bit large

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int MAX=2147483647;
const int N=1e6+10;
long long n,f[N],x,y,minn=MAX,maxx,ans;
int main()
{
	fre(socdist2);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{		
		scanf("%d%d",&x,&y);
		maxx=max(maxx,x);
		if(!y) f[x]=1;
		else   f[x]=2;
	}
	x=1100000,y=2200000;
	for(int i=1;i<=maxx;i++)
	{
		minn=min(minn,abs(x-y)-1);
		if(f[i]==1) y=i;
		if(f[i]==2) x=i;
	}
	minn=min(minn,abs(x-y)-1);	x=-1;
	for(int i=1;i<=maxx;i++) 
		if(f[i]==2) 
		{
			if(i>x) ans++;
			x=i+minn;
		}
	printf("%d\n",ans);
	return 0;
}

Method two (sorting):
By sorting, the above problems are avoided, with high efficiency and small space!

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
using namespace std;
const int MAX=2147483647;
const int N=1005,M=1e6+5;
int n,t,f[N],C=M,ans=1;
struct node
{
	int x,s;
} a[N];
bool cmp(node a,node b) {return a.x<b.x;}
void input()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)	scanf("%d%d",&a[i].x,&a[i].s);
}
void work()
{
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;i++)	//printf("%d %d\n",a[i].x,a[i].s);
	{
		if(a[i].s) f[++t]=a[i].x;
		if(i!=1&&((a[i].s&&!a[i-1].s)||(!a[i].s&&a[i-1].s))) //cout<<a[i].x<<"   "<<a[i-1].x<<endl;
		{
			int temp=a[i].x-a[i-1].x-1;
			C=min(C,temp);	
		}
	}
	//cout<<C<<endl;
	for(int i=2;i<=t;i++)	
	{
		if(f[i]-f[i-1]>C) ans++;
	}
	cout<<ans<<endl;
}


int main()
{
	fre(socdist2);
	input();
	work();
	return 0;
}
Published 130 original articles · Like 93 · Visitors 6800

Guess you like

Origin blog.csdn.net/bigwinner888/article/details/105442593