玲珑杯 1143

来源:互联网 发布:最好的网络电话软件 编辑:程序博客网 时间:2024/05/19 12:16

1143 - 计算几何你瞎暴力
Time Limit:5s Memory Limit:256MByte

Submissions:1735Solved:341

DESCRIPTION
今天HH考完了期末考试,他在教学楼里闲逛,他看着教学楼里一间间的教室,于是开始思考:如果从一个坐标为 (x1,y1,z1)的教室走到(x2,y2,z2)的距离为 |x1−x2|+|y1−y2|+|z1−z2|那么有多少对教室之间的距离是不超过
R的呢?
INPUT
第一行是一个整数T(1≤T≤10), 表示有T组数据
接下来是T组数据,对于每组数据:
第一行是两个整数n,q(1≤n≤5×104,1≤q≤103), 表示有
n间教室, q次询问.
接下来是n行, 每行3个整数xi,yi,zi(0≤xi,yi,zi≤10),表示这间教室的坐标.
最后是q行,每行一个整数R(0≤R≤109),意思见描述.
OUTPUT
对于每个询问
R
R输出一行一个整数,表示有多少对教室满足题目所述的距离关系.
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
“玲珑杯”ACM比赛 Round #18
题解:正常做是o(Tn^2)的,会超时,发现,xi,yi,zi的范围较小(0-10),所以不重复的点只有11*11*11。去重后n变小,可过。
代码:

#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<vector>#include<queue>#include<algorithm>#include<map>using namespace std;typedef long long int ll;typedef pair<int,int>pa;const int N=2e5+10;const int MOD=1e9+7;const ll INF=1e18;int read(){    int x=0;    char ch = getchar();    while('0'>ch||ch>'9')ch=getchar();    while('0'<=ch&&ch<='9')    {        x=(x<<3)+(x<<1)+ch-'0';        ch=getchar();    }    return x;}/************************************************************/struct node{    int x,y,z;    bool operator <(const node &t)const    {        if(x!=t.x) return x<t.x;        else if(y!=t.y) return y<t.y;        else return z<t.z;    }}a[N],b[N];int t,n,q;ll l;ll cnt[50];ll g[N];int solve(node q,node w){    return abs(q.x-w.x)+abs(q.y-w.y)+abs(q.z-w.z);}int solo(node q,node w){    if(q.x!=w.x||q.y!=w.y||q.z!=w.z) return 1;    return 0;}int main(){    scanf("%d",&t);    while(t--)    {        memset(cnt,0,sizeof(cnt));        scanf("%d%d",&n,&q);        for(int i=1;i<=n;i++)        {            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);            g[i]=1;        }        sort(a+1,a+1+n);        int tot=1;        b[1]=a[1];        for(int i=2;i<=n;i++)        {            if(solo(a[i],a[i-1]))                b[++tot]=a[i];             else g[tot]++;        }        for(int i=1;i<=tot;i++)        {            for(int j=i+1;j<=tot;j++)            {                int dis=solve(b[i],b[j]);                cnt[dis]+=g[i]*g[j];            }        }        for(int i=1;i<=tot;i++)        {            if(g[i]==1) continue;            else                cnt[0]+=g[i]*(g[i]-1)/2;        }        for(int i=1;i<=30;i++) cnt[i]+=cnt[i-1];        while(q--)        {            scanf("%lld",&l);            if(l>30)                l=30;            printf("%lld\n",cnt[l]);        }    }    return 0;}
原创粉丝点击