玲珑学院 1143 计算几何你瞎暴力【计算几何】【技巧暴力】


1143 - 计算几何你瞎暴力

Time Limit:5s Memory Limit:256MByte

Submissions:1828Solved:365

DESCRIPTION

今天HHHH考完了期末考试,他在教学楼里闲逛,他看着教学楼里一间间的教室,于是开始思考:


如果从一个坐标为 (x1,y1,z1)(x1,y1,z1)的教室走到(x2,y2,z2)(x2,y2,z2)的距离为 |x1x2|+|y1y2|+|z1z2||x1−x2|+|y1−y2|+|z1−z2|


那么有多少对教室之间的距离是不超过RR的呢?


INPUT
第一行是一个整数 T(1T10)T(1≤T≤10), 表示有 TT组数据

接下来是 TT组数据,对于每组数据:

第一行是两个整数 n,q(1n5×104,1q103)n,q(1≤n≤5×104,1≤q≤103), 表示有 nn间教室, qq次询问.

接下来是 nn行, 每行3个整数 xi,yi,zi(0xi,yi,zi10)xi,yi,zi(0≤xi,yi,zi≤10),表示这间教室的坐标.

最后是 qq行,每行一个整数 R(0R109)R(0≤R≤109),意思见描述.
OUTPUT
对于每个询问 RR输出一行一个整数,表示有多少对教室满足题目所述的距离关系.
SAMPLE INPUT
1
3 3
0 0 0
1 1 1
1 1 1
1
2
3
SAMPLE OUTPUT
1
1
3
HINT
对于样例,1号教室和2号教室之间的距离为3, 1号和3号之间的距离为3, 2号和3号之间的距离为0
SOLUTION

题目链接: 点击打开链接
题目大意:给出n个教室的坐标(三维)以及求任意两点间距离的方式,q次询问,对每次询问输出距离小于R的坐标对数。
解题思路:
一、【真丶暴力】
先离线存取每个点坐标,然后两层循环求出所有可能组合的情况,将结果存在答案数组里,因为数组里存的是距离等于R的组合种类数,而题目要求的是距离大于等于R的组合种类数,所以需要从前往后更新一下答案数组。最后对于每次询问,直接输出结果即可。
注:因为两坐标间距离最大为30,所以当R超过30时,将其看作30就可以了。
Mycode:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX = 50000+5;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;

int t, n, q, tem, ans;
struct node
{
    int x, y, z;
};
int dis(node a, node b)
{
    return (abs(a.x-b.x) + abs(a.y-b.y) + abs(a.z - b.z));
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        int aa[35];
        node a[MAX];
        scanf("%d%d",&n,&q);
        for(int i = 0; i < n; ++i)
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);

        for(int i = 0; i < n; ++i)
        {
            for(int j = i+1; j < n; ++j)
            {
                ++aa[dis(a[i], a[j])];
            }
        }

        for(int i = 1; i <= 30; ++i)
            aa[i] += aa[i-1];

        while(q--)
        {
            scanf("%d",&tem);
            if(tem >= 30)
                printf("%d\n",aa[30]);
            else
                printf("%d\n",aa[tem]);
        }
    }
    return 0;
}
显然,这样做会TLE,毕竟n的范围达到了5*1e4。再读读题,这时才意识到坐标范围为[0,10]。根据这个条件可以得到思路二。
二、【技巧丶暴力】
用三维数组记录每个点出现的次数,然后遍历每个坐标,根据其出现次数得到组合时出现的情况。
注:
(1).两坐标相同时,其可能的组合种类数为 n!/n 即 n*(n-1)/2  <n为坐标的个数> 
(2).两坐标不同时,其可能的组合种类数为 n*m/2   <n,m分别为两坐标的个数>
Mycode:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX = 10005;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;

int n, q, t, tem;
int a, b, c, x, y, z;
LL aa[35];
LL dex[15][15][15];

int dis(int aa, int bb, int cc, int xx, int yy, int zz)
{
    return abs(aa-xx)+abs(bb-yy)+abs(cc-zz);
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset(aa, 0, sizeof(aa));
        memset(dex, 0, sizeof(dex));
        scanf("%d%d",&n,&q);
        while(n--)
        {
            scanf("%d%d%d",&x,&y,&z);
            ++dex[x][y][z];
        }
        for(a = 0; a <= 10; ++a)
            for(b = 0; b <= 10; ++b)
                for(c = 0; c <= 10; ++c)
                    if(dex[a][b][c])
                        for(x = 0; x <= 10; ++x)
                            for(y = 0; y <= 10; ++y)
                                for(z = 0; z <= 10; ++z)
                                    if(dex[x][y][z])
                                    {
                                        tem = dis(a, b, c, x, y, z);
                                        if(tem == 0)
                                            aa[tem] += (dex[x][y][z])*(dex[x][y][z]-1)/2;
                                        else
                                            aa[tem] += dex[x][y][z]*dex[a][b][c];
                                    }
        for(int i = 1; i <= 30; ++i)
            aa[i] /= 2;
        for(int i = 1; i <= 30; ++i)
            aa[i] += aa[i-1];
        while(q--)
        {
            scanf("%d",&tem);
            if(tem > 30)
                tem = 30;
            printf("%lld\n",aa[tem]);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值