[CSP-S Simulation Test]: kill (+ greedy binary answer)

Topic Portal (internal title 50)


Input Format

The first row contains four integers $ n, m, s $, represents the number, and the number of tasks monster point delivery position.
The second line contains n-$ $ integer $ p_1, p_2, ..., p_n $.
The third line contains $ n $ integers $ q_1, q_2, ..., q_n $.


Output Format

Output line contains an integer $ ans $, represents the answer.


Sample

Sample input:

2 4 5
2 10
6 1 4 8

Sample output:

5


Data range and tips

Sample explained:

A first individual play position is $ 4 $ monster, a second individual playing position is $ 8 $ monster, the former for $ 3 $ time, which spend $ 5 $ time, the program corresponding to the time of $ 5 $, and is a best plan.

data range:

For all data: $ 1 \ leqslant p_i, q_i, s \ leqslant {10} ^ 9 $.


answer

Consider half the answer, then how $ judge $.

Greedy resolved, for $ s $ located on the right people, we let him play monster on the left as much as possible, if you can not play hit the right; on the contrary empathy.

Note that the border issue can be.

Time complexity: $ \ Theta (n \ log \ max (p_i, q_i, s) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
int n,m,s;
long long p[5001],q[5001],dis[5001];
bool judge(long long x)
{
	int lft=0,rht=m+1;
	for(int i=1;i<=n;i++)
	{
		if(p[i]>s)break;
		while(abs(p[i]-q[lft])+dis[lft]>x)
		{
			lft++;
			if(lft>m)return 0;
		}
		lft++;
		if(lft>m)return 0;
	}
	lft--;
	for(int i=n;i;i--)
	{
		if(p[i]<=s)break;
		while(abs(p[i]-q[rht])+dis[rht]>x){rht--;if(!rht)return 0;}
		rht--;
		if(!rht)return 0;
	}
	rht++;
	if(rht<=lft)return 0;
	return 1;
}
int main()
{
	scanf("%d%d%d",&n,&m,&s);
	for(int i=1;i<=n;i++)scanf("%lld",&p[i]);
	for(int i=1;i<=m;i++)scanf("%lld",&q[i]);
	sort(p+1,p+n+1);
	sort(q+1,q+m+1);
	for(int i=1;i<=m;i++)dis[i]=abs(q[i]-s);
	dis[0]=dis[m+1]=1LL<<60;
	long long lft=0,rht=1LL<<60;
	while(lft<rht)
	{
		long long mid=(lft+rht)>>1;
		if(!judge(mid))lft=mid+1;
		else rht=mid;
	}
	cout<<lft<<endl;
	return 0;
}

rp ++

Guess you like

Origin www.cnblogs.com/wzc521/p/11563957.html