24h購物| | PChome| 登入
2013-05-03 22:42:12| 人氣1,160| 回應0 | 上一篇 | 下一篇

[UVA][極角排序] 1432 - Fire-Control System

推薦 0 收藏 0 轉貼0 訂閱站台


A new mighty weapon has just been developed, which is so powerful that it can attack a sector of indefinite size, as long as the center of the circle containing the sector is the location of the weapon. We are interested in developing a fire-control system that calculates firing-solutions automatically.


The following example gives an example of a firing solution:

epsfbox{p4356.eps}

Here the firing region is the sector $ overline{{ABC}}$ that covers six points: A, B, C, D, E, H.

You may further assume that the weapon is always located at point (0, 0), no targets will be on the point (0, 0) and the coordinates of the targets will be distinct.


A firing solution is called effective if and only if it covers a minimum of K points out of N given points (targets) on the two-dimensional Cartesian plane. Furthermore, since the cost of a particular fire solution is in direct proportion to the size of the area it covers, a firing could be quite costly; thus we are only interested in the optimal firing solution with the minimum cost.

Input 

There are multiple test cases in the input file.

Each test case starts with two non-negative integers, N and K (1$ le$N$ le$5000, K$ le$N) , followed by N lines each containing two integers, X , and Y , describing the distinct location of one target. It is guaranteed that the absolute value of any integer does not exceed 1000.

Two successive test cases are separated by a blank line. A case with N = 0 and K = 0 indicates the end of the input file, and should not be processed by your program.

Output 

For each test case, please print the required size (to two decimal places), in the format as indicated in the sample output.

Sample Input 

3 1 
0 1 
1 0 
-5 -6 

3 2 
0 2 
2 0 
-5 -6 

0 0

Sample Output 

Case #1: 0.00 
Case #2: 3.14


1. 把所有點與原點拉距離,然後排序過一次,這樣可以知道所有張開扇形面積的半徑。
     因此重複的可以不用做。
2.  把所有點根據極角排序,然後利用掃描線的方式去維護一個最小區間的扇形。

計算極角的方式使用 atan2(y, x) 得到,
因此如果跨足於 [-pi, pi] 之間的話,要特別考慮一下範圍,
同時要注意到區間長度不可超過 2*pi, 否則是無效的。


#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
struct Pt {
int x, y;
int dist;
double theta;
bool operator<(const Pt &a) const {
if(theta != a.theta)
return theta < a.theta;
return dist > a.dist;
}
};
Pt D[5005];
int mmR[5005];
const double pi = acos(-1);
int main() {
/*freopen("in.txt", "r+t", stdin);
freopen("out1.txt", "w+t", stdout);*/
int N, K;
int cases = 0;
int i, j, k;
while(scanf("%d %d", &N, &K) == 2) {
if(N == 0 && K == 0)
break;
for(i = 0; i < N; i++) {
scanf("%d %d", &D[i].x, &D[i].y);
mmR[i] = D[i].x*D[i].x + D[i].y*D[i].y;
D[i].dist = mmR[i];
D[i].theta = atan2(D[i].y, D[i].x);
}
if(K == 0 || N == 0) {
printf("Case #%d: 0.00\n", ++cases);
continue;
}
sort(mmR, mmR+N); // sort maybe radius
sort(D, D+N); // sort by theta
double ret = 1e+50;
double area, theta, theta1, theta2;
/*for(i = 0; i < N; i++) {
printf("%lf\n", D[i].theta);
}*/
int radius = -1;
for(i = K-1; i < N; i++) {
if(radius == mmR[i]) continue;
radius = mmR[i];
// sweep line algorithm
int tmpK = 0, pre = 0;
int idx1 = 0, idx2 = 0, j1, j2;
int N2 = N*2;
for(j1 = 0, j2 = 0; j1 < N2; j1++) {
if(j1 >= N && j2 >= N) break;
if(D[idx1].dist <= radius)
tmpK++;
while(tmpK >= K && j2 < j1) {
if(tmpK > K && D[idx2].dist <= radius)
tmpK--, idx2++, j2++;
else if(D[idx2].dist > radius)
idx2++, j2++;
else
break;
if(idx2 >= N) idx2 = 0;
}
if(tmpK >= K && j1-j2 < N) {
theta1 = D[idx1].theta;
theta2 = D[idx2].theta;
if(j1 >= N) theta1 += 2*pi;
if(j2 >= N) theta2 += 2*pi;
else if(theta2 <= theta1)
theta = theta1 - theta2;
else
theta = 2*pi - (theta1 - theta2);
//printf("%lf %lf %lf %d %d %d %d %d\n", theta, theta1, theta2, tmpK, j1, j2, idx1, idx2);
area = 0.5*radius*theta;
ret = min(ret, area);
}
idx1++;
if(idx1 >= N) idx1 = 0;
}
}
printf("Case #%d: %.2lf\n", ++cases, ret);
}
return 0;
}
 

台長: Morris
人氣(1,160) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][dp] 10029 - Edit Step Ladders
此分類上一篇:[UVA][大數] 10433 - Automorphic Numbers

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文