2
23
2013
6

HMMs - Summary

Summary of the Hidden Markov Models

(Source: http://www.comp.leeds.ac.uk/roger/HiddenMarkovModels/html_dev/summary/s1_pg1.html)

 

Summary

Frequently, patterns do not appear in isolation but as part of a series in time - this progression can sometimes be used to assist in their recognition. Assumptions are usually made about the time based process - a common assumption is that the process's state is dependent only on the preceding N states - then we have an order N Markov model. The simplest case is N=1.

Various examples exists where the process states (patterns) are not directly observable, but are indirectly, and probabalistically, observable as another set of patterns - we can then define a hidden Markov model - these models have proved to be of great value in many current areas of research, notably speech recognition.

Such models of real processes pose three problems that are amenable to immediate attack; these are :

  • Evaluation : with what probability does a given model generate a given sequence of observations. The forward algorithm solves this problem efficiently.

 

  • Decoding : what sequence of hidden (underlying) states most probably generated a given sequence of observations. The Viterbi algorithm solves this problem efficiently.

     

  • Learning : what model most probably underlies a given sample of observation sequences - that is, what are the parameters of such a model. This problem may be solved by using the forward-backward algorithm.

HMMs have proved to be of great value in analysing real systems; their usual drawback is the over-simplification associated with the Markov assumption - that a state is dependent only on predecessors, and that this dependence is time independent.

A full exposition on HMMs may be found in:

L R Rabiner and B H Juang, `An introduction to HMMs', iEEE ASSP Magazine, 3, 4-16.

Category: Statistics | Tags:
1
30
2013
8

Facebook Hacker Cup 2013 Qualification Round

Beautiful strings
20 points

When John was a little kid he didn't have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could... he evaluated the beauty of strings in a quest to discover the most beautiful string in the world.

Given a string s, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it.

The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty. Johnny doesn't care about whether letters are uppercase or lowercase, so that doesn't affect the beauty of a letter. (Uppercase 'F' is exactly as beautiful as lowercase 'f', for example.)

You're a student writing a report on the youth of this famous hacker. You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string?

Input

The input file consists of a single integer m followed by m lines.

Output

Your output should consist of, for each test case, a line containing the string "Case #x: y" where x is the case number (with 1 being the first case in the input file, 2 being the second, etc.) and y is the maximum beauty for that test case.

Constraints

5 ≤ m ≤ 50
2 ≤ length of s ≤ 500

贪心地把最大beauty给出现频率最大的letter即可。

#include<iostream>
#include<fstream>
#include<string.h>
#include<algorithm>
using namespace std;
//char s[501];
string s;
int m;
ifstream fin;
ofstream fout;
int main(){
	fin.open("beautiful_stringstxt.txt");
	fout.open("output.txt");
	fin>>m;
	getline(fin,s);
	for(int cas=1;cas<=m;cas++){
		getline(fin,s);
		//cout<<s<<endl;
		int len=s.size();
		int count[26];
		for(int i=0;i<26;i++)count[i]=0;
		for(int i=0;i<len;i++){
			char c=s[i];
			if(c<='Z'&&c>='A')c=c-'A'+'a';
			if(c<='z'&&c>='a'){
				count[c-'a']++;
			}
		}
		sort(count,count+26);
		int ans=0;
		for(int i=26;i>=1;i--){
			ans+=i*count[i-1];
		}
		fout<<"Case #"<<cas<<": "<<ans<<endl;
	}
	return 0;
}

Balanced Smileys
35 points

Your friend John uses a lot of emoticons when you talk to him on Messenger. In addition to being a person who likes to express himself through emoticons, he hates unbalanced parenthesis so much that it makes him go :(

Sometimes he puts emoticons within parentheses, and you find it hard to tell if a parenthesis really is a parenthesis or part of an emoticon.

A message has balanced parentheses if it consists of one of the following:

  • - An empty string ""
  • - One or more of the following characters: 'a' to 'z', ' ' (a space) or ':' (a colon)
  • - An open parenthesis '(', followed by a message with balanced parentheses, followed by a close parenthesis ')'.
  • - A message with balanced parentheses followed by another message with balanced parentheses.
  • - A smiley face ":)" or a frowny face ":("

Write a program that determines if there is a way to interpret his message while leaving the parentheses balanced.

Input

The first line of the input contains a number T (1 ≤ T ≤ 50), the number of test cases.
The following T lines each contain a message of length s that you got from John.

Output

For each of the test cases numbered in order from 1 to T, output "Case #i: " followed by a string stating whether or not it is possible that the message had balanced parentheses. If it is, the string should be "YES", else it should be "NO" (all quotes for clarity only)

Constraints

  • 1 ≤ length of s ≤ 100

我写了一个O(n3)的DP,思路正确结果WA了,原因是忽略了“()”这种边界情况:

#include<iostream>
#include<string>
using namespace std;

int T;
string s;
ifstream fin;
ofstream fout;
bool dp[101][101];
int main(){
	fin.open("balanced_smileystxt.txt");
	fout.open("output.txt");
	fin>>T;
	getline(fin,s);
	for(int cas=1;cas<=T;cas++){
		for(int i=0;i<101;i++)for(int j=0;j<101;j++)dp[i][j]=false;
		getline(fin,s);
		int len=s.size();
		int k;
		for(k=0;k<len;k++){
			char c=s[k];
			if(c!=' '&&(c<'a'||c>'z')&&c!=':'&&c!='('&&c!=')'){
				break;
			}
		}
		if(k<len){
			fout<<"Case #"<<cas<<": NO"<<endl;
			continue;
		}
		if(len==0){
			fout<<"Case #"<<cas<<": YES"<<endl;
			continue;
		}
		for(int l=1;l<=len;l++){
			for(int i=0;i+l-1<len;i++){
				if(l==1){
					if(s[i]!='('&&s[i]!=')')dp[i][i]=true;
					else dp[i][i]=false;
					continue;
				}
				else if(l==2){//忽略了“()”的情况!!
					if((s[i]==':'&&s[i+1]==')')||(s[i]==':'&&s[i+1]=='(')){
						dp[i][i+1]=true;
					}
				}
				for(int k=i;k<i+l-1;k++){
					if(dp[i][k]&&dp[k+1][i+l-1]){
						dp[i][i+l-1]=true;
						break;
					}
				}
				if(l>=3){
					if(s[i]=='('&&s[i+l-1]==')'&&dp[i+1][i+l-2]){
						dp[i][i+l-1]=true;
					}
				}
			}
		}
		fout<<"Case #"<<cas<<": ";
		if(dp[0][len-1])fout<<"YES";
		else fout<<"NO";
		fout<<endl;
	}
	return 0;
}

官方题解给出了一个O(n)的答案,实际上这里只需要检测括号是否有可能匹配。


Find the Min
45 points
 

After sending smileys, John decided to play with arrays. Did you know that hackers enjoy playing with arrays? John has a zero-based index array, m, which contains n non-negative integers. However, only the first k values of the array are known to him, and he wants to figure out the rest.

John knows the following: for each index i, where k <= i < n, m[i] is the minimum non-negative integer which is *not* contained in the previous *k* values of m.

For example, if k = 3, n = 4 and the known values of m are [2, 3, 0], he can figure out that m[3] = 1.

John is very busy making the world more open and connected, as such, he doesn't have time to figure out the rest of the array. It is your task to help him.

Given the first k values of m, calculate the nth value of this array. (i.e. m[n - 1]).

Because the values of n and k can be very large, we use a pseudo-random number generator to calculate the first k values of m. Given non-negative integers a, b, c and positive integer r, the known values of m can be calculated as follows:

  • m[0] = a
  • m[i] = (b * m[i - 1] + c) % r, 0 < i < k

Input

The first line contains an integer T (T <= 20), the number of test cases.
This is followed by T test cases, consisting of 2 lines each.
The first line of each test case contains 2 space separated integers, n, k (1 <= k <= 105, k < n <= 109).
The second line of each test case contains 4 space separated integers a, b, c, r (0 <= a, b, c <= 109, 1 <= r <= 109).

Output

For each test case, output a single line containing the case number and the nth element of m.

注意到序列是循环的这个问题就很好解决了,只需要求出第一个“循环节”。这时候一个暴力的方法是O(k2)的,仍然难以接受,为此我加入了一个小优化:

考虑a,x1,...,xk,y这个序列片段,现在我们要求y,如果a<xk并且a在x1~xk-1 中没有再次出现,则y=a;否则我们只需从小到大检验大于xk的数。这样在最坏情况下也可以秒杀。

#include<iostream>
#include<fstream>

using namespace std;
int had[100001];
int circle[100001];
int memo[100001];
int T;
long long n,k;
long long a,b,c,r;
ifstream fin;
ofstream fout;

int main(){
    fin.open("find_the_mintxt.txt");
    fout.open("output.txt");
    fin>>T;
    for(int cas=1;cas<=T;cas++){
        fin>>n>>k;
        fin>>a>>b>>c>>r;
        
        for(int i=0;i<=k;i++)had[i]=0;
        
        long long pre=a;
        if(a<=k)had[a]++;
        memo[0]=a;
        for(int i=1;i<k;i++){
            pre=(b*pre+c)%r;
            if(pre<=k)had[pre]++;
            memo[i]=pre;
            //fout<<memo[i]<<' ';
        }
        //fout<<endl;
        int j;
        for(j=0;j<=k;j++){
            if(had[j]==0)break;
        }
        circle[0]=j;
        if(memo[0]<=k)had[memo[0]]--;
        had[j]++;
        //fout<<circle[0]<<' ';
        
        for(int i=1;i<=k;i++){
            
            if(memo[i-1]<circle[i-1]&&had[memo[i-1]]==0){
                circle[i]=memo[i-1];
            }
            else{
                int x;
                for(x=circle[i-1]+1;x<=k;x++){
                    if(had[x]==0)break;
                }
                circle[i]=x;
            }
            
            
            /*
            int j;
            for(j=0;j<=k;j++){
                if(had[j]==0)break;
            }
            circle[i]=j;
            */
            
            if(i<k){
                if(memo[i]<=k)had[memo[i]]--;
                had[circle[i]]++;
            }
            //fout<<circle[i]<<' ';
        }
        //fout<<endl;
        fout<<"Case #"<<cas<<": "<<circle[(n-k-1)%(k+1)]<<endl;
    }
    return 0;
}

官方题解:

The qualification round is over, and 10169 hackers solved at least one problem. The most exciting things to watch in this round was who finished first, and who would get the last passing submission. There was only one person with a total penalty of less than an hour, which made Mark the clear winner. A few people were playing a game of chicken to see who would get the last submission. The winner of this game submitted his solution to Beautiful Strings with only 4 seconds left in the round, to claim the honor of finishing last among all qualifiers. Congratulations to Ryan for this accomplishment! Reminder: the strategy of finishing last might not work as well for future rounds...

Beautiful Strings

This was the easiest problem in the round. It was attempted by 10697 contestants, and solved by 9865. The main idea is to count the frequency of each letter, then assign the value 26 to the most frequent letter, 25 to the next, etc. If two letters are tied for most frequent, it doesn't matter which of them gets which value, since the sum will be the same. The python code below explains the solution pretty well.

from collections import Counter

def get_beauty(string):

    string = string.lower()

    # Remove all characters other than letters

    string = ''.join(x for x in string if 'a' <= x <= 'z' )

    # Make a dictionary where the keys are letters and the values are counts

    freq = Counter(string)

    # Get the values (letter counts) and sort them in descending order

    arr = freq.values()

    arr.sort()

    arr.reverse()

    # 26 * (count of most common letter) + (25 * next most common) + ...

    values_and_counts = zip(range(26, 0, -1), arr)

    return sum(value * count for value, count in values_and_counts)

Balanced Smileys

This problem was attempted by 7096 contestants, but only 2860 solved it. There are a lot of ways to solve this problem. You could go for the brute force solution, which was O(2^N), a dynamic programming/memoization approach, which would be O(N^2)/O(N^3), or the solution intended by the writer, which was O(N). We decided to let everyone who made a correct solution pass, so any of the above actually passes our tests. The number of passing submissions for this problem is just what we wanted from the qualification round, so we think it was a good call. This post will only cover the O(N) solution.

The idea is to keep track of the possible range of open parentheses.

We use two values, 'minOpen' and 'maxOpen'. Initialize both of these to 0.

Iterate over the message, character by character.

Whenever you encounter a '(', you increment maxOpen, and if it wasn't part of a smiley, you also increment minOpen.

Whenever you encounter a ')', you decrement minOpen, and if it wasn't part of a frowny face, decrement maxOpen. If minOpen is negative, reset it to 0.

If maxOpen ever was negative, or minOpen isn't 0, it wasn't possible that the message had balanced parentheses. Otherwise it was possible. Python code that solves this problem is below.

def isBalanced(message):

    minOpen = 0

    maxOpen = 0

    for i in xrange(len(message)):

        if message[i] == '(':

            maxOpen += 1

            if i != 0 and message[i-1] != ':':

                minOpen += 1

        elif message[i] == ')':

            minOpen = max(0, minOpen-1)

            if i != 0 and message[i-1] != ':':

                maxOpen -= 1

                if maxOpen < 0:

                    break

    if maxOpen >= 0 and minOpen == 0:

        return "YES"

    else:

        return "NO"

Find the Min

This problem was attempted by 2555 contestants, and solved by 1929, making it the hardest problem in this round. The most challenging part of this problem is the large n. To solve this problem, you should notice 2 things:

  1. The maximum result of m[i] (i >= k) will not be larger than k+1, so even though m[i] (i < k) may be as large as 10^9, the solution is never larger than k + 1
  2. The result of m will be repeated every k+1 numbers. This means that we just have to calculate the k+1 next numbers, even if n is very large.

Now we've reduced the problem to the following: find m[k], m[k+1], ..., m[2k+1]. The brute force version of doing this is still too slow (O(k^2)), so we have to use a BST (set/map in C++) to maintain the 'available' values, i.e the values not include in previous k elements. This reduces the time complexity to O(k log k), which should run well within the 6 minutes. For a clean implementation of this idea, take a look at the solution of Mark in first place.

//这里用到了set和multiset来维护数集
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <queue>

using namespace std;

int M[200020];

int main() {
  int N; cin >> N;
  for(int t = 0; t < N; t++) {
    int n, k; cin >> n >> k; n--;
    int a, b, c, r; cin >> a >> b >> c >> r;

    M[0] = a;
    for(int i = 1; i < k; i++) {
      M[i] = (1ll * b * M[i - 1] + c) % r;
    }

    set<int> st;
    for(int i = 0; i <= k; i++) st.insert(i);
    for(int i = 0; i < k; i++) st.erase(M[i]);

    multiset<int> dupst;
    for(int i = 0; i < k; i++) dupst.insert(M[i]);

    for(int i = k; i <= 2 * k; i++) {
      M[i] = *st.begin();
      st.erase(st.begin());

      if(i < 2 * k) {
        dupst.erase(dupst.find(M[i - k]));
        if(M[i - k] <= k && dupst.find(M[i - k]) == dupst.end()) {
          st.insert(M[i - k]);
        }
      }
    }

    cout << "Case #" << (t + 1) << ": ";
    if(n <= 2 * k) {
      cout << M[n] << endl;
    } else {
      cout << M[k + (n - 2 * k - 1) % (k + 1)] << endl;
    }
  }
  return 0;
}

Take a look at people's solutions from https://www.facebook.com/hackercup/scoreboard?round=185564241586420 to get some more pointers on how to solve the problems.

Good luck to everyone that qualified to Round 1!

Writers:

Beautiful Strings: David Alves

Balanced Smileys: Torbjørn Morland

Find the Min: ZiHing Cheung

 

Category: HackerCup | Tags: set multiset dp
1
16
2013
1

USACO 5.2

1.Snail Trail

DFS,Snail的移动路线可以分为一个个线段,每个线段成为DFS的一个阶段,当Snail在一个方向上运动受阻时枚举转向的方向并进入下一阶段。开始时由于我重复搜索运动受阻时的点导致无限重复“撞墙”结果爆栈了,看来在写递归函数时还有很多细节需要注意。

/*
ID: huilong1
LANG: C++
TASK: snail
*/
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;

ifstream fin;
ofstream fout;
int N,B;
char map[121][121];
int direct[4][2]={
	{0,1},{0,-1},{1,0},{-1,0}
};

int dfs(int x,int y){
	int maxlen=0;
	for(int k=0;k<4;k++){
		int len=0;
		int i,j;
		i=x;
		j=y;
		while(1){
			int nx,ny;
			nx=i+direct[k][0];
			ny=j+direct[k][1];
			if(nx>=N||nx<0||ny>=N||ny<0||map[nx][ny]=='#'){
				if(len==0)break;//防止重复“撞墙”造成的爆栈
				int futurlen=dfs(i,j);
				if(len+futurlen>maxlen)maxlen=futurlen+len;
				break;
			}
			if(map[nx][ny]=='-'){
				if(len>maxlen)maxlen=len;
				break;
			}
			i=nx;
			j=ny;
			map[i][j]='-';
			len++;
		}
		if(len==0)continue;//这句很重要,防止消除痕迹时形成死循环
		int ii=x+direct[k][0],jj=y+direct[k][1];
		//回溯,消除痕迹
		while(!(ii==i&&jj==j)){
			map[ii][jj]='.';
			ii+=direct[k][0];
			jj+=direct[k][1];
		}
		map[i][j]='.';
	}
	return maxlen;
}

int main(){
	fin.open("snail.in");
	fout.open("snail.out");
	fin>>N>>B;
	for(int i=0;i<N;i++)for(int j=0;j<N;j++)map[i][j]='.';
	for(int i=0;i<B;i++){
		char x;
		int y;
		fin>>x>>y;
		map[(int)(x-'A')][y-1]='#';
	}
	map[0][0]='-';
	fout<<dfs(0,0)+1<<endl;
	return 0;
}

2.Electric Fences

题目对精度要求不高,可以把平面坐标扩大10倍,这样就只需搜索0=<x<=1000和0<=y<=1000范围内的整点,但是搜索所有点还是会超时。首先试验了一下模拟退火算法(Simulated annealing),结果由于对“温度”和随机变化的过程把握不好,导致得到次优解并且波动很大,最终放弃并试了一个不太严谨的方法:先按步长为10搜索所有整点,找到其中的最优点,然后在最优点x和y方向上+-10的范围内提高精度按步长1搜索最优解。

3.Visconsin Square

时限是5秒,简单的DFS就可以过。


模拟退火(Simulated annealing)算法:

以下内容出自heaad的博客

 

一. 爬山算法 ( Hill Climbing )
 
         介绍模拟退火前,先介绍爬山算法。爬山算法是一种简单的贪心搜索算法,该算法每次从当前解的临近解空间中选择一个最优解作为当前解,直到达到一个局部最优解。
 
         爬山算法实现很简单,其主要缺点是会陷入局部最优解,而不一定能搜索到全局最优解。如图1所示:假设C点为当前解,爬山算法搜索到A点这个局部最优解就会停止搜索,因为在A点无论向那个方向小幅度移动都不能得到更优的解。
 
图1
 
 
二. 模拟退火(SA,Simulated Annealing)思想
 
         爬山法是完完全全的贪心法,每次都鼠目寸光的选择一个当前最优解,因此只能搜索到局部的最优值。模拟退火其实也是一种贪心算法,但是它的搜索过程引入了随机因素。模拟退火算法以一定的概率来接受一个比当前解要差的解,因此有可能会跳出这个局部的最优解,达到全局的最优解。以图1为例,模拟退火算法在搜索到局部最优解A后,会以一定的概率接受到E的移动。也许经过几次这样的不是局部最优的移动后会到达D点,于是就跳出了局部最大值A。
 
         模拟退火算法描述:
 
         若J( Y(i+1) )>= J( Y(i) )  (即移动后得到更优解),则总是接受该移动
 
         若J( Y(i+1) )< J( Y(i) )  (即移动后的解比当前解要差),则以一定的概率接受移动,而且这个概率随着时间推移逐渐降低(逐渐降低才能趋向稳定)
 
  这里的“一定的概率”的计算参考了金属冶炼的退火过程,这也是模拟退火算法名称的由来。
 
  根据热力学的原理,在温度为T时,出现能量差为dE的降温的概率为P(dE),表示为:
 
    P(dE) = exp( dE/(kT) )
 
  其中k是一个常数,exp表示自然指数,且dE<0。这条公式说白了就是:温度越高,出现一次能量差为dE的降温的概率就越大;温度越低,则出现降温的概率就越小。又由于dE总是小于0(否则就不叫退火了),因此dE/kT < 0 ,所以P(dE)的函数取值范围是(0,1) 。
 
  随着温度T的降低,P(dE)会逐渐降低。
 
  我们将一次向较差解的移动看做一次温度跳变过程,我们以概率P(dE)来接受这样的移动。
 
  关于爬山算法与模拟退火,有一个有趣的比喻:
 
  爬山算法:兔子朝着比现在高的地方跳去。它找到了不远处的最高山峰。但是这座山不一定是珠穆朗玛峰。这就是爬山算法,它不能保证局部最优值就是全局最优值。
 
  模拟退火:兔子喝醉了。它随机地跳了很长时间。这期间,它可能走向高处,也可能踏入平地。但是,它渐渐清醒了并朝最高方向跳去。这就是模拟退火。
 
    下面给出模拟退火的伪代码表示。
/*
 *  J(y):在状态y时的评价函数值
 *  Y(i):表示当前状态
 *  Y(i+1):表示新的状态
 *  r: 用于控制降温的快慢
 *  T: 系统的温度,系统初始应该要处于一个高温的状态
 *  T_min :温度的下限,若温度T达到T_min,则停止搜索
*/
while( T > T_min )
{
  dE = J( Y(i+1) ) - J( Y(i) ) ;  

  if ( dE >= 0 )  //表达移动后得到更优解,则总是接受移动
        Y(i+1) = Y(i) ;  //接受从Y(i)到Y(i+1)的移动
  else
  {
    // 函数exp( dE/T )的取值范围是(0,1) ,dE/T越大,则exp( dE/T )也
        if ( exp( dE/T ) > random( 0 , 1 ) )
            Y(i+1) = Y(i) ;  //接受从Y(i)到Y(i+1)的移动
  }
  T = r * T ;  //降温退火 ,0<r<1 。r越大,降温越慢;r越小,降温越快
  /*
  * 若r过大,则搜索到全局最优解的可能会较高,但搜索的过程也就较长。若r过小,则搜索的过程会很快,但最终可能会达到一个局部最优值
  */
  i ++ ;
}

四. 使用模拟退火算法解决旅行商问题

 
  旅行商问题 ( TSP , Traveling Salesman Problem ) :有N个城市,要求从其中某个问题出发,唯一遍历所有城市,再回到出发的城市,求最短的路线。
 
  旅行商问题属于所谓的NP完全问题,精确的解决TSP只能通过穷举所有的路径组合,其时间复杂度是O(N!) 。
 
  使用模拟退火算法可以比较快的求出TSP的一条近似最优路径。(使用遗传算法也是可以的,我将在下一篇文章中介绍)模拟退火解决TSP的思路:
 
    1. 产生一条新的遍历路径P(i+1),计算路径P(i+1)的长度L( P(i+1) )
 
    2. 若L(P(i+1)) < L(P(i)),则接受P(i+1)为新的路径,否则以模拟退火的那个概率接受P(i+1) ,然后降温
 
    3. 重复步骤1,2直到满足退出条件
 
  产生新的遍历路径的方法有很多,下面列举其中3种:
 
    1. 随机选择2个节点,交换路径中的这2个节点的顺序。
 
    2. 随机选择2个节点,将路径中这2个节点间的节点顺序逆转。
 
    3. 随机选择3个节点m,n,k,然后将节点m与n间的节点移位到节点k后面。
 
 
五. 算法评价
 
        模拟退火算法是一种随机算法,并不一定能找到全局的最优解,可以比较快的找到问题的近似最优解。 如果参数设置得当,模拟退火算法搜索效率比穷举法要高。
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
1
12
2013
4

TopCoder SRM 565

题目来自TopCoder

MonstersValley

Problem Statement:  
Manao is traversing a valley inhabited by monsters. During his journey, he will encounter several monsters one by one. The scariness of each monster is a positive integer. Some monsters may be scarier than others. The i-th (0-based index) monster Manao will meet has scariness equal to dread[i].  Manao is not going to fight the monsters. Instead, he will bribe some of them and make them join him. To bribe the i-th monster, Manao needs price[i] gold coins. The monsters are not too greedy, therefore each value in price will be either 1 or 2.  At the beginning, Manao travels alone. Each time he meets a monster, he first has the option to bribe it, and then the monster may decide to attack him. A monster will attack Manao if and only if he did not bribe it and its scariness is strictly greater than the total scariness of all monsters in Manao's party. In other words, whenever Manao encounters a monster that would attack him, he has to bribe it. If he encounters a monster that would not attack him, he may either bribe it, or simply walk past the monster
You are given the vector <int>s dread and price. Compute the minimum price Manao will pay to safely pass the valley.
 
用dp[p][n]表示Manao用p个金币通过前n个monsters可以获得的最大scariness值,则有
dp[n][p]=max{dp[n-1][p](dp[n-1][p]>scariness[n]), dp[n-1][p-price[n]]+scariness[n]}
如果用p个金币不能通过前n个monsters那么dp[n][p]=-1
#include<vector>
#include<iostream>
using namespace std;
class MonstersValley2{
	public:
	int minimumPrice(vector <int> dread,vector <int> price){
		long long dp[21][41];
		for(int i=0;i<=40;i++)dp[0][i]=0;
		for(int i=1;i<=dread.size();i++){
			for(int j=0;j<=40;j++){
				dp[i][j]=-1;
				if(dp[i-1][j]>=dread[i-1])dp[i][j]=dp[i-1][j];
				if(j>=price[i-1]&&dp[i-1][j-price[i-1]]!=-1){
					long long t=dp[i-1][j-price[i-1]]+dread[i-1];
					if(t>dp[i][j])dp[i][j]=t;
				}
				cout<<dp[i][j]<<' ';
			}
			cout<<endl;
		}
		for(int j=0;j<=40;j++){
			if(dp[dread.size()][j]!=-1)return j;
		}
		return -1;
	}
};

DivisibleSequence

Problem Statement:
A divisible sequence with starting value N and length H is a sequence of positive integers with the following properties:
The sequence has H elements, labeled A[0] through A[H-1].
A[0] equals N.
For each i between 0 and H-2, inclusive, A[i+1] divides A[i].
You are given the ints N and H. Let C be the count of all divisible sequences with starting value N and length H. Return the value (C modulo 1,000,000,009).
 
首先对N进行因数分解,比如12=22*3
这样一步步生成下一个元素的过程中,只要各个因子对应的指数不增,则最终形成一个Divisible Sequence。
注意到各个因子的指数变化是相互独立的,所以这个问题就转化为求一个以正整数开头的单调不增序列的个数。
比如12的例子,因子2对应的指数为2,生成下一个元素的过程中指数2或者不变,或者减小。

生成单调序列的方法:

设序列头元素为整数x(x>0),要生成长度为H的单调不增序列。
这个过程可以表示为x次减1操作和H-1次取数操作的组合。
如x=2,H=3的情况下,操作序列:减1、取数、减1、取数、取数 形成一个单调不增序列1,0,0
所以这样的序列共有C(H-1+c,c)个。
 
这样一来整个算法流程可以概括为:
1.因数分解,提取各个质因数的指数
2.计算各自的单调序列个数
3.以上单调序列的个数相乘得到答案

质因数分解的试除法:

官方题解提供的代码
    // Let us use trial divison to find prime factors p
    for (int p=2; p <= N/p; p++) {
        int c = 0;
        while (N % p == 0) {
            N /= p;	
            c++;
        }
        res = ( res * C(H-1+c, c) ) % MOD;
    }
    if (N > 1) {
        // N is one last prime factor, c = 1
        // C(H-1+1,1) = H
        res = ( res * H ) % MOD;
    }

注意大于sqrt(N)的质因子最多只有一个,所以最后只要检测N是否大于1,如果大于1的话N就是最后的质因数。


计算C(n,k)的方法:

官方题解提供的代码

final int MOD = 1000000009;
// Calculates x raised to the y-th power modulo MOD
long modPow(long x, long y)
{
    long r=1, a=x;
    while (y > 0) {
        if ( (y&1)==1 ) {
            r = (r * a) % MOD;
        }
        a = (a * a) % MOD;
        y /= 2;
    }
    return r;
}
// Modular multiplicative inverse through Fermat's little theorem:
long modInverse(long x)
{
    return modPow(x, MOD-2);
}
// Modular division x / y, find modular multiplicative inverse of y
// and multiply by x.
long modDivision(long p, long q)
{
    return (p * modInverse(q)) % MOD;
}
// Binomial coifficient C(n,k) in O(k) time.
long C(long n, int k)
{
    if (k > n) {
        return 0;
    }
    long p = 1, q = 1;
    for (int i=1; i<=k; i++) {
        q = ( q * i) % MOD;
        p = ( p * (n - i + 1) ) % MOD;
    }
    return modDivision( p, q);
}

这里出现了modular divisionModular multiplicative inverse(模反元素)的概念 和 费马小定理(Fermat's little theorem)

如果a-1和x对于m是同模的,则x就是a的模反元素,这时有a*x mod m = a*a-1 mod m = 1

模反元素可以用于计算modular division,p/q mod m 可以表示为 p*q-1 mod m,其中q-1是q的模反元素。

而费马小定理可以方便地解决m为素数时求解q-1的问题:

假如a是一个整数,p是一个质数,这个定理表明amod p = a mod p

如果a不是p的倍数,这个定理可以写成ap-1mod p = 1。

如此以来,对于本题我们只要计算ap-2即为a的模反元素。


怎样计算xy :

观察官方题解的modPow函数,这个方法巧妙地用o(log(y))的时间计算了x

代码维护了底数a和结果r。初始时r=1,a=x,每一步迭代中如果y是奇数,则r更新为r*a。然后底数a更新为a2,指数y更新为y/2。

这是因为xy=(x2)y/2*xy%2 ,这样计算xy就转化为计算(x2)y/2

1
10
2013
1

USACO 4.4

 1.Shuttle Puzzle
本来以为是个什么神搜索,原来是个找规律构造题。本来枚举量不算大,不过我没想到判重的好方法,不判重又怕超时,所以还是copy了题解的规律。就这样水过了。

2.Pollutant Control
求边数最少的最小割,如果有多个方案,输出边的输入顺序最小的那个。把每个边的容量Ci修改为Ci*1001+1,因为边数最大为1000,这样求出最大流tot之后tot/1001就是原图的最小割,tot mod 1001就是最小割的边数,这样同时保证了最小割有最小边数。然后按输入顺序枚举每条边,如果移除这条边(Ci)后求得的最大流totnew满足tot-totnew=Ci,则输出这条边,然后在此基础上继续枚举删边求最大流的过程。

3.Frame Up
全拓扑排序。

allTopSort(G){
    if(图G为空){
        输出缓存
        return
    }
    for(G中每个入度为零结点v){
        将v放入缓存
        从G中删除v及其出边,更新相连的结点的入度
        allTopSort(G)
        恢复v及其出边
    }
}
Category: USACO | Tags: usaco
1
10
2013
0

USACO 4.3

1.Buy Low, Buy Lower
动态规划
求最长下降子序列的长度和个数。
设序列为a[0]...a[n]
用dp[i]表示以a[i]为结尾的最长下降子序列的长度,则
dp[i]=max{dp[j]+1}(j<i and a[j]>a[i])
用total[i]表示以a[i]结尾的互不相同的最长下降子序列的个数,则
total[i]=∑total[j] (j<i and a[j]>a[j] and dp[j]+1==dp[i]) 
注意去重,如果有多个符合条件的j对应相同的a[j]我们只加入最大的j对应的total[j],
这是因为如果u>v且a[u]==a[v],以a[v]为结尾的所有序列都包含于以a[u]为结尾的序列集合。
 
2. The Primes
搜索+剪枝
对五位且数位和一定的素数打表。
先放对角线上的两个素数,注意左上角数字是确定的,左下角是一个素数的结尾所以必须为奇数。
然后顺序枚举第1、2、3行上的素数,之后第4、5行可以算出来,记录可行解。
排序输出。
由于每列都是素数,可以打表记录下一些素数“特征”是否存在,用于剪枝,比如有素数为11351,就记 head[1]=head[11]=head[113]=head[1135]=true,如果枚举过程中出现了一些“特征”不存在(对应的head值为 false)就剪枝。另外一定要充分利用已经放置的数的信息进行提前剪枝,比如枚举到第三行素数,可以得到第二列和第四列的前四位数,即可比对这些“特 征”是否存在。
 
3.Street Race
枚举去掉的路口,如果去掉后从起点floodfill不能到达终点,则此路口unavoidable。
如果从一个unavoidable的路口出发不能回到起点可达的路口,则此路口是splitting point。
 
4.Letter Game
首先注意到由于目标词长度最大为7,那么最多只有两个字母组合,且只有长度为3或4的单词可以组合在一起。
对长度3或4的单词单独打表,加上一些简单的剪枝,枚举两单词组合的情况不会超时。

 

Category: USACO | Tags: usaco
1
10
2013
0

USACO 4.2

1.Drainage Ditches
基础的最大流问题,基本步骤是找增广路径,更新残余网络,直到搜索不到增广路径。

2.The Perfect Stall
最大二分匹配,可以使用hungary算法

bool find(int c){
    for(int j=0;j<M;j++){
        if(!graph[c][j]||inpath[j])continue;//如果没有边相连或者j被搜索过则略过
        inpath[j]=true;//把j放到增广路径上,true表示j已经被搜索过
        if(pair[j]==-1||find(pair[j])){
            pair[j]=c;//更新匹配关系
            return true;
        }
        //inpath[j]=false; 加上这句会超时,其实这里并不用回溯
        //因为inpath标识了从j出发能否找到增广路径
    }
    return false;
}
void hungary(){
    for(int i=0;i<N;i++){
        //i必定还没有被匹配
        for(int j=0;j<M;j++)inpath[j]=false;
        if(find(i))match++;
    }
}

3.Job Processing
各种借鉴。
用贪心算法分别求出A机器集合和B机器集合单阶段各自的最优解。为求得整体最优解,把A机器处理的工件的时间线和B机器处理的工件的时间线相接。
此图来自usaco官方题解:


4.Cowcycles
枚举量看似很大,实际上可以水过,注意防止重复计算。
算方差可以用公式D[X]=E[X^2]-E[X]^2
枚举代码有些臃肿:

//用pick(F1,F2,R1,R2,F,R)表示在[F1,F2]区间选取F个数,在[R1,R2]区间选取R个数
void pick(int f1,int f2,int r1,int r2,int f,int r){
    int i,j;
    if(f==0&&r==0){
        calcul();
        return;
    }
    if(f*r!=0){
        for(i=f1;i<=f2-f+1;i++){
            for(j=r1;j<=r2-r+1;j++){
                seqf[F-f]=i;
                seqr[R-r]=j;
                pick(i+1,f2,j+1,r2,f-1,r-1);
            }
        }
    }
    else if(f==0){
        for(j=r1;j<=r2-r+1;j++){
            seqr[R-r]=j;
            pick(f1,f2,j+1,r2,f,r-1);
        }
    }
    else if(r==0){
        for(i=f1;i<=f2-f+1;i++){
            seqf[F-f]=i;
            pick(i+1,f2,r1,r2,f-1,r);
        }
    }
    return;
}
Category: USACO | Tags: usaco
1
10
2013
0

USACO 4.1

Beef McNuggets
数论,动态规划。

确定一个数c能否由a1,a2,a3...,an线性组合而成等效于求一次不定方程
a1*x1+a2*x2+...+an*xn=c ( x1,x2,...,xn>=0 ) (方程A)
是否有解。

裴蜀定理:裴蜀等式ax+by=m(a,b>0)有解当且仅当m是a b的最大公约数gcd(a,b)的倍数。

若gcd(a,b)>1 则方程对于无限个m无解。

若gcd(a,b)=1。当a,b>1时,如果要求x y非负数,m取某些值时无解,比如m=1时。那么是否只有有限个m无解呢?答案是肯定的:
设a,b互质,x0,y0是此方程的特解。其通解可以表示为x=x0+[b/gcd(a,b)]*t , y=y0+[a/gcd(a,b)]*t (t为任意整数)。
令x>=0且y>=0,得-x0/b<=t<=y/a,显然y0/a+x0/b>1时存在t满足此不等式,也即方程有解。令y0/a+x0/b>1,有a*x0+b*y0=m>ab。
可见当m大于ab时方程必有解。

把此结论推广到方程A可知:当gcd(a1,a2,...,an)=1时,有有限个c无解,也就是说我们可以求出一个最大的不能被表示的c;否则有无限个c无解。

确定一个数能否被表示出来可以简单地应用动态规划,如果连续出现256个数字可以被表示出来,那么之后所有数字均可以被表示出来,此时可以停止动态规划。

USACO题解中第二种方法认为数据中必有两个数字互质,所以可以只搜索256*256-2*256以内的数字能否被表示(因为“Given two relatively prime numbers N and M, the largest number that you cannot make is NM - M - N”)。

这个方法对于USACO的数据是正确的,因为这些数据只要有一个大于零的解,都有其中两个数字互质的情况出现。但是显然某些情况下n个数互质,但是两两不互质,比如21 35 15。所以我认为这个方法是有漏洞的。


Fence Rails

搜索,需要精心剪枝。

迭代加深搜索(IDDFS: Iterative deepening depth-first search):控制搜索层数的DFS,即首先允许深度优先搜索K层搜索树,若没有发现可行解,再加大搜索层数(如K+1)重复搜索。

“一般来说,如果目标结点离根结点远,需要遍历整棵树,可以考虑使用深度优先搜索;如果目标离根结点近,或求最小步数,则考虑广度优先搜索或迭代加深搜索;若广度优先搜索存在空间不够的问题,则考虑使用迭代加深搜索。”(本段来源于:http://wenku.baidu.com/view/ce41b54669eae009581bec4a.html)

首先对rails从小到大进行排序,确定所给的boards能否切出n个rails实际上等效于确定所给的boards能否切出排序后的前n个rails。为此我们使用IDDFS,先确定一个搜索深度(即上述的n),再深搜每个rail对应的board,搜索到第一个可行的切割方式时就返回。

搜索时先搜索较大的rail,这样可以让搜索树比较“瘦”(相对于先搜索较小的rail),也就是说树基部的分支较少。

显然这时搜索的时间很大程度上取决于搜到可行解之前我们探索了多少失败的方式,如果能剪掉这些“失败树枝”就好了。所以我们需要提前预知必然的失败并且果断停止在这些“失败树枝”上继续搜索。设boards总长度是B,rails总长度是R,搜索过程中的边角料(不能再切出其他rail)的总长度为W,当B-R<W时可以剪枝。

board最大长度是128,但是rail总数会达到1023之多,可见长度重复的rails有很多,要避免因此产生的重复搜索,比如rail1=rail2=5,那么搜索rail2时只需从rail1对应的board开始。


Fence Loops
水搜索,简单地使用dfs就可以秒杀所有数据。实际上求图中最小环的正统方法是枚举环上的一条边然后求最短路径,不过输入数据是边的邻接信息,而最短路径算法是基于结点的邻接矩阵或者邻接表的,需要重构一下。构造邻接矩阵时可以保留所有篱笆的端点(即使它们会重合),这样最多200个结点,发现两结点重合就将距离置为0。


Cryptcowgraphy
搜索加剪枝。
两个剪枝:
(1)判断C O W字符之间的片段是否是明文的子串,不是就剪枝。
(2)判断第一个和最后一个加密用字符是不是分别为C和W,不是就剪枝。
防止重复搜索相同状态:
如果搜索到的C O W,它的CO或者OW区间内包含上次使用的C O W,就剪枝;比如本次搜索到的COW用红色表示,上次使用的COW用黑色表示,以下情况需要剪枝:CCOWOWCOCOWW 。这是因为先应用黑色COW和先应用红色COW效果相同。
使用以上剪枝还不够,为了配合第2个剪枝,我将搜索顺序设定为先搜索跨度较大的COW再搜索跨度较小的COW。至此AC。
 

Category: USACO | Tags: usaco
1
8
2013
0

USACO 3.4

 

Closed Fences
计算几何,借鉴了很多,写的很辛苦,因为需要考虑不少特殊情况。用二分法分割篱笆来探索每个篱笆能不能被看到,注意要设置一个分段长度的下限。在判断线段是否相交时有一些技巧。借鉴了一下混合积的方法,发现不能处理两个线段共线的情况。我判断共线和共线情况下线段是否相交的方法如下:设坐标系oxyz中两个线段ab和cd处于oxy平面上,若ab×ac与ab×ad的z分量都为0则abcd共线。共线的情况下,若ac*ad>0且bc*bd>0,则两线段不相交,否则相交。
/*
ID: huilong1
LANG: C++
TASK: fence4
*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#define ESP 1e-4
FILE *fin,*fout;

typedef struct{
	double x;
	double y;
}Point;

typedef struct{
	Point a;
	Point b;
}Seg;

typedef struct{
	double x;
	double y;
	double z;
}Vector;

int N;
Seg fence[201];
Point obser;
Point corner[201];
Seg visible[201];
int numsee;

//判断是否是相邻的篱笆 
bool neighbor(int i,int j){
	if(i==0&&j==N-1||i==N-1&&j==0)return true;
	int abs=i-j;
	if(abs<0)abs=-abs;
	if(abs==1)return true;
	return false;
}

//获得中点
Point getmiddle(Point a,Point b){
	Point m;
	m.x=(a.x+b.x)/2;
	m.y=(a.y+b.y)/2;
	return m;
}

//算点积
double dotpro(Vector a,Vector b){
	return a.x*b.x+a.y*b.y+a.z*b.z;
}

//算叉积
Vector crosspro(Vector a,Vector b){
	Vector t;
	t.x=a.y*b.z-a.z*b.y;
	t.y=a.z*b.x-a.x*b.z;
	t.z=a.x*b.y-a.y*b.x;
	return t;
} 

//获得两点决定的向量
Vector getvector(Point a,Point b){
	Vector t;
	t.x=b.x-a.x;
	t.y=b.y-a.y;
	t.z=0;
	return t;
} 

//判断线段是否交叉
bool cross(Seg p,Seg q){
	Vector pv=getvector(p.a,p.b);
	Vector qv=getvector(q.a,q.b);
	Vector paqa=getvector(p.a,q.a);
	Vector paqb=getvector(p.a,q.b);
	Vector pbqa=getvector(p.b,q.a);
	Vector pbqb=getvector(p.b,q.b);
	Vector qapa=getvector(q.a,p.a);
	Vector qapb=getvector(q.a,p.b);
	//判断两线段是否共线
	Vector v1=crosspro(pv,paqa);
	Vector v2=crosspro(pv,paqb);
	if(v1.z==v2.z&&v1.z==0){//共线 
		if(dotpro(paqa,paqb)>0&&dotpro(pbqa,pbqb)>0)return false;
		else return true;
	}
	//不共线 
	if(dotpro(v1,v2)>0)return false;
	if(dotpro(crosspro(qv,qapa),crosspro(qv,qapb))>0)return false;
	return true;
} 

//判断是否能看到点
int seepoint(Point p,int ind){
	Seg view;
	view.a=obser;
	view.b=p;
	for(int i=0;i<N;i++){
		if(i==ind)continue;
		if(cross(view,fence[i]))return i;
	}
	return -1;
} 

//判断线段是否收缩为一个点 
bool ispoint(Seg s){
	Point a=s.a;
	Point b=s.b;
	if((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)<ESP)return true;
	return false;
}

//判断是否可见 
bool cansee(Seg s,int ind){
	if(ispoint(s))return false;
	Point a=s.a;
	Point b=s.b;
	Point mid=getmiddle(a,b);
	int sta,stb,stmid;
	sta=seepoint(a,ind);
	stb=seepoint(b,ind);
	stmid=seepoint(mid,ind);
	if(sta==stb&&sta>=0)return false;
	if(sta<0||stb<0||stmid<0)return true;
	Seg t;
	t.a=a,t.b=mid;
	if(cansee(t,ind))return true;
	t.a=mid,t.b=b;
	if(cansee(t,ind))return true;
	return false;
} 

int main(){
	fin=fopen("fence4.in","r");
	fout=fopen("fence4.out","w");
	fscanf(fin,"%d",&N);
	fscanf(fin,"%lf %lf",&obser.x,&obser.y);
	for(int i=0;i<N;i++){
		fscanf(fin,"%lf %lf",&corner[i].x,&corner[i].y);
	}
	
	//按顺序生成篱笆 
	for(int i=0;i<=N-2;i++){
		fence[i].a=corner[i];
		fence[i].b=corner[i+1];
	}
	fence[N-1].a=corner[0];
	fence[N-1].b=corner[N-1];
	
	//判断篱笆合法性 (不相邻的不能相交) 
	for(int i=0;i<N;i++){
		for(int j=0;j<i-1;j++){
			if(!neighbor(i,j)&&cross(fence[i],fence[j])){
				//printf("%d %d\n",i,j);
				//system("pause");
				fprintf(fout,"NOFENCE\n");
				return 0;
			}
		}
	}
	
	//为了按顺序输出 调整篱笆顺序 
	Seg t=fence[N-1];
	fence[N-1]=fence[N-2];
	fence[N-2]=t;
	
	for(int i=0;i<N;i++){
		if(cansee(fence[i],i)){
			visible[numsee]=fence[i];
			numsee++;
		}
	}
	fprintf(fout,"%d\n",numsee);
	for(int i=0;i<numsee;i++){
		fprintf(fout,"%.0lf %.0lf %.0lf %.0lf\n",visible[i].a.x,visible[i].a.y,visible[i].b.x,visible[i].b.y);
	}
	//system("pause");
	return 0;
}
 
American Heritage
给一个二叉树的前序遍历和中序遍历,求后序遍历。设计一个递归式,不用重构整个二叉树。
/*
ID: huilong1
LANG: C++
TASK: heritage
*/
#include<stdio.h>
#include<stdlib.h>

FILE *fin,*fout;
char in[26];
int hashin[26];
char pre[26];
int num;

void showpost(int prebeg,int preend,int inbeg,int inend){
	if(prebeg>preend)return;
	showpost(prebeg+1,
	         prebeg+hashin[pre[prebeg]-'A']-inbeg,
			 inbeg,
			 hashin[pre[prebeg]-'A']-1);
	showpost(prebeg+hashin[pre[prebeg]-'A']-inbeg+1,
             prebeg+hashin[pre[prebeg]-'A']-inbeg+inend-hashin[pre[prebeg]-'A'],
			 hashin[pre[prebeg]-'A']+1,
			 inend);
	fprintf(fout,"%c",pre[prebeg]);
	return;
}

int main(){
	fin=fopen("heritage.in","r");
	fout=fopen("heritage.out","w");
	while(fscanf(fin,"%c",&in[num])&&in[num]!='\n'){
		hashin[in[num]-'A']=num;
		num++;
	}
	int i;
	for(i=0;i<num;i++)fscanf(fin,"%c",&pre[i]);
	showpost(0,num-1,0,num-1);
	fprintf(fout,"\n");
	//system("pause");
	return 0;
}
 
Electric Fence
用解析法解决了,后来发现可以简单地使用皮克公式。
 
Raucous Rockers
背包问题的变形,我加了一维用来表示唱片的数量。用dp[m][t][n]表示前m张唱片,最后一张唱片剩下t分钟,从前n首歌中选歌时可以选的最大歌曲数。则有:
dp[m][t][n]=max{ dp[m][t][n-1],
                 dp[m][t-time(n)][n-1]+1 ( if time(n)<=t ),
                 dp[m-1][T-time(n)][n-1]+1 ( if time(n)>t )
            }

皮克公式(Pick's theorem)

给定顶点座标均是整点(或正方形格点)的简单多边形,皮克定理说明了其面积A和内部格点数目i、边上格点数目b的关系:A = i + b/2 - 1。

 

观察线段的二分法

以下出自(http://www.cppblog.com/jericho/archive/2011/01/19/138837.html)

设观察点为O,取线段AB,及AB中点C,连结AO、BO、CO;
1. 枚举所有线段,若AO、BO与同一条线段相交,则AB看不到。
2. 若A、B很近,则AB可以看成质点,AB看不到。
3. 枚举所有线段,分别与AO、BO、CO求是否相交,循环结束后判断是否有一条线段(AO、BO、CO)没有相交,如果有,则AB可以看见。
4. 二分AB, 对AC、BC进行上述过程。
Category: USACO | Tags: usaco

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