POJ 2318 TOYS
计算几何+二分
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string.h>
#define maxn 5005
using namespace std;
double precision=1e-16;
struct Point{
int x;
int y;
};
struct Cardboard{
Point a;
Point b;
};
Cardboard card[maxn];
Point p[maxn];
int num[maxn];//用来记录每一个隔断里存了多少个点
int check(Point a,Point b,Point c){
int ans=(a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
if(ans>precision){
return 1;
}else{
return 0;
}
}
int erfen(int x,int y,Point p){
int l=x;
int r=y-1;
while(l<=r){
int mid=(l+r)/2;
if(check(card[mid].a,card[mid].b,p)){
l=mid+1;
}else{
r=mid-1;
}
}
num[l]++;
}
int main(){
while(1){
memset(num,0,sizeof(num));
int n,m,x1,y1,x2,y2;
cin>>n;
if(n==0){
break;
}
else{
cin>>m>>x1>>y1>>x2>>y2;
for(int i=0;i<n;i++){
cin>>card[i].a.x;
card[i].a.y=y1;
cin>>card[i].b.x;
card[i].b.y=y2;
}
for(int i=0;i<m;i++){
cin>>p[i].x;
cin>>p[i].y;
erfen(0,n,p[i]);
}
for(int i=0;i<=n;i++){
cout<<i<<": "<<num[i]<<endl;
}
cout<<endl;
}
}
}
/*
5 6 0 10 60 0
3 1
4 3
6 8
10 10
15 30
1 5
2 1
2 8
5 5
40 10
7 9
4 10 0 10 100 0
20 20
40 40
60 60
80 80
5 10
15 10
25 10
35 10
45 10
55 10
65 10
75 10
85 10
95 10
0
*/
POJ 2398 Toy Storage
计算几何+二分+结构体排序
与上一题类似,只不过是对输出方式做一点改变
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string.h>
#define maxn 5005
using namespace std;
double precision=1e-16;
struct Point{
int x;
int y;
};
struct Cardboard{
Point a;
Point b;
};
Cardboard card[maxn];
Point p[maxn];
int num[maxn];//用来记录每一个隔断里存了多少个点
int Num[maxn];//用来记录存了这个点的隔断有多少个
int cmp1(Cardboard A,Cardboard B){
if(A.a.x<B.a.x){
return 1;
}
return 0;
}
int cmp2(Cardboard A,Cardboard B){
if(A.b.x<B.b.x){
return 1;
}
return 0;
}
int check(Point a,Point b,Point c){
int ans=(a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
if(ans>precision){
return 1;
}else{
return 0;
}
}
int erfen(int x,int y,Point p){
int l=x;
int r=y-1;
while(l<=r){
int mid=(l+r)/2;
if(check(card[mid].a,card[mid].b,p)){
l=mid+1;
}else{
r=mid-1;
}
}
num[l]++;
}
int main(){
while(1){
memset(num,0,sizeof(num));
memset(Num,0,sizeof(Num));
int n,m,x1,y1,x2,y2;
cin>>n;
if(n==0){
break;
}
else{
cin>>m>>x1>>y1>>x2>>y2;
for(int i=0;i<n;i++){
cin>>card[i].a.x;
card[i].a.y=y1;
cin>>card[i].b.x;
card[i].b.y=y2;
}
sort(card,card+n,cmp1);
sort(card,card+n,cmp2);
for(int i=0;i<m;i++){
cin>>p[i].x;
cin>>p[i].y;
erfen(0,n,p[i]);
}
for(int i=0;i<=n;i++){
if(num[i]!=0){
Num[num[i]]++;
}
}
cout<<"Box"<<endl;
for(int i=0;i<=n;i++){
if(Num[i]==0){
continue;
}
else{
cout<<i<<": "<<Num[i]<<endl;
}
}
}
}
}
/*
5 6 0 10 60 0
3 1
4 3
6 8
10 10
15 30
1 5
2 1
2 8
5 5
40 10
7 9
4 10 0 10 100 0
20 20
40 40
60 60
80 80
5 10
15 10
25 10
35 10
45 10
55 10
65 10
75 10
85 10
95 10
0
*/