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<N≤300), which represents the number of beads.
Each of the next N lines contains two integer x and y, represents the coordinate of the beads. You can assume that 0<x,y<10000.
The last case is followed by a line containing one zero.

输出

 For each case, print your answer with the following format:
 If it is clockwise rotation without removing any beads, please print “C; otherwise if it is counterclockwise rotation without removing any beads, print “CC” instead; otherwise, suppose remove at least x beads to make a clockwise rotation and remove at least y beads to make a counterclockwise rotation. If xy, print “Remove x bead(s), C”, otherwise print “Removey bead(s), CC” instead.
Your output format should imitate the sample output. Print a blank line after each test case.

示例输入

3
1 1
2 2
3 3 3
1 1
2 2
1 1 4
1 1
2 2
3 3
2 2 0

示例输出

C
CC
Remove 1 bead(s), C

提示

 

来源

2010年山东省第一届ACM大学生程序设计竞赛

 
  计算几何 + DP
  题意是给你n个点,第i个点和第i+1个点可以构成向量,问最少删除多少个点可以让构成的向量顺时针旋转或者逆时针旋转。
  思路
  计算几何用的知识是求叉积和点积,这道题可以加深理解这两个计算几何基础知识点的用法。叉积的作用是判断两个向量的左右(顺逆),点积的作用是判断两个向量的前后。举个例子,假设有2个向量v1,v2,‘*’暂时代表叉积运算,‘·’暂时代表点积运算。叉积判定:如果v1*v2>0,则v1在v2的顺时针方向;如果v1*v2=0,则v1、v2共线;如果v1*v2<0,则v1在v2的逆时针方向。点积判定:如果v1·v2>0,则v1和v2都指向同一侧面;如果v1·v2=0,则v1和v2垂直;如果v1·v2<0,则v1和v2都指向相反的侧面。
  DP是用来记录所有可能情况的最大向量数,dp[j][i]表示以向量ji(第j个点到第i个点构成的向量)为终点的最大顺时针/逆时针向量数。状态转移方程为 dp[j][i] = max{dp[k][j]+1}。
  判断如果顺时针逆时针关系可以用叉积,如果共线再用点积判断同方向还是反方向。
 
  注意的点:
  1、规定顺时针的旋转范围是(0<=T<180),逆时针的旋转范围是(0<T<=180),也就是说如果两条向量共线的话,顺时针旋转可以同方向(T=0),不能反方向;逆时针旋转可以反方向(T=180),不能同方向。
  2、如果删掉一定点后,顺时针旋转的最大向量数和逆时针的一样,则取顺时针的值输出。否则会WA。
  
  代码:
 #include <iostream>
#include <cmath>
#include <string.h>
using namespace std;
#define eps 1e-10
int dp[][];
/********** 定义点 **********/
struct Point{
double x,y;
Point(double x=,double y=):x(x),y(y) {}
};
Point p[];
/********** 定义向量 **********/
typedef Point Vector;
/********** 点 - 点 = 向量 **********/
Vector operator - (Point a,Point b)
{
return Vector(a.x-b.x,a.y-b.y);
}
/********** 2向量求叉积 **********/
double Cross(Vector a,Vector b)
{
return a.x*b.y-b.x*a.y;
}
/********** 向量点积 **********/
double Dot(Vector a,Vector b)
{
return a.x*b.x+a.y*b.y;
}
bool check1(int i,int j,int k) //核对向量ji是否在向量kj的顺时针方向或者同方向
{
if(k==) return true;
Vector v1 = p[i]-p[j]; //向量ji
Vector v2 = p[j]-p[k]; //向量kj
double x = Cross(v1,v2);
if(fabs(x)<eps){ //向量ji和kj共线,判断一下两向量方向。
double d = Dot(v1,v2);
if(d>eps) //顺时针可以有同方向(0≤T<180)
return true;
else //反方向
return false;
}
else if(x>eps){ //向量ji在向量kj的顺时针方向
return true;
}
return false;
}
bool check2(int i,int j,int k)
{
if(k==) return true;
Vector v1 = p[i]-p[j]; //向量ji
Vector v2 = p[j]-p[k]; //向量kj
double x = Cross(v1,v2);
if(fabs(x)<eps){ //向量ji和kj共线,判断一下两向量方向
double d = Dot(v1,v2);
if(d>eps) //同方向
return false;
else //逆时针可以有反方向(0 < T ≤ 180)
return true;
}
else if(x<eps){ //向量ji在向量kj的逆时针方向
return true;
}
return false;
}
int main()
{
int n;
while(cin>>n){
if(n==) break;
//dp[j][i]表示以向量ji(第j个点到第i个点构成的向量)为终点的最大顺时针向量数
int i,j,k;
for(i=;i<=n;i++) //输入n个点
cin>>p[i].x>>p[i].y;
int r1=,r2=; //最大向量数
//dp
memset(dp,,sizeof(dp));
for(i=;j<=n;i++)
for(j=;j<i;j++){
int Max = ;
for(k=;k<i;k++){
if(check1(i,j,k)){
if(dp[k][j]+>Max)
Max = dp[k][j]+;
}
}
dp[j][i]=Max;
if(dp[j][i]>r1)
r1 = dp[j][i];
}
memset(dp,,sizeof(dp));
for(i=;j<=n;i++)
for(j=;j<i;j++){
int Max = ;
for(k=;k<i;k++){
if(check2(i,j,k)){
if(dp[k][j]+>Max)
Max = dp[k][j]+;
}
}
dp[j][i]=Max;
if(dp[j][i]>r2)
r2 = dp[j][i];
}
if(r1==n-) //向量数比点数少一个
cout<<"C"<<endl;
else if(r2==n-)
cout<<"CC"<<endl;
else if(r1>=r2)
cout<<"Remove "<<n--r1<<" bead(s), C"<<endl;
else
cout<<"Remove "<<n--r2<<" bead(s), CC"<<endl;
cout<<endl;
}
return ;
}

Freecode : www.cnblogs.com/yym2013

sdut 2153:Clockwise(第一届山东省省赛原题,计算几何+DP)的更多相关文章

  1. sdut 2159:Ivan comes again!(第一届山东省省赛原题,STL之set使用)

    Ivan comes again! Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 The Fairy Ivan gave Say ...

  2. sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)

    Balloons Time Limit: 1000MS Memory limit: 65536K 题目描述 Both Saya and Kudo like balloons. One day, the ...

  3. sdut 2154:Shopping(第一届山东省省赛原题,水题)

    Shopping Time Limit: 1000MS Memory limit: 65536K 题目描述 Saya and Kudo go shopping together.You can ass ...

  4. sdut 2158:Hello World!(第一届山东省省赛原题,水题,穷举)

    Hello World! Time Limit: 1000MS Memory limit: 65536K 题目描述 We know that Ivan gives Saya three problem ...

  5. sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)

    The Android University ACM Team Selection Contest Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里 ...

  6. sdut 2163:Identifiers(第二届山东省省赛原题,水题)

    Identifiers Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  Identifier is an important c ...

  7. sdut 2165:Crack Mathmen(第二届山东省省赛原题,数论)

    Crack Mathmen Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  Since mathmen take securit ...

  8. Rectangles(第七届ACM省赛原题+最长上升子序列)

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1255 描述 Given N (4 <= N <= 100)  rec ...

  9. sdut 2153 Clockwise (2010年山东省第一届ACM大学生程序设计竞赛)

    题目大意: n个点,第i个点和第i+1个点可以构成向量,问最少删除多少个点可以让构成的向量顺时针旋转或者逆时针旋转. 分析: dp很好想,dp[j][i]表示以向量ji(第j个点到第i个点构成的向量) ...

随机推荐

  1. 【Oracle】查看正在运行的存储过程

    select name from v$db_object_cache where locks > 0 and pins > 0 and type='PROCEDURE';

  2. yum groupinstall报错,处理方法

    http://www.cnblogs.com/xiaoluo501395377/archive/2013/05/21/3089970.html ===== 创建repo库 # createrepo - ...

  3. “好奇号”火星车和它搭载的软件(来自Erlang程序员的观点)

    http://www.aqee.net/on-curiosity-and-its-software/

  4. JavaScript异常处理

    http://www.yaosansi.com/post/747.html 异常处理概述      在代码的运行过程中,错误是不可避免的,总的来说,错误发生于两种情况:一是程序内部的逻辑或者语法错误, ...

  5. EMQ ---客户端上线自动订阅主题

    通过修改配置文件即可实现. emq v2.3.11,软件架构做了调整,把功能集成在了emq_modules模块,/data/loaded_plugins默认会加载emq_modules. 我们只需要改 ...

  6. curl命令常用参数

    -a/--append 上传文件时,附加到目标文件 -A/--user-agent <string> 设置用户代理发送给服务器 -anyauth 可以使用“任何”身份验证方法 -b/--c ...

  7. 实现简单容器模板类Vec(vector capacity 增长问题、allocator 内存分配器)

    首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下:  C++ Code  1 2   template < class _Ty, cl ...

  8. 初始化列表(const和引用成员)、拷贝构造函数

    一.构造函数初始化列表 推荐在构造函数初始化列表中进行初始化 构造函数的执行分为两个阶段 初始化段 普通计算段 (一).对象成员及其初始化  C++ Code  1 2 3 4 5 6 7 8 9 1 ...

  9. Android异步任务处理框架AsyncTask源代码分析

    [转载请注明出处:http://blog.csdn.net/feiduclear_up CSDN 废墟的树] 引言 在平时项目开发中难免会遇到异步耗时的任务(比方最常见的网络请求).遇到这样的问题.我 ...

  10. 简单认识DataSet与DataTable

    DataSet 是放在内存中的,对DataSet中数据的修改并不直接反应到数据库,要通过 DataAdapter 的 Update 方法更新回数据库; DataSet相当你用的数据库: DataTab ...