POJ 2398 二分加叉乘
2014-04-03 09:51
323 查看
题目很简单的
给定一个矩形,中间几条线段将它隔开, 给定玩具的坐标,最后统计各个区域内玩具的个数,输出有 k (k>0)个玩具的区域有多少个
好久没写二分。写了好久。。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <memory.h>
using namespace std;
#define ll __int64
const int MAXN = 4444;
struct node{
ll x,y;
node(){}
node(ll _x,ll _y){x=_x,y=_y;}
};
struct line{
node top,bottom;
bool operator < (const line & l) const{
return top.x < l.top.x;
}
}Line[MAXN];
int L[MAXN];
int length;
int total[MAXN];
ll x1,y1,x2,y2,Ui,Li,x,y;
int n,m;
// P1
// \
// .p0 \
// p2
// 这种情况。返回结果顺时针<0,逆时针>0
int Multi(node p1, node p2, node p0) {
ll ans = (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
if(ans > 0 ) return 1;
else if(ans < 0) return -1;
else return 0;
}
void calc(ll x,ll y){
//cout << "x: " << x << "y: "<<y << endl;
int l = 0,r = length - 1;
node cur = node(x,y);
if(x == x1){
L[0]++;
return;
}
if(x == x2){
L[length-1]++;
return;
}
int mid = -1;
while(l < r){
mid = (l+r)>>1;
//printf("mid = %d, l = %d r=%d\n",mid,l,r);
int r1 = Multi(Line[mid+1].top,Line[mid+1].bottom,cur);
if(r1 < 0){
if(Multi(Line[mid].top,Line[mid].bottom,cur) >= 0){
break;
}else{
r = mid ;
}
}else{
l = mid+1;
}
}
//cout << "mid+1: "<<mid+1 << endl<<"top("<<Line[mid+1].top.x << ","<<Line[mid+1].top.y <<") ,bottom("<<Line[mid+1].bottom.x <<"," << Line[mid+1].bottom.y<< ")"<<endl;
//cout << "mid"<<mid << endl << "top("<<Line[mid].top.x << ","<<Line[mid].top.y <<") ,bottom("<<Line[mid].bottom.x <<"," << Line[mid].bottom.y<< ")"<<endl;
//printf("mid = %d\n",mid);
L[mid]++;
}
int main()
{
while(cin >> n){
if(!n)break;
cin >> m >> x1 >> y1 >> x2 >> y2;
length = 0;
Line[length].top = node(x1,y1); Line[length].bottom = node(x1,y2); length++;
Line[length].top = node(x2,y1); Line[length].bottom = node(x2,y2); length++;
for(int i = 0;i < n;i++){
cin >> Ui >> Li;
Line[length].top = node(Ui,y1); Line[length].bottom = node(Li,y2); length++;
}
sort(Line,Line+length);
memset(L,0,sizeof(L));
memset(total,0,sizeof(total));
while(m--){
cin >> x >> y;
calc(x,y);
}
int max_size=0;
for(int i = 0;i <= length;i++){
total[L[i]]++;
max_size = max(L[i],max_size);
}
cout << "Box" << endl;
for(int i = 1;i <= max_size;i++){
if(total[i]){
cout << i <<": "<< total[i] << endl;
}
}
}
return 0;
}
给定一个矩形,中间几条线段将它隔开, 给定玩具的坐标,最后统计各个区域内玩具的个数,输出有 k (k>0)个玩具的区域有多少个
好久没写二分。写了好久。。
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <memory.h>
using namespace std;
#define ll __int64
const int MAXN = 4444;
struct node{
ll x,y;
node(){}
node(ll _x,ll _y){x=_x,y=_y;}
};
struct line{
node top,bottom;
bool operator < (const line & l) const{
return top.x < l.top.x;
}
}Line[MAXN];
int L[MAXN];
int length;
int total[MAXN];
ll x1,y1,x2,y2,Ui,Li,x,y;
int n,m;
// P1
// \
// .p0 \
// p2
// 这种情况。返回结果顺时针<0,逆时针>0
int Multi(node p1, node p2, node p0) {
ll ans = (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
if(ans > 0 ) return 1;
else if(ans < 0) return -1;
else return 0;
}
void calc(ll x,ll y){
//cout << "x: " << x << "y: "<<y << endl;
int l = 0,r = length - 1;
node cur = node(x,y);
if(x == x1){
L[0]++;
return;
}
if(x == x2){
L[length-1]++;
return;
}
int mid = -1;
while(l < r){
mid = (l+r)>>1;
//printf("mid = %d, l = %d r=%d\n",mid,l,r);
int r1 = Multi(Line[mid+1].top,Line[mid+1].bottom,cur);
if(r1 < 0){
if(Multi(Line[mid].top,Line[mid].bottom,cur) >= 0){
break;
}else{
r = mid ;
}
}else{
l = mid+1;
}
}
//cout << "mid+1: "<<mid+1 << endl<<"top("<<Line[mid+1].top.x << ","<<Line[mid+1].top.y <<") ,bottom("<<Line[mid+1].bottom.x <<"," << Line[mid+1].bottom.y<< ")"<<endl;
//cout << "mid"<<mid << endl << "top("<<Line[mid].top.x << ","<<Line[mid].top.y <<") ,bottom("<<Line[mid].bottom.x <<"," << Line[mid].bottom.y<< ")"<<endl;
//printf("mid = %d\n",mid);
L[mid]++;
}
int main()
{
while(cin >> n){
if(!n)break;
cin >> m >> x1 >> y1 >> x2 >> y2;
length = 0;
Line[length].top = node(x1,y1); Line[length].bottom = node(x1,y2); length++;
Line[length].top = node(x2,y1); Line[length].bottom = node(x2,y2); length++;
for(int i = 0;i < n;i++){
cin >> Ui >> Li;
Line[length].top = node(Ui,y1); Line[length].bottom = node(Li,y2); length++;
}
sort(Line,Line+length);
memset(L,0,sizeof(L));
memset(total,0,sizeof(total));
while(m--){
cin >> x >> y;
calc(x,y);
}
int max_size=0;
for(int i = 0;i <= length;i++){
total[L[i]]++;
max_size = max(L[i],max_size);
}
cout << "Box" << endl;
for(int i = 1;i <= max_size;i++){
if(total[i]){
cout << i <<": "<< total[i] << endl;
}
}
}
return 0;
}
相关文章推荐
- poj2398 几何运算(叉乘---判断是否在凸四边形内的判断(二分、排序))
- 叉乘+二分解决POJ 2398
- POJ 2318 TOYS (向量叉乘+二分)
- 叉乘+二分解决POJ 2318
- POJ 2318 TOYS / POJ 2398 (二分,叉集)
- POJ 2398 Toy Storage【叉积+二分】
- poj 2318 TOYS 叉乘 二分查找
- POJ 2398 Toy Storage [叉积判断+二分查找]【计算几何】
- POJ 2398 Toy Storage(二分加折线拐向)
- POJ 2398 计算几何+二分+排序
- poj 2398 (叉积+二分)
- POJ 2398 计算几何+二分+排序
- POJ 2398 Toy Storage(点与直线关系 + 二分)
- Poj 2398 Toy Storage (叉积+二分)
- poj 2398-几何二分搜索
- poj 2398 Toy Storage 【统计相同个数的点的 区域个数】【二分+叉积判断】
- poj 2398 Toy Storage 叉乘
- poj 2398 Toy Storage 计算几何+二分
- 7_4_P题 Toy Storage 题解[Poj 2398](计算几何,点线关系,二分)
- poj 2398 Toy Storage【二分+叉积】