POJ 2398 map /// 判断点与直线的位置关系

题目大意:

poj2318改个输出

输出 a: b 即有a个玩具的格子有b个

 

可以先看下poj2318的报告

用map就很方便

 

#include <cstdio>
#include <cmath>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
const double eps=1e-10;
const int N=1000+5;
double add(double a,double b)
{
    if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
    return a+b;
}
struct P {
    double x,y;
    P(){};
    P(double _x,double _y):x(_x),y(_y){};
    P operator - (P p) {
        return P(add(x,-p.x),add(y,-p.y)); };
    P operator + (P p) {
        return P(add(x,p.x),add(y,p.y)); };
    P operator * (double d) {
        return P(x*d,y*d); };
    double dot (P p) {
        return add(x*p.x,y*p.y); };
    double det (P p) {
        return add(x*p.y,-y*p.x); };
}st,ed,a[N],toy;
map <int,int> mp, ans;
int n, m;
bool cmp(P a,P b) {
    if(a.x==b.x) return a.y<b.y;
    return a.x<b.x;
}
int main()
{
    while(~scanf("%d",&n)) {
        if(n==0) break;
        scanf("%d%lf%lf%lf%lf",&m,&st.x,&st.y,&ed.x,&ed.y);
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&a[i].x,&a[i].y);
        a[n].x=ed.x, a[n].y=ed.y;
        sort(a,a+1+n,cmp);
        mp.clear(); ans.clear();
        for(int i=0;i<m;i++) {
            scanf("%lf%lf",&toy.x,&toy.y);
            int k=0;
            while((toy-P(a[k].x,st.y)).det(P(a[k].y,ed.y)-toy)<0) k++;
            mp[k]++; /// mp记录每个格子对应的玩具数量
        }
        printf("Box\n");
        map <int,int> :: iterator it;
        for(it=mp.begin();it!=mp.end();it++)
            ans[it->second]++; /// 遍历mp ans记录每种玩具数量对应的格子数
        for(it=ans.begin();it!=ans.end();it++)
            printf("%d: %d\n",it->first,it->second);
    }
    return 0;
}
View Code

 

posted @ 2018-09-08 08:22  _Jessie  阅读(158)  评论(0编辑  收藏  举报