# 第七届蓝桥杯决赛试题：广场舞

### 题目:



LQ市的市民广场是一个多边形，广场上铺满了大理石的地板砖。

（广场形状的例子参考【图1.png】）

【输入格式】

【输出格式】

【样例输入】
5
3 3
6 4
4 1
1 -1
0 4

【样例输出】
7

【样例说明】

【数据规模与约定】

CPU消耗  < 1000ms

#### 代码老实人的做法，仅供参考

//  广场舞
#include <bits/stdc++.h>
using namespace std;

#define MX 100000001
#define MI -100000001

int n;
class Point {
public:
Point() = default;
Point(long ix, long iy) : x(ix), y(iy) { }
long x, y;

bool operator == (const Point& p)
{
if(p.x==x && p.y==y) return true;
return false;
}

bool operator < (const Point& p) const
{
if(p.x<x) return true;
if(p.x==x&&p.y<y) return true;
return false;
}
};

Point pn[1000];
set<Point> pset;

class Line {
public:
Line(Point ip1, Point ip2) : p1(ip1), p2(ip2) {
kmom = p1.y-p2.y;
kson = p1.x-p2.x;
bson = kson;
bmom = p1.y*kson-kmom*p1.x;
left = min(p1.x,p2.x);
right = max(p1.x,p2.x);
top = max(p1.y,p2.y);
bottom = min(p1.y,p2.y);
}
Line() = default;
Point p1, p2;
long kmom, kson, bmom, bson, left, right, top, bottom;
};

class Square {
public:
Square(long ileft, long iright, long itop, long ibottom) : left(ileft), right(iright), top(itop), bottom(ibottom) {
width = right-left;
height = top-bottom;
square = new Point*[top-bottom+1];
for(int k=0;k<(top-bottom+1); ++k) {
square[k] = new Point[right-left+1];
}
for(long y=top, i=0; y>=bottom; --y,++i) {
for(long x=left,j=0; x<=right; ++x,++j) {
square[i][j] = Point(x,y);
}
}
}
Square() = default;
long left, right, top, bottom;
Point **square;
long width, height;
};

Square xsquare;
Point **square;

Line ln[1000];

void gline()
{
for(int i=0; i<n; ++i) {
Point p1 = pn[i];
Point p2 = pn[(i+1)%n];
ln[i] = Line(p1,p2);
}
}

void init()
{
cin >> n;
long x, y, left=MX, right=MI, bottom=MX, top=MI;
for(int i=0; i<n; ++i) {
cin >> x >> y;
pn[i] = Point(x,y);
if(x>right) right = x;
if(x<left) left = x;
if(y>top) top = y;
if(y<bottom) bottom = y;
}
gline();
xsquare = Square(left,right,top,bottom);
square = xsquare.square;
}

set<int> getIgnore(long y)
{
set<int> tset;
int topsum(0), bottomsum(0);
for(int i=0; i<n; ++i) {
if(ln[i].top==y) {
tset.insert(i);
topsum += 1;
}
if(ln[i].bottom==y) {
tset.insert(i);
bottomsum += 1;
}
}
if(topsum==0||bottomsum==0) return tset;
if(topsum==bottomsum){
for(set<int>::iterator it = tset.begin(); it!=tset.end(); ++it,++it) {
tset.erase(it);
}
return tset;
}

}

bool isInSquare(Point p)
{
Point p1(p.x,p.y);
for(int i=0; i<n; ++i) {
if(p1==pn[i]) return true;
}

double y = p.y*1.0, x = p.x*1.0;
long sumleft(0), sumright(0);
set<int> tset =  getIgnore(y);

for(int i=0; i<n; ++i) {
if(tset.find(i) != tset.end()) continue;
Line xln = ln[i];
double t = (xln.kmom*xln.bson*x + xln.bmom*xln.kson)/(xln.kson*xln.bson);
if(y>xln.top*1.0||y<xln.bottom*1.0)  continue;

if(y>t) {
if(xln.kmom*1.0/xln.kson*1.0<0) {
sumleft += 1;
} else if(xln.kmom*1.0/xln.kson*1.0>0) {
sumright += 1;
}

}
if(y<t) {
if(xln.kmom*1.0/xln.kson*1.0<0) {
sumright += 1;
} else if(xln.kmom*1.0/xln.kson*1.0>0) {
sumleft += 1;
}
}

}
if(sumleft%2 && sumright%2) return true;
return false;
}

void test_square()
{
for(int i=0; i<=xsquare.height; ++i) {
for(int j=0; j<=xsquare.width; ++j) {

if(isInSquare(square[i][j])) {
Point p = square[i][j];
pset.insert(p);
}

}
}
}

bool xjudge(Point p)
{
if(pset.find(p)!=pset.end()) return true;
return false;
}

long res(0);

long doit()
{
for(long i=0; i<xsquare.height; ++i) {
for(long j=0; j<xsquare.width; ++j) {
if(xjudge(square[i][j])&&xjudge(square[i][j+1])&&
xjudge(square[i+1][j])&&xjudge(square[i+1][j+1])) {
res += 1;
}
}
}
return res;
}

int main()
{
init();
test_square();
cout << doit() << endl;

return 0;
} 

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120