POJ 2398 Toy Storage

题意:

给你一些玩具,找出有t个玩具的区间个数。

题解:

poj2318的简单进阶,区别:隔断无序,需要对这些分段进行排序。然后哈希存具有t个玩具的区间。

代码:

#include <stdio.h>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <map>
#include <list>
#include <utility>
#include <set>
#include <algorithm>
#include <deque>
#include <vector>
#define mem(arr,num) memset(arr,0,sizeof(arr))
#define _for(i, a, b) for(int i = a; i <= b; i++)
#define __for(i, a, b) for(int i = a; i >=b; i--)
#define IO ios::sync_with_stdio(false);\
        cin.tie(0);\
        cout.tie(0);
using namespace std;
typedef long long ll;
typedef vector<int > vi;
const ll INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 5000 + 5;
struct P
{
    int x,y;
    P() {}
    P(int a, int b)
    {
        x = a, y = b;
    }
    P operator- (P b)
    {
        return P(x-b.x,y-b.y);
    }
} L,R,p[N];
pair <P, P> pr;
vector<pair<P, P> > line;
double cross(P a, P b) {
    return a.x * b.y - a.y * b.x;
}
double judge(P c, P a, P b){
    return cross(c - a,b - a);
}
int res[N],ans[N];
bool cmp(pair<P,P> a,pair<P,P> b){
    return a.first.x < b.first.x;
}
int main()
{
    int n, m;
    while(cin >> n, n)
    {
        mem(res,0);
        mem(ans,0);
        line.clear();
        cin >> m >> L.x >> L.y >> R.x >> R.y;
        pr.second = L;
        pr.first.x = L.x, pr.first.y = R.y;
        line.push_back(pr);
        _for(i, 1, n)
        {
            int a, b;
            P p;
            cin >> a >> b;
            p.x = a, p.y = L.y;
            pr.second = p;
            p.x = b, p.y = R.y;
            pr.first = p;
            line.push_back(pr);
        }
        pr.second.x = R.x, pr.second.y = L.y;
        pr.first = R;
        line.push_back(pr);
        sort(line.begin(),line.end(),cmp);
        _for(i, 1, m) cin >> p[i].x >> p[i].y;
        _for(i, 1, m) {
            int l = 0, r = line.size()-1,mid;
            while(r - l != 1){
                mid = (l+r)/2;
                P _x = line[mid].first,_y = line[mid].second;
                if(judge(p[i], _x, _y)<0) r = mid;
                else l = mid;
            }
            res[l] ++;
        }
        _for(i, 0, n)
            ans[res[i]]++;
        cout << "Box" << endl;
        _for(i, 1, n)
            if(ans[i])
            cout << i <<": "<< ans[i] <<endl;
    }
    return 0;
}

 

posted @ 2018-04-11 19:47  GHzz  阅读(145)  评论(0编辑  收藏  举报