CC MSTONES(Milestones-概率)

本文探讨了一个王国中剩余里程石的重构问题,通过输入文档记录的里程石坐标,找出同一直线上最多能有多少个里程石。在特定约束条件下,利用几何原理和随机抽样方法解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Milestones

Problem code: MSTONES

All submissions for this problem are available.

Once upon a time, there was a Kingdom of Seven Roads. Besides a fancy name, it actually had exactly 7 straight roads. Its residents wanted to keep track of the distances they traveled so they placed milestones along some roads. The roads slowly deteriorated and disappeared but some milestones remained. Archeologists documented remaining milestones and want to reconstruct the kingdom, starting with its main road. Help them by finding the maximum number of collinear milestones.

Input

The first line contains a single integer T, the number of test cases. The first line of each testcase contains the number of documented milestones N. Following lines give the coordinates (Xi, Yi) of those milestones. Coordinates of all milestones will be different.

Output

For each test case, output the maximum number of collinear milestones.

Constraints

  • T <= 30
  • 1 <= N <= 10 000
  • -15 000 <= Xi, Yi <= 15 000

Example

Input:
2

5
0 0
1 0
2 0
1 1
3 1

2
1 1
10 10

Output:
3
2

Author: thocevar
Tester: subra
Editorial http://discuss.codechef.com/problems/MSTONES
Tags feb11 hard thocevar
Date Added: 12-12-2010
Time Limit: 5 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.3.2, CPP 4.8.1, CPP11, CS2, D, ERL, FORT, FS, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC




题目大意:给若干点,它们可被7条直线覆盖,求在同一直线上的点的最大个数

解法:由题意可知这条直线最少覆盖n/7个点,否则那7条直线不可能覆盖点集。

如果我们随便取2个点,它们连成1条直线,则它是答案的概率=(1/7)^2=1/49

随便取K次,都不是答案的概率=(48/49)^K K=200时,数值很小,基本可以断定。




#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXT (30+10)
#define MAXN (10000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int T,n;
int x[MAXN],y[MAXN];
bool is_out(int i,int j,int k)
{
	int X1=x[i]-x[j],Y1=y[i]-y[j];
	int X2=x[i]-x[k],Y2=y[i]-y[k];
	return X1*Y2-X2*Y1;
}
int rand(int n)
{
	return rand()%n+1;
}
int main()
{
//	freopen("MileStones.in","r",stdin);
//	freopen("MileStones.out","w",stdout);
	scanf("%d",&T);
	For(t,T)
	{
		scanf("%d",&n);
		For(i,n) scanf("%d%d",&x[i],&y[i]);
		int K=200,ans=0;
		if (n<=2) {printf("%d\n",n);continue;}
		
		while(K--)
		{
			int i=rand(n),j=rand(n),tot=0;
			while (i==j) i=rand(n),j=rand(n);
			For(k,n)
				if ((k^i)&&(k^j)) tot+=is_out(i,j,k);
			ans=max(ans,n-tot);
			
		}
		printf("%d\n",ans);
		
	}

	
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值