LightOJ 1418 Trees on My Island (Pick定理)
题目链接:LightOJ 1418
Problem Description
I have bought an island where I want to plant trees in rows and columns. So, the trees will form a rectangular grid and each of them can be thought of having integer coordinates by taking a suitable grid point as the origin.
But, the problem is that the island itself is not rectangular. So, I have identified a simple polygonal area inside the island with vertices on the grid points and have decided to plant trees on grid points lying strictly inside the polygon.
Figure: A sample of my island
For example, in the above figure, the green circles form the polygon, and the blue circles show the position of the trees.
Now, I seek your help for calculating the number of trees that can be planted on my island.
Input
Input starts with an integer \(T (≤ 100)\), denoting the number of test cases.
Each case starts with a line containing an integer \(N (3 ≤ N ≤ 10000)\) denoting the number of vertices of the polygon.
Each of the next \(N\) lines contains two integers \(x_i y_i (-10^6 ≤ x_i, y_i ≤ 10^6)\) denoting the co-ordinate of a vertex. The vertices will be given in clockwise or anti-clockwise order. And they will form a simple polygon.
Output
For each case, print the case number and the total number of trees that can be planted inside the polygon.
Sample Input
1
9
1 2
2 1
4 1
4 3
6 2
6 4
4 5
1 5
2 3
Sample Output
Case 1: 8
Note
Dataset is huge, use faster I/O methods.
Solution
题意:
给定一个多边形,顶点都在格点上,求多边形内部的格点个数。
思路
Pick 定理 裸题。
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef double db;
const db eps = 1e-10;
const db pi = acos(-1.0);
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll maxn = 1e5 + 10;
inline int dcmp(db x) {
if(fabs(x) < eps) return 0;
return x > 0? 1: -1;
}
class Point {
public:
ll x, y;
Point(ll x = 0, ll y = 0) : x(x), y(y) {}
void input() {
scanf("%lld%lld", &x, &y);
}
Point operator-(const Point a) {
return Point(x - a.x, y - a.y);
}
ll cross(const Point a) {
return x * a.y - y * a.x;
}
};
Point p[maxn];
ll gcd(ll a, ll b) {
return b == 0? a: gcd(b, a % b);
}
int main() {
int T;
scanf("%d", &T);
for(int _ = 1; _ <= T; ++_) {
int n;
scanf("%d", &n);
ll on = 0;
ll s = 0;
for(int i = 0; i < n; ++i) {
p[i].input();
}
p[n] = p[0];
for(int i = 0; i < n; ++i) {
s += (p[i + 1] - p[0]).cross(p[i] - p[0]);
on += gcd(abs(p[i].x - p[i + 1].x), abs(p[i].y - p[i + 1].y));
}
s = abs(s);
ll in = s / 2 - on / 2 + 1;
printf("Case %d: ", _);
printf("%lld\n", in);
}
return 0;
}
LightOJ 1418 Trees on My Island (Pick定理)的更多相关文章
- UVa 10088 - Trees on My Island (pick定理)
样例: 输入:123 16 39 28 49 69 98 96 55 84 43 51 3121000 10002000 10004000 20006000 10008000 30008000 800 ...
- UVa 10088 (Pick定理) Trees on My Island
这种1A的感觉真好 #include <cstdio> #include <vector> #include <cmath> using namespace std ...
- HDU 3775 Chain Code ——(Pick定理)
Pick定理运用在整点围城的面积,有以下公式:S围 = S内(线内部的整点个数)+ S线(线上整点的个数)/2 - 1.在这题上,我们可以用叉乘计算S围,题意要求的答案应该是S内+S线.那么我们进行推 ...
- 【POJ】2954 Triangle(pick定理)
http://poj.org/problem?id=2954 表示我交了20+次... 为什么呢?因为多组数据我是这样判断的:da=sum{a[i].x+a[i].y},然后!da就表示没有数据了QA ...
- Area(Pick定理POJ1256)
Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5429 Accepted: 2436 Description ...
- poj 2954 Triangle(Pick定理)
链接:http://poj.org/problem?id=2954 Triangle Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- poj 1265 Area (Pick定理+求面积)
链接:http://poj.org/problem?id=1265 Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- poj1265Area(pick定理)
链接 Pick定理是说,在一个平面直角坐标系内,如果一个多边形的顶点全都在格点上,那么这个图形的面积恰好就等于边界上经过的格点数的一半加上内部所含格点数再减一. pick定理的一些应用 题意不好懂, ...
- pick定理:面积=内部整数点数+边上整数点数/2-1
//pick定理:面积=内部整数点数+边上整数点数/2-1 // POJ 2954 #include <iostream> #include <cstdio> #include ...
随机推荐
- MYSQL 的七种join
建表 在这里呢我们先来建立两张有外键关联的张表. CREATE DATABASE db0206; USE db0206; CREATE TABLE `db0206`.`tbl_dept`( `id` ...
- (转)vue项目刷新当前页面
场景: 有时候我们在vue项目页面做了一些操作,需要刷新一下页面. 解决的办法及遇到的问题: this.$router.go(0).这种方法虽然代码很少,只有一行,但是体验很差.页面会一瞬间的白屏,体 ...
- Identity MVC:UI
基于原来做的cookie认证的代码:MvcCookieAuthSample 增加登陆和退出的方法: 增加Login和SignIn这两个Action方法. 在Views下面创建Account文件夹,然后 ...
- vector中数据释放崩溃问题
struct LINE { char securityID[32]; /*!< 证券代码 */ int64_t dateTime; /*!< 日期时间(日期变化)YYYYMMDDhhmms ...
- es5和es6中的this指向问题
const test ={ id:2, a:function(){ var a_this=this; setTimeout(function(){ console.log('a:',this,a_th ...
- Freeswitch Tutorial
I. Install Freeswitch 1) FreeSWITCH Explained https://freeswitch.org/confluence/ https://freeswitch. ...
- rpc - rpc 程序号数据库
SYNOPSIS /etc/rpc DESCRIPTION rpc 文件列出了rpc 程序的可读名, 可以此代替rpc 程序号. 每行包含如下信息: 运行rpc 程序的服务名 rpc 程序号 别名 各 ...
- EAN13条码的校验位的Excel算法
校验位原公式: 单元格=10-RIGHT(SUM(MID($B3,{1;2;3;4;5;6;7;8;9;10;11;12},1)*{1;3;1;3;1;3;1;3;1;3;1;3})) 简化公式: 单 ...
- CF555E Case of Computer Network
题面:https://www.luogu.com.cn/problem/CF555E 题意:给定一张\(n\)个点\(m\)条边的无向图. 给定\(q\)组有向点对\((s,t)\). 询问是否存在使 ...
- Ubunu12.04 vncserver xstartup配置文件
以下有效: #!/bin/sh # Uncomment the following two lines for normal desktop: unset SESSION_MANAGER #exec ...