您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  计算几何 POJ ACM 二分