Time limit: 3.000 seconds

## Background

Some problems are difficult to solve but have a simplification that is easy to solve. Rather than deal with the difficulties of constructing a model of the Earth (a somewhat oblate spheroid), consider a pre-Columbian flat world that is a 500 kilometer × 500 kilometer square.

In the model used in this problem, the flat world consists of several warring kingdoms. Though warlike, the people of the world are strict isolationists; each kingdom is surrounded by a high (but thin) wall designed to both protect the kingdom and to isolate it. To avoid fights for power, each kingdom has its own electric power plant.

When the urge to fight becomes too great, the people of a kingdom often launch missiles at other kingdoms. Each SCUD missile (Sanitary Cleansing Universal Destroyer) that lands within the walls of a kingdom destroys that kingdom's power plant (without loss of life).

## The Problem

Given coordinate locations of several kingdoms (by specifying the locations of houses and the location of the power plant in a kingdom) and missile landings you are to write a program that determines the total area of all kingdoms that are without power after an exchange of missile fire.

In the simple world of this problem kingdoms do not overlap. Furthermore, the walls surrounding each kingdom are considered to be of zero thickness. The wall surrounding a kingdom is the minimal-perimeter wall that completely surrounds all the houses and the power station that comprise a kingdom; the area of a kingdom is the area enclosed by the minimal-perimeter thin wall.

There is exactly one power station per kingdom.

There may be empty space between kingdoms.

## The Input

The input is a sequence of kingdom specifications followed by a sequence of missile landing locations.

A kingdom is specified by a number N ( 3 ≤ N ≤ 100 ) on a single line which indicates the number of sites in this kingdom. The next line contains the x and y coordinates of the power station, followed by N-1 lines of x, y pairs indicating the locations of homes served by this power station. A value of -1 for N indicates that there are no more kingdoms. There will be at least one kingdom in the data set.

Following the last kingdom specification will be the coordinates of one or more missile attacks, indicating the location of a missile landing. Each missile location is on a line by itself. You are to process missile attacks until you reach the end of the file.

Locations are specified in kilometers using coordinates on a 500 km by 500 km grid. All coordinates will be integers between 0 and 500 inclusive. Coordinates are specified as a pair of integers separated by white-space on a single line. The input file will consist of up to 20 kingdoms, followed by any number of missile attacks.

## The Output

The output consists of a single number representing the total area of all kingdoms without electricity after all missile attacks have been processed. The number should be printed with (and correct to) two decimal places.

## Sample Input

12

3 3

4 6

4 11

4 8

10 6

5 7

6 6

6 3

7 9

10 4

10 9

1 7

5

20 20

20 40

40 20

40 40

30 30

3

10 10

21 10

21 13

-1

5 5

20 12

## Sample Output

70.50

## Hint

You may or may not find the following formula useful.

Given a polygon described by the vertices v0, v1, ..., vn such that v0 = vn, the signed area of the polygon is given by

where the x, y coordinates of vi = (xi, yi); the edges of the polygon are from vi to vi + 1 for i = 0 ... n - 1 .

If the points describing the polygon are given in a counterclockwise direction, the value of a will be positive, and if the points of the polygon are listed in a clockwise direction, the value of a will be negative.

## Analysis

计算几何类型的题目。需要用到三个基本算法，一是求凸包，二是判断点在多边形内，三是求多边形面积（题目中已给出）。关于凸包算法请详见Graham's Scan法。判断点在多边形内的算法有很多种，这里用到的是外积法：设待判断的点为p，逆时针或顺时针遍例多边形的每个点v_{n}，将两个向量<p, v_{n}>和<v_{n}, v_{n + 1}>做外积。如果对于多边形上所有的点，外积的符号都相同（顺时针为负，逆时针为正），则可断定p在多边形内。外积出现0，则表示p在边上，否则在多边形外。

算法的思路很直接，实现也很简单，关键是这道题的测试数据太扯蛋了，让我郁闷了很久。题目中并未说明导弹打在墙上怎么办，只是说“... whithin the wall ...”。根据测试结果来看，打在墙上和打在据点上都要算打中。题目中还提到国家不会互相重叠“... kingdoms do not overlap.”，但测试表明数据里确有重叠的情况，因此在导弹击中后一定要跳出循环，否则会出现一弹多击的情况。

## Solution

#include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <vector> #include <math.h> using namespace std; struct POINT { int x; int y; bool operator==(const POINT &other) { return (x == other.x && y == other.y); } } ptBase; typedef vector<POINT> PTARRAY; bool CompareAngle(POINT pt1, POINT pt2) { pt1.x -= ptBase.x, pt1.y -= ptBase.y; pt2.x -= ptBase.x, pt2.y -= ptBase.y; return (pt1.x / sqrt((float)(pt1.x * pt1.x + pt1.y * pt1.y)) < pt2.x / sqrt((float)(pt2.x * pt2.x + pt2.y * pt2.y))); } void CalcConvexHull(PTARRAY &vecSrc, PTARRAY &vecCH) { ptBase = vecSrc.back(); sort(vecSrc.begin(), vecSrc.end() - 1, &CompareAngle); vecCH.push_back(ptBase); vecCH.push_back(vecSrc.front()); POINT ptLastVec = { vecCH.back().x - ptBase.x, vecCH.back().y - ptBase.y }; PTARRAY::iterator i = vecSrc.begin(); for (++i; i != vecSrc.end() - 1; ++i) { POINT ptCurVec = { i->x - vecCH.back().x, i->y - vecCH.back().y }; while (ptCurVec.x * ptLastVec.y - ptCurVec.y * ptLastVec.x < 0) { vecCH.pop_back(); ptCurVec.x = i->x - vecCH.back().x; ptCurVec.y = i->y - vecCH.back().y; ptLastVec.x = vecCH.back().x - (vecCH.end() - 2)->x; ptLastVec.y = vecCH.back().y - (vecCH.end() - 2)->y; } vecCH.push_back(*i); ptLastVec = ptCurVec; } vecCH.push_back(vecCH.front()); } int CalcArea(PTARRAY &vecCH) { int nArea = 0; for (PTARRAY::iterator i = vecCH.begin(); i != vecCH.end() - 1; ++i) { nArea += (i + 1)->x * i->y - i->x * (i + 1)->y; } return nArea; } bool PointInConvexHull(POINT pt, PTARRAY &vecCH) { for (PTARRAY::iterator i = vecCH.begin(); i != vecCH.end() - 1; ++i) { int nX1 = pt.x - i->x, nY1 = pt.y - i->y; int nX2 = (i + 1)->x - i->x, nY2 = (i + 1)->y - i->y; if (nX1 * nY2 - nY1 * nX2 < 0) { return false; } } return true; } int main(void) { vector<PTARRAY> vecKingdom; POINT ptIn; int aFlag[100] = {0}, nAreaSum = 0; for (int nPtCnt; cin >> nPtCnt && nPtCnt >= 1;) { PTARRAY vecSrc, vecCH; cin >> ptIn.x >> ptIn.y; vecSrc.push_back(ptIn); for (; --nPtCnt != 0;) { cin >> ptIn.x >> ptIn.y; POINT &ptMin = vecSrc.back(); vecSrc.insert(vecSrc.end() - (ptIn.y > ptMin.y || (ptIn.y == ptMin.y && ptIn.x > ptMin.x)), ptIn); } CalcConvexHull(vecSrc, vecCH); vecKingdom.push_back(vecCH); } while (cin >> ptIn.x >> ptIn.y) { vector<PTARRAY>::iterator i = vecKingdom.begin(); for (int k = 0; i != vecKingdom.end(); ++i, ++k) { if (PointInConvexHull(ptIn, *i) && aFlag[k] != 1) { nAreaSum += CalcArea(*i); aFlag[k] = 1; break; } } } cout << setiosflags(ios::fixed) << setprecision(2); cout << (float)nAreaSum / 2.0f << endl; return 0; }