例题8-6 两亲性分子(Amphiphilic Carbon Molecules, ACM/ICPC Shanghai 2004, UVa1606)

来源:互联网 发布:mac限免 编辑:程序博客网 时间:2024/05/24 20:06
1. 运用了扫描法,并在扫描的过程中动态的维护计数,简化了计算。
2. 运用了等价转化的思想,把所有黑点关于中心对称化为白点,便可只计算一侧的白点数。
3. 由叉积的性质,可以扫描一侧的点,并避免了浮点运算。
4. 扫描操作设立一条轴l,枚举所有点,另一条轴r用来扫描,第一次扫过π角度,之后动态维护。

5. 在扫描操作中,要注意避免死循环。

摘自https://www.cnblogs.com/dutlei/archive/2013/01/14/2860332.html

在C语言的math.h或C++中的cmath中有两个求反正切的函数atan(double x)与atan2(double y,double x)  他们返回的值是弧度 要转化为角度再自己处理下。

前者接受的是一个正切值(直线的斜率)得到夹角,但是由于正切的规律性本可以有两个角度的但它却只返回一个,因为atan的值域是从-90~90 也就是它只处理一四象限,所以一般不用它。

第二个atan2(double y,double x) 其中y代表已知点的Y坐标 同理x ,返回值是此点与远点连线与x轴正方向的夹角,这样它就可以处理四个象限的任意情况了,它的值域相应的也就是-180~180了

例如:

例1:斜率是1的直线的夹角

cout<<atan(1.0)*180/PI;//45°

cout<<atan2(1.0,1.0)*180/PI;//45° 第一象限

cout<<atan2(-1.0,-1.0)*180/PI;//-135°第三象限

后两个斜率都是1 但是atan只能求出一个45°

例2:斜率是-1的直线的角度

cout<<atan(-1.0)*180/PI;//-45°

cout<<atan2(-1.0,1.0)*180/PI;//-45° y为负 在第四象限

cout<<atan2(1.0,-1.0)*180/PI;//135° x为负 在第二象限

 

常用的不是求过原点的直线的夹角 往往是求一个线段的夹角 这对于atan2就更是如鱼得水了

例如求A(1.0,1.0) B(3.0,3.0)这个线段AB与x轴正方向的夹角

用atan2表示为 atan2(y2-y1,x2-x1) 即 atan2(3.0-1.0,3.0-1.0)

它的原理就相当于把A点平移到原点B点相应变成B'(x2-x1,y2-y1)点 这样就又回到先前了

例三:

A(0.0,5.0) B(5.0,10.0)

线段AB的夹角为

cout<<atan2(5.0,5.0)*180/PI;//45°

#include <set>#include <map>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <deque>#include <cstdio>#include <string>#include <vector>#include <cctype>#include <sstream>#include <utility>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#define SF(a) scanf("%d", &a)#define PF(a) printf("%d\n", a)  #define SFF(a, b) scanf("%d%d", &a, &b)  #define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)#define SFFFF(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)#define CLEAR(a, b) memset(a, b, sizeof(a))#define IN() freopen("in.txt", "r", stdin)#define OUT() freopen("out.txt", "w", stdout)#define FOR(i, a, b) for(int i = a; i < b; ++i)#define LL long long#define maxn 1005#define maxm 205#define mod 1000000007#define INF 10000007#define eps 1e-4using namespace std;//-------------------------CHC------------------------------//struct Point {int x, y;double angle;bool operator<(const Point &rhs) const {return angle < rhs.angle;}}p[maxn], t[maxn];int n, k;int color[maxn];int cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }int solve() {if (n <= 2) return 2;int ans = 0;FOR(i, 0, n) {k = 0;printf("center = (%d, %d)\n", p[i].x, p[i].y);FOR(j, 0, n) {if (j == i) continue;t[k].x = p[j].x - p[i].x;t[k].y = p[j].y - p[i].y;if (color[j]) t[k].x = -t[k].x, t[k].y = -t[k].y;t[k].angle = atan2(t[k].y, t[k].x);++k;}sort(t, t + k);int l = 0, r = 0;int cnt = 2;while (l < k) {printf("l(%d, %d), r(%d, %d)\n", t[l].x, t[l].y, t[r].x, t[r].y);if (l == r) ++cnt, r = (r + 1) % k;//避免叉积恒大于0而死循环while (l != r && cross(t[l], t[r]) >= 0) ++cnt, r = (r + 1) % k;++l, --cnt;ans = max(ans, cnt);}}return ans;}int main() {//IN(); OUT();while (SF(n) && n) {FOR(i, 0, n) SFFF(p[i].x, p[i].y, color[i]);PF(solve());}}