1
13
2013
1

USACO 5.1

1.Fencing the Cows

二维凸包(Convex Hulls)问题。

以下是usaco training上的教程:

  • Find a point which will be within the convex hull:【注1】
  • Calculate the angle that each point makes with the x axis (within the range 0 to 360 degrees)【注2】
  • Sort the points based on this angle
  • Add the first two points
  • For every other point except the last point
  • Make it the next point in the convex hull
  • Check to see if the angle it forms with the previous two points is greater than 180 degrees. As long as the angle formed with the last two points is greater than 180 degrees, remove the previous point【注3】
  • To add the last point
  • Perform the deletion above,
  • Check to see if the angle the last point forms with the previous point and the first point is greater than 180 degrees or if the angle formed with the last point and the first two points is greater than 180 degrees.【注4】
  • If the first case is true, remove the last point, and continue checking with the next-to-last point.
  • If the second case is true, remove the first point and continue checking.
  • Stop when neither case is true.
  • The adding of the points is linear time, as is the calculation of the angles. Thus, the run-time is dominated by the time of the sort, so gift-wrapping runs in O(nlogn) time, which is provably optimal.

【注1】:

计算点集的中点(可以简单地取所有点在x和y方向上的均值)。此点必在凸包内。

【注2】:

计算各个点与中点的连线与x轴的夹角(在0到2pi之间),这个可以使用C语言math.h库函数中的atan2(double y,double x)来实现,不过注意atan2的值域是(-pi,pi],需要进一步将其映射到[0,2pi)区间内。此外注意atan2(0,0)==0,所以我们可以放心地将其应用到中点和点集中某点重合的情况。

【注3】:

检查逆时针连续的三点是否形成“右转”的角,如果“右转”就把“转角”的点删除,否则会出现一个凹多边形。注意删除一个“转角”之后还要要不断检查之前的点,直到该删除的点全部删除为止。

【注4】:

到此为止从第一个点逆时针转到最后一个点的所有“转角”都成为左转的角了,但是当我们把最后一个点和第一个点相连时,这两个点处的“转角”还可能是“右转”的。为了排除这种情况我们要不断检查第一个和最后一个点,如果“右转”就删除,直到它们全部“左转”为止。


怎样判断是否“右转”:

设有逆时针方向连续的三点(x1,y1),(x2,y2),(x3,y3),首先取两向量<x2-x1,y2-y1>(记为<p1,q1>)和<x3-x2,y3-y2>(记为<p2,q2>),当两向量的向量积的z分量小于0时(即p1q2-p2q1<0)就形成了所谓的“右转”关系。


2.Starry Night

可以简单地应用floodfill来找出所有星座。比较星座是否相同时有些麻烦,因为涉及到平移、旋转、翻转等操作,我是通过修改比对星座时的遍历顺序来实现的,这样最多一共需要比对八种遍历方式下两星座图是否重合。


3.Musical Themes

用一个时间复杂度是O(L*N2)的算法过了。其中N是音符的数量,L是需要比对的Themes的平均长度,由于这个L难以预料,所以多少有点侥幸。思路如下:

用dp[i]表示前i个音符中themes的最大长度,用note数组表示所有的音符,则有

dp[i]={

    dp[i-1]+1(if note[i-dp[i-1]]...note[i]这个序列或其变调形式

              可以在note[0]...note[i-dp[i-1]-1]范围内找到);

    dp[i-1](if not)

}

注意dp[i]比dp[i-1]大1当且仅当note[i]加入后可以与前dp[i-1]个音符形成一个新theme。


usaco的官方题解给出了一个O(N2)的算法,更充分地利用了每一步的计算结果:

Let theme(i,j) be the length of the longest theme which occurs starting at both note i and j. 

Note that if note[i+1]-note[i]==note[j+1]-note[j], than theme(i,j)=1+theme(i+1,j+1). Otherwise, theme(i,j)=1.

Thus, we order the search in such a way that theme(i,j) is tested immediately after theme(i+1,j+1), keeping track of the length of the current theme, as well as the length of the longest theme found so far.

#include <fstream.h>

int     n;
int     note[5000];

int 
main () {
    ifstream filein ("theme.in");
    filein >> n;
    for (int i = 0; i < n; ++i) 
	filein >> note[i];
    filein.close ();

    int     longest = 1;

    for (int i = 1; i < n; ++i) {
	int     length = 1;
	for (int j = n - i - 1 - 1; j >= 0; --j) {
	    if (note[j] - note[j + 1] == note[j + i] - note[j + i + 1]) {
		++length;
		if (length > i) 
		    length = i;
		if (longest < length)
		    longest = length;
	    }
	    else {
		length = 1;
	    }
	}
    }

    ofstream fileout ("theme.out");
    fileout << ((longest >= 5) ? longest : 0) << endl;
    fileout.close ();

    exit (0);
}
Category: USACO | Tags: usaco 凸包 convex hulls | Read Count: 1212
pay for dissertation 说:
2019年10月08日 12:43

Wow! I’m browsing away perusing your web journal from my lap! Simply needed to say I adore perusing your website and anticipate every one of your posts! Carry on the eminent work.Feel free to browse for getting more information about writing work


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com