UVA-11529(极角排序)
2016-07-05 23:21
218 查看
题意:
n个不共线的点,任意三个都可以形成一个三角形,平均每个三角形内部包含多少个点;
思路:
反方向考虑,每个点被多少个三角形包含,
枚举每个点,以这个点为原点,再枚举剩下的点,在PI范围内再选两个点组成的三角行,那原点一定不在这些三角形中,统计不在哪些三角形走的数目那在哪些三角形中的数目也就知道了;
最后再除一下就是答案了;
AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
}
const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1203;
const int maxn=1005;
double x
,y
,temp[2*N];
LL n;
LL cal(LL x,LL y)
{
LL ans=1;
for(int i=0;i<y;i++)
ans=ans*(x-i);
for(LL i=2;i<=y;i++)
ans/=i;
return ans;
}
double solve()
{
LL ans=0,sum,num=cal(n,3);
for(int i=1;i<=n;i++)
{
sum=0;
int cnt=1;
for(int j=1;j<=n;j++)
{
if(i==j)continue;
temp[cnt]=atan2(y[j]-y[i],x[j]-x[i]);
if(temp[cnt]<0)temp[cnt]+=2*PI;
cnt++;
temp[cnt]=temp[cnt-1]+2*PI;
cnt++;
}
sort(temp+1,temp+cnt);
int pos=1;
for(int j=1;j<n;j++)
{
while(temp[pos]-temp[j]<=PI&&pos<cnt)pos++;//[i+1,pos-1]
LL len=(LL)(pos-1-j);
if(len>=2)sum=sum+cal(len,2);
}
ans=cal(n-1,3)-sum+ans;
}
return ans*1.0/(num*1.0);
}
int main()
{
int Case=0;
while(1)
{
read(n);
if(!n)break;
for(int i=1;i<=n;i++)
scanf("%lf%lf",&x[i],&y[i]);
printf("City %d: %.2lf\n",++Case,solve());
}
return 0;
}
n个不共线的点,任意三个都可以形成一个三角形,平均每个三角形内部包含多少个点;
思路:
反方向考虑,每个点被多少个三角形包含,
枚举每个点,以这个点为原点,再枚举剩下的点,在PI范围内再选两个点组成的三角行,那原点一定不在这些三角形中,统计不在哪些三角形走的数目那在哪些三角形中的数目也就知道了;
最后再除一下就是答案了;
AC代码:
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
}
const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1203;
const int maxn=1005;
double x
,y
,temp[2*N];
LL n;
LL cal(LL x,LL y)
{
LL ans=1;
for(int i=0;i<y;i++)
ans=ans*(x-i);
for(LL i=2;i<=y;i++)
ans/=i;
return ans;
}
double solve()
{
LL ans=0,sum,num=cal(n,3);
for(int i=1;i<=n;i++)
{
sum=0;
int cnt=1;
for(int j=1;j<=n;j++)
{
if(i==j)continue;
temp[cnt]=atan2(y[j]-y[i],x[j]-x[i]);
if(temp[cnt]<0)temp[cnt]+=2*PI;
cnt++;
temp[cnt]=temp[cnt-1]+2*PI;
cnt++;
}
sort(temp+1,temp+cnt);
int pos=1;
for(int j=1;j<n;j++)
{
while(temp[pos]-temp[j]<=PI&&pos<cnt)pos++;//[i+1,pos-1]
LL len=(LL)(pos-1-j);
if(len>=2)sum=sum+cal(len,2);
}
ans=cal(n-1,3)-sum+ans;
}
return ans*1.0/(num*1.0);
}
int main()
{
int Case=0;
while(1)
{
read(n);
if(!n)break;
for(int i=1;i<=n;i++)
scanf("%lf%lf",&x[i],&y[i]);
printf("City %d: %.2lf\n",++Case,solve());
}
return 0;
}
相关文章推荐
- 1.10055 - Hashmat the brave warrior
- 2.10071 - Back to High School Physics
- 3.458 - The Decoder
- 4.694 - The Collatz Sequence
- 6.494 - Kindergarten Counting Game
- 7.490 - Rotating Sentences
- 8.414 - Machined Surfaces
- 9.488 - Triangle Wave
- A.457 - Linear Cellular Automata
- B.489 - Hangman Judge
- C.445 - Marvelous Mazes
- 1.10494 - If We Were a Child Again
- 2.424 - Integer Inquiry
- 3.10250 - The Other Two Trees
- 5.465 - Overflow
- 6.113 - Power of Cryptography
- 7.10161 - Ant on a Chessboard
- 8.621 - Secret Research
- 9.401 - Palindromes
- A.537 - Artificial Intelligence?