集训队专题(5.1)1003 maximum shortest distance

maximum shortest distance

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1386    Accepted Submission(s): 469


Problem Description
There are n points in the plane. Your task is to pick k points (k>=2), and make the closest points in these k points as far as possible.
 

Input
For each case, the first line contains two integers n and k. The following n lines represent n points. Each contains two integers x and y. 2<=n<=50, 2<=k<=n, 0<=x,y<10000.
 

Output
For each case, output a line contains a real number with precision up to two decimal places.

 

Sample Input
  
  
3 2 0 0 10 0 0 20
 

Sample Output
  
  
22.36
 

Author
alpc50
 

Source
 

此题是一个最大团的变形问题,题目给出k,和n个点,求出n个点之间的距离,让你求出最小的lim,两点距离大于lim算连通,使得最大团的顶点数不小于k。距离方面可以用二分的思想来优化,而最大团直接套小编给出的最大团模板即可。

#include <math.h>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=50+10;
int g[maxn][maxn],dp[maxn],stk[maxn][maxn],mx;
void dfs(int n, int ns, int dep)
{
    if (ns == 0)
	{
        if (dep > mx) mx = dep;
        return ;
    }
    int i,j,k,p,cnt;
    for(i=0; i<ns; i++)
	{
		k = stk[dep][i];
		cnt = 0;
		if(dep + n - k <= mx) return ;
		if(dep + dp[k] <= mx) return ;
		for(j=i+1; j<ns; j++)
		{
			p = stk[dep][j];
			if(g[k][p]) stk[dep+1][cnt++] = p;
		}
		dfs(n, cnt, dep+1);
	}
    return ;
}
int clique(int n)
{
	int i,j,ns;
	for(mx=0, i=n-1; i>=0; i--)
	{
		for(ns=0, j=i+1; j<n; j++)
			if(g[i][j]) stk[1][ns++] = j;
		dfs(n, ns, 1);
		dp[i] = mx;
	}
	return mx;
}
int x[maxn],y[maxn],n,k;
double arc[maxn][maxn],dis[maxn*maxn];
bool check(double lim)
{
	int i,j;
	memset(g,0,sizeof(g));
	for(int i=0; i<n; i++)
		for(int j=i+1; j<n; j++)
			if(arc[i][j] >= lim)
				g[i][j] = g[j][i] = 1;
	if(clique(n) >= k) return true;
	else return false;
}
int main()
{
	int i,j,cnt;
	while(scanf("%d%d",&n,&k)!=EOF)
	{
		for(i=0; i<n; i++) scanf("%d%d",&x[i],&y[i]);
		cnt=0;
		for(i=0; i<n; i++)
		{
			arc[i][i] = -1;
			for(j=i+1; j<n; j++)
			{
				arc[i][j] = sqrt((double)((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
				dis[cnt++] = arc[j][i] = arc[i][j];
			}
		}
		sort(dis,dis+cnt);
		int l=0,r=cnt-1,mid,ans;
		while(l<=r)
		{
			mid = (l+r)>>1;
			if(check(dis[mid]))
			{
				ans = mid;
				l = mid+1;
			}
			else r = mid-1;
		}
		printf("%.2lf\n",dis[ans]);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, L units away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L). To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river. Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to M rocks (0 ≤ M ≤ N). FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks. Input Line 1: Three space-separated integers: L, N, and M Lines 2..N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position. Output Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks Sample Inputcopy Outputcopy 25 5 2 2 14 11 21 17 4 Hint Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).
07-24

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值