山东省第一届ACM省赛 C SDUT 2153 Clockwise(dp+计算几何)


Clockwise


Time Limit: 1000ms | Memory limit: 65536K 有疑问?点这里^

题目描述

Saya have a long necklace with N beads, and she signs the beads from 1 to N. Then she fixes them to the wall to show N-1 vectors – vector i starts from bead i and end up with bead i+1.
One day, Kudo comes to Saya’s home, and she sees the beads on the wall. Kudo says it is not beautiful, and let Saya make it better.
She says: “I think it will be better if it is clockwise rotation. It means that to any vector i (i < N-1), it will have the same direction with vector i+1 after clockwise rotate T degrees, while 0≤T<180.”
It is hard for Saya to reset the beads’ places, so she can only remove some beads. To saving the beads, although she agrees with Kudo’s suggestion, she thinks counterclockwise rotation is also acceptable. A counterclockwise rotation means to any vector i (i < N-1), it will have the same direction with vector i+1 after counterclockwise rotate T degrees, while 0 < T ≤ 180.”
Saya starts to compute at least how many beads she should remove to make a clockwise rotation or a counterclockwise rotation.
Since the necklace is very-very long, can you help her to solve this problem?
输入

The input consists of several test cases.
The first line of input in each test case contains one integer N (2

#include 
#define LL long long
#define eps 1e-10
using namespace std;

class Point
{
public:
    double x, y;
    Point(double x = 0, double y = 0):x(x),y(y) {}
};
Point p[310];
int dp[310][310];
Point operator - (Point a, Point b)
{
    return Point(a.x-b.x, a.y-b.y);
}
double Cross(Point a, Point b)
{
    return a.x*b.y-b.x*a.y;
}
double Dot(Point a, Point b)
{
    return a.x* b.x + a.y * b.y;
}

bool Check(int i, int j, int k, bool f)
{
    if(!k) return true;
    Point v1 = p[i] - p[j];
    Point v2 = p[j] - p[k];
    double x = Cross(v1, v2);
    if(fabs(x) < eps)
    {
        double d = Dot(v1,v2);
        if(d > eps) return f;
        return !f;
    }
    else if(x > eps) return f;
    return !f;
}

int main()
{
    int n;
    while(~scanf("%d",&n) && n)
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%lf %lf", &p[i].x, &p[i].y);
        }
        int ans1 = 0;
        int ans2 = 0;
        memset(dp, 0, sizeof(dp));
        for(int i=2; i<=n; i++)
        {
            for(int j=1; jint MAX = 0;
                for(int k=0; kif(Check(i, j, k, true)) MAX = max(MAX, dp[k][j]+1);
                dp[j][i] = MAX;
                ans1 = max(dp[j][i],ans1);
            }
        }

        memset(dp, 0, sizeof(dp));
        for(int i=2; i<=n; i++)
        {
            for(int j=1; jint MAX = 0;
                for(int k=0; kif(Check(i, j, k, false)) MAX = max(MAX, dp[k][j]+1);
                dp[j][i] = MAX;
                ans2 = max(dp[j][i], ans2);
            }
        }
        if(ans1 == n-1)
            printf("C\n");
        else if(ans2 == n-1)
            printf("CC\n");
        else if(ans1 >= ans2)
            printf("Remove %d bead(s), C\n", n-1-ans1);
        else
            printf("Remove %d bead(s), CC\n", n-1-ans2);
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(计算几何,动态规划,sdut)