题目链接:https://vjudge.net/problem/UVA-11186
题意:给定一个圆的半径,圆心为(0,0)。给定圆上n个点,圆上任意三个点比不共线,任意三点均可以组成三角形,求能组成的所有三角形的面积之和。
思路:直接n^3枚举所有的三角形,对面积求和即可;n^2容斥推理也可以,参考的是这个博客http://blog.csdn.net/shimmer_/article/details/38613079,讲得很清楚。
代码:
N^3复杂度:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<sstream>
#include<deque>
#include<stack>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const int maxn = 10000 + 20;
const int maxt = 300 + 10;
const int mod = 10;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int Dis[] = {-1, 1, -5, 5};
const double inf = 0x3f3f3f3f;
const int MOD = 1000;
const double PI = acos(-1.0);
int n, m, k;
int R;
struct node{
double x, y;
node(double x = 0.0, double y = 0.0) : x(x), y(y) {}
};
vector<node> v;
double change(double q){
return q / 180.0 * PI;
}
double Len(node n1, node n2){//求两点之间距离
return sqrt((n1.x - n2.x) * (n1.x - n2.x) + (n1.y - n2.y) * (n1.y - n2.y));
}
double Area(double a, double b, double c){//已知三边求三角形面积
double p = (a + b + c) / 2.0;
return sqrt(p * (p - a) * (p - b) * (p - c));
}
double dis[maxn][maxn];
int main(){
while(~scanf("%d%d", &n, &R) && n && R){
v.clear();
memset(dis, 0, sizeof dis);
double q;
for(int i = 0; i < n; ++i){
scanf("%lf", &q);
q = change(q);
v.push_back(node((double)R * cos(q), (double)R * sin(q)));
}
for(int i = 0; i < n; ++i){//需要预处理一下,不然在n^3里求会超时
for(int j = 0; j < i; ++j){
dis[i][j] = dis[j][i] = Len(v[i], v[j]);
}
}
double ans = 0.0;
double len1, len2, len3;
for(int i = 0; i < n; ++i){
for(int j = i + 1; j < n; ++j){
for(int k = j + 1; k < n; ++k){
ans += Area(dis[i][j], dis[i][k], dis[j][k]);
}
}
}
printf("%.0f\n", ans);
}
return 0;
}
N^3复杂度:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<cstdlib> #include<sstream> #include<deque> #include<stack> #include<set> #include<map> using namespace std; typedef long long ll; typedef unsigned long long ull; const double eps = 1e-9; const int maxn = 10000 + 20; const int maxt = 300 + 10; const int mod = 10; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, -1, 1}; const int Dis[] = {-1, 1, -5, 5}; const double inf = 0x3f3f3f3f; const int MOD = 1000; const double PI = acos(-1.0); int n, m, k; int R; struct node{ double x, y; node(double x = 0.0, double y = 0.0) : x(x), y(y) {} }; vector<node> v; double change(double q){ return q / 180.0 * PI; } double Area(double x1, double y1, double x2, double y2, double x3, double y3){//已知三角形三点坐标求三角形面积 return fabs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)) / 2.0; } int main(){ while(~scanf("%d%d", &n, &R) && n && R){ v.clear(); double q; for(int i = 0; i < n; ++i){ scanf("%lf", &q); q = change(q); v.push_back(node((double)R * cos(q), (double)R * sin(q))); } double ans = 0.0; double len1, len2, len3; for(int i = 0; i < n; ++i){ for(int j = i + 1; j < n; ++j){ for(int k = j + 1; k < n; ++k){ ans += Area(v[i].x, v[i].y, v[j].x, v[j].y, v[k].x, v[k].y); } } } printf("%.0f\n", ans); } return 0; }
N^2复杂度:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<cstdlib> #include<sstream> #include<deque> #include<stack> #include<set> #include<map> using namespace std; typedef long long ll; typedef unsigned long long ull; const double eps = 1e-9; const int maxn = 10000 + 20; const int maxt = 300 + 10; const int mod = 10; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, -1, 1}; const int Dis[] = {-1, 1, -5, 5}; const double inf = 0x3f3f3f3f; const int MOD = 1000; const double PI = acos(-1.0); int n, m, k; int R; double change(double q){ return q / 180.0 * PI; } double deg[maxn]; int main(){ while(~scanf("%d%d", &n, &R) && n && R){ memset(deg, 0, sizeof deg); double q; for(int i = 0; i < n; ++i){ scanf("%lf", &q); deg[i] = change(q); } sort(deg, deg + n); double ans = 0; for(int i = 0; i < n; ++i){ for(int j = i + 1; j < n; ++j){ ans += (n + 2 * i - 2 * j) * sin(deg[j] - deg[i]);//看博客链接里的讲解很清楚 } } ans = ans * R * R / 2.0; printf("%.0f\n", ans); } return 0; }