2020 Winter Holiday 【gmoj2414】 【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 is 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

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.

analysis

This part was transferred from @kejin 大佬 's blog.
We now have to find r and then find the final answer d.
Save all uninfected cows first, and then enumerate them. Left and right expand from the enumerated position to both sides until they encounter an infected cow.
r = m i n r , m i n l r r = min (r, min (position of uninfected cattle − l, r − position of uninfected cattle)

After calculating r, we enumerate each point (not only the n nn points with cows), so that the size of the infected area is va. If an infected cow is encountered, determine if va <= 0, ans ++ , And then assign va to r regardless of whether va is <= 0. If no infected cattle are encountered, va−−.

Finally, output va.

Code on

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int n,a[1000001],b[1001],mx,mn,d,ans;
int main()
{
    freopen("socdist2.in","r",stdin);
	freopen("socdist2.out","w",stdout);
	cin>>n;
	int x,y,t=0; 
	for(int i=1;i<=n;i++)
	{
		cin>>x>>y;
		if(y==1) a[x]=2;
		if(y==0)
		{
			a[x]=1;
			t++;
			b[t]=x;
		}
		mx=max(mx,x);
		mn=min(mn,x);
	}
	sort(b+1,b+t+1);
	int d=2147483647;
	for(int i=1;i<=t;i++)
	{
		int l=b[i],r=b[i];
		while(a[l]!=2)
		{
			l--;
			if(l<mn)
			{
				l=-1000001;
				break;
			}
		}
		while(a[r]!=2)
		{
			r++;
			if(r>mx)
			{
				r=1000001;
				break;
			}
		}
		d=min(d,min(b[i]-l,r-b[i]));
	}
	d-=1;
	int temp=0;
	for(int i=mn;i<=mx;i++)
	{
		if(a[i]==2)
		{
			if(temp<=0) ans++;
			temp=d;
		}
		else temp--;
	}
	cout<<ans;
	fclose(stdin);
	fclose(stdout); 
    return 0;
}

Published 110 original articles · 100 praises · views 7998

Guess you like

Origin blog.csdn.net/dglyr/article/details/105693835