记录编号 154248 评测结果 AAAAAAAAAA
题目名称 [HAOI 2014]穿越封锁线 最终得分 100
用户昵称 GravatarAsm.Def 是否通过 通过
代码语言 C++ 运行时间 0.002 s
提交时间 2015-03-21 23:20:53 内存使用 0.29 MiB
显示代码纯文本
/***********************************************************************/
/**********************By Asm.Def-Wu Jiaxin*****************************/
/***********************************************************************/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <cmath>
using namespace std;
FILE *in, *out;
#define SetFile(x) ( in = fopen(#x".in", "r"), out = fopen(#x".out", "w") )
#define SetIO(i, o) ( in = i, out = o )
#define getc() fgetc(in)
template<class T>inline void getd(T &x){
	char ch = getc();bool neg = false;
	while(!isdigit(ch) && ch != '-')ch = getc();
	if(ch == '-')ch = getc(), neg = true;
	x = ch - '0';
	while(isdigit(ch = getc()))x = x * 10 - '0' + ch;
	if(neg)x = -x;
}
/***********************************************************************/
const double eps = 1e-7;
const int maxn = 53;
struct Point{int x, y;}point[maxn];
int N, x, ycnt;
double y[maxn];

inline void init(){
	getd(N);
	int i, j;
	for(i = 0;i < N;++i)getd(point[i].x), getd(point[i].y);
	point[N] = point[0];
	getd(x), getd(i);
	Point *p1 = point, *p2 = point + 1;
	for(i = 0;i < N;++i,++p1,++p2){
		if(p1->x == p2->x || (p1->x<x&&p2->x<x) || (p1->x>x&&p2->x>x))continue;
		y[ycnt++] = p1->y + (p2->y-p1->y+0.0) / (p2->x-p1->x) * (x-p1->x);
	}
	sort(y, y + ycnt);
}

inline int cmp(double a, double b){return fabs(b-a)<eps ? 0 : a < b ? -1 : 1;}

inline bool inside(double a, double b){
	//printf("checking %lf, %lf", a, b);
	double mid = b;
	if(fabs(a - b) < eps)return false;
	do{mid = (mid + a) / 2;}while(fabs(mid-int(mid+eps)) < eps);
	Point *p1 = point, *p2 = point + 1;
	bool ans = 0;
	for(int i = 0;i < N;++i,++p1,++p2){
		if(cmp(p1->y, mid) * (cmp(p2->y, mid)) > 0)continue;
		if(p1->x > x && p2->x > x)ans ^= 1;
		else{
			double tmp = p1->x + (p2->x-p1->x+0.0) / (p2->y-p1->y) * (mid-p1->y);
			if(cmp(x, tmp) == -1)ans ^= 1;
		}
	}
	//printf(",and ans = %d\n", ans);
	return ans;
}

inline void work(){
	double ans = 0;
	//printf("ycnt = %d\n", ycnt);
	for(int i = 1;i < ycnt;++i){
		if(fabs(y[i-1]-y[i])<eps)continue;
		if(inside(y[i-1], y[i]))ans += y[i] - y[i-1];
	}
	fprintf(out, "%d\n", int(ans + eps));
}

int main(){

#ifdef DEBUG
	SetIO(fopen("test.txt", "r"), stdout);
#elif !defined ONLINE_JUDGE
	SetFile(ha14b);
#endif
	init();
	work();

#ifdef DEBUG
	printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}