2013
11-10

# Watchdog

A company (name withheld) has an office building in the center of Lund. The building has a perfectly square roof with a number of hatches. Because of a series of burglaries where the perpetrators have entered through these hatches, it was decided to use a watchdog to guard the hatches. A particularly vicious but rather stupid breed of dog was chosen, and unfortunately the dog fell off the roof on its third watch.

A new dog has been procured and it has been decided to attach a leash to its collar and attach the other end at some point on the roof. However, if the leash is too short the dog cannot reach all hatches, but if it is too long then the dog will fall off the building again. The leash has hooks at both ends, so no part of it is used to tie knots. The company wants the dog to reach the center of each hatch (the dog can reach exactly as far as the leash could reach if it were lying flat on the roof), but it does not want the leash to extend beyond the edge of the roof (to the edge is OK). They hope that by carefully choosing the length of the leash and where to attach it, the dog will be able to reach all hatches without risking falling off the building. A leash can only be attached at a point with integer coordinates (if the building is 10 by 10 meters, then the south-west corner of the building has coordinates (0, 0) and the north-east corner has coordinates (10, 10)). A leash cannot be attached at a point where there is a hatch.

If there is no place where you can attach a leash, reach all hatches but not reach beyond the edge of the roof, it is impossible to use this breed of dog, and the company will instead use a poodle (which is a less vicious type of dog, but also less prone to falling off buildings).

On the first line of the input is a single positive integer N, telling the number of test cases to follow. Each case starts with one line with two integers S H, where S is even, 2 <= S <= 40,and 1 <= H <= 50. S is the side of the square roof in meters and H is the number of hatches.

The following H lines each contain two integers X and Y. These are the coordinates of the hatches. Hatches will never lie outside the roof or on the roof’s perimeter. No two hatches will occupy the same position

For each test case, output one line containing the coordinates X Y at which to fasten the leash (if there are several possible points, output the one with smallest X, and if there are still several possibilities select the one with smallest Y among those with smallest X) such that a leash of suitable length allows access to all hatches without extending beyond the edge of the roof. If there is no such point, output “poodle” for that test case.

3
10 2
6 6
5 4
20 2
1 1
19 19
10 3
1 1
1 2
1 3

3 6
poodle
2 2

//* @author:
import java.util.*;
import java.math.*;
public class Main {
static int S, H;
static int []h = new int [51];
//static Hatch []p = new Hatch [51];
static int []x = new int [51];
static int []y = new int [51];
static void deal()
{
int i, j, k;
double dist, temp;
for (i = 0; i <= S; i++)
{
k = 1;
for (j = 0; j <= S; j++)
{
dist = -1;
for (k = 0; k < H; k++)
{
if (i == x[k] && j == y[k])
break;
temp = Math.sqrt((double)((i - x[k]) * (i - x[k]) + (j - y[k]) * (j - y[k])));
dist = Math.max(dist, temp);
}
if (k < H || dist > S - i || dist > i || dist > S - j || dist > j)
continue;
System.out.println(i + " " + j);
return;
}
}
System.out.println("poodle");
}

static void input()
{
int N, i;
Scanner cin = new Scanner (System.in);
N = cin.nextInt();
while (N > 0)
{
S = cin.nextInt();
H = cin.nextInt();
for (i = 0; i < H; i++)
{
x[i] = cin.nextInt();
y[i] = cin.nextInt();
}
deal();
N--;
}
}

public static void main (String []args)
{
input();
}
}

1. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮

2. /*
* =====================================================================================
*
* Filename: 1366.cc
*
* Description:
*
* Version: 1.0
* Created: 2014年01月06日 14时52分14秒
* Revision: none
* Compiler: gcc
*
* Author: Wenxian Ni (Hello World~), [email protected]
* Organization: AMS/ICT
*
* =====================================================================================
*/

#include
#include

using namespace std;

int main()
{
stack st;
int n,i,j;
int test;
int a[100001];
int b[100001];
while(cin>>n)
{
for(i=1;i>a[i];
for(i=1;i>b[i];
//st.clear();
while(!st.empty())
st.pop();
i = 1;
j = 1;

while(in)
break;
}
while(!st.empty()&&st.top()==b[j])
{
st.pop();
j++;
}
}
if(st.empty())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}