POJ 2398 Toy Storage [叉积,计算几何]

131 篇文章 0 订阅

题目

Toy Storage

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 9582Accepted: 5587

Description

Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for Reza to find his favorite toys anymore.
Reza’s parents came up with the following idea. They put cardboard partitions into the box. Even if Reza keeps throwing his toys into the box, at least toys that get thrown into different partitions stay separate. The box looks like this from the top:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aeSdPLEj-1632215588662)(http://poj.org/images/2398_1.jpg)]
We want for each positive integer t, such that there exists a partition with t toys, determine how many partitions have t, toys.

Input

The input consists of a number of cases. The first line consists of six integers n, m, x1, y1, x2, y2. The number of cardboards to form the partitions is n (0 < n <= 1000) and the number of toys is given in m (0 < m <= 1000). The coordinates of the upper-left corner and the lower-right corner of the box are (x1, y1) and (x2, y2), respectively. The following n lines each consists of two integers Ui Li, indicating that the ends of the ith cardboard is at the coordinates (Ui, y1) and (Li, y2). You may assume that the cardboards do not intersect with each other. The next m lines each consists of two integers Xi Yi specifying where the ith toy has landed in the box. You may assume that no toy will land on a cardboard.

A line consisting of a single 0 terminates the input.

Output

For each box, first provide a header stating “Box” on a line of its own. After that, there will be one line of output per count (t > 0) of toys in a partition. The value t will be followed by a colon and a space, followed the number of partitions containing t toys. Output will be sorted in ascending order of t for each box.

Sample Input

4 10 0 10 100 0
20 20
80 80
60 60
40 40
5 10
15 10
95 10
25 10
65 10
75 10
35 10
45 10
55 10
85 10
5 6 0 10 60 0
4 3
15 30
3 1
6 8
10 10
2 1
2 8
1 5
5 5
40 10
7 9
0

Sample Output

Box
2: 5
Box
1: 4
2: 1

解释与代码

kuangbin计算几何基础第二题,和第一题差不多,只是加个排序加上最后格式改改就行了

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb          push_back
#define ppb         pop_back
#define lbnd        lower_bound
#define ubnd        upper_bound
#define endl        '\n'
#define trav(a, x)  for(auto& a : x)
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define DEBUG       cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x)  trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ini(a)      memset(a,0,sizeof(a))
#define case        ll T;read(T);for(ll Q=1;Q<=T;Q++)
#define lowbit(x)   x&(-x)
#define pr          printf
#define sc          scanf
#define _           0
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \
    (void)(cout << "L" << __LINE__ \
    << ": " << #x << " = " << (x) << '\n')
#define TIE \
    cin.tie(0);cout.tie(0);\
    ios::sync_with_stdio(false);
//#define long long int

//using namespace __gnu_pbds;

template <typename T>
void read(T &x) {
    x = 0;
    int f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch ^ 48);
        ch = getchar();
    }
    x *= f;
    return;
}

inline void write(long long x) {
    if(x<0) putchar('-'), x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
    putchar('\n');
}

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI    = acos(-1.0);
const double eps   = 1e-6;
const int    INF   = 0x3f3f3f3f;
const ll     LLINF = 0x3f3f3f3f3f3f3f3f;
const int    maxn  = 5005;
const ll     N     = 5;


int cmp (double x) {
    if (fabs(x) < eps) return 0;
    if (x > 0) return 1;
    return -1;
}

const double pi = acos(-1.0);
inline double sqr (double x) {
    return x*x;
}

struct point {
    double x, y;
    point() {}
    point(double a, double b) : x(a), y(b) {}
    void input() {
        scanf("%lf%lf", &x, &y);
//        cin>>x>>y;
    }
    friend point operator + (const point &a, const point &b) {
		return point(a.x + b.x, a.y + b.y);
	}
	friend point operator - (const point &a, const point &b) {
		return point(a.x - b.x, a.y - b.y);
	}
	friend bool operator == (const point &a, const point &b) {
		return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0;
	}
	friend point operator * (const point &a, const double &b) {
		return point(a.x * b, a.y * b);
	}
	friend point operator * (const double &a, const point &b) {
		return point(a * b.x, a * b.y);
	}
	friend point operator / (const point &a, const double &b) {
		return point(a.x / b, a.y / b);
	}
	double norm() {//计算长度
		return sqrt(sqr(x) + sqr(y));
	}
};
//计算两个向量的叉积
double det (const point &a, const point &b) {
    return a.x*b.y - a.y*b.x;
}
//计算两个向量的点积
double dot (const point &a, const point &b) {
    return a.x*b.x + a.y*b.y;
}
double dist (const point &a, const point &b) {
    return (a - b).norm();
}
point rotate_point (const point &p, double A) {
    double tx = p.x, ty = p.y;
    return point (tx * cos(A) - ty * sin(A), tx * sin(A) + ty * cos(A));
}

struct line {
    point a, b;
    line() {}
    line (point x, point y): a(x), b(y) {};

};

bool compp (line x, line y){
	if (x.a.x == y.a.x) return x.a.y < y.a.y;
	return x.a.x < y.a.x;
}

line point_make_line(const point a, const point b) {
    return line(a, b);
}

//vector<vector<point> > vvp(5001);
vector<line> vl(maxn);
int ans[maxn];

void solve(){
	int n, m;
	double x1, y1, x2, y2, u, l;
	while (sc("%d", &n) && n!=0) {
		ini(ans);
		sc("%d%lf%lf%lf%lf",&m, &x1, &y1, &x2, &y2);
		for (int o=0; o<n; o++) {
			sc("%lf%lf",&u,&l);
			vl[o] = point_make_line(point(u, y1), point (l,y2));
//			cout<<u<<" "<<y1<<" "<<l<<" "<<y2<<endl;
		}
		sort(vl.begin(), vl.begin()+n, compp);
		for (int o=0; o<m; o++) {
			sc("%lf%lf",&u,&l);
			int L = 0, R = n;
			while (L < R) {
				int mid = (L + R) >> 1;
				point p(u-vl[mid].a.x, l-vl[mid].a.y);//计算向量
				if (det(p, vl[mid].b - vl[mid].a) < 0) {
					L = mid + 1;
				} else {
					R = mid;
				}
			}
			ans[L]++;
		}
		cout<<"Box"<<endl;
		int pppq[maxn];
		ini(pppq);
		for (int i=0; i<=n; i++) {
//			pr("%d: %d\n",i, ans[i]);
			pppq[ans[i]] ++;
		}
		for (int i=1; i<maxn; i++) {
			if (pppq[i] != 0) {
				pr("%d: %d\n",i, pppq[i]);
			}
		}
//		cout<<endl;
	}
}


int main()
{
//	TIE;
    #ifndef ONLINE_JUDGE
//    freopen ("in.txt" , "r", stdin );
//    freopen ("out.txt", "w", stdout);
    #else
    #endif
	solve();
//    case{solve();}
//    case{cout<<"Case "<<Q<<":"<<endl;solve();}
	return ~~(0^_^0);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值