Astronomy Problem

Time Limit: 8000ms
Memory Limit: 524288KB

This problem will be judged on CodeForcesGym. Original ID: 100524A
64-bit integer IO format: %I64d      Java class name: (Any)

 
解题:暴力搞
$设两个点的坐标分别为(x_1,y_1),(x_2,y_2)且有x_1^2+y_1^2 = a^2,x_2^2+y_2^2=b^2,那么有(x_1-x_2)^2+(y_1-y_2)^2=c^2。$
$于是推出一个错误的公式,a^2+b^2-c^2=2\times (x_1\times x_2+y_1\times y_2),初略的想法是:等式右边是固定的,所以可以根据这个进行排序,左边是不定的$
$可以用左边找右边,可是,可是,WA第7组数据. 因为,你算多了,而且这个等式本身就不对$
$但是,但是,这个错误的等式可以为我们省出不少的点对,只要不满足这个等式的点对,一定不是符合条件的,但是满足这个等式,也有可能不符合条件$
$所以只会找出这些点对,进行一一检验即可$
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
struct Point{
LL val,a,b,c;
Point(LL val = ,LL a = ,LL b = ,LL c = ){
this->val = val;
this->a = a;
this->b = b;
this->c = c;
}
bool operator<(const Point &rhs)const{
return val < rhs.val;
}
}P[maxn*maxn];
int x[maxn],y[maxn],n,q,tot;
LL calc(int a,int b){
LL tmp = (LL)(x[a] - x[b])*(x[a] - x[b]);
return tmp + (LL)(y[a] - y[b])*(y[a] - y[b]);
}
int main(){
freopen("astronomy.in","r",stdin);
freopen("astronomy.out","w",stdout);
while(~scanf("%d",&n)){
if(!n) break;
tot = ;
for(int i = ; i <= n; ++i){
scanf("%d%d",x + i,y + i);
for(int j = ; j < i; ++j){
LL tmp = ((LL)x[i]*x[j] + (LL)y[i]*y[j])<<;
P[tot++] = Point(tmp,calc(j,),calc(i,),calc(i,j));
}
}
sort(P,P + tot);
scanf("%d",&q);
while(q--){
LL a,b,c;
scanf("%I64d%I64d%I64d",&a,&b,&c);
LL tmp = a - c + b;
if(n == || tmp&){
puts("");
continue;
}
int low = lower_bound(P,P + tot,Point(tmp,,,)) - P,ret = ;
while(low < tot && P[low].val == tmp){
if(P[low].a == a && P[low].b == b || P[low].a == b && P[low].b == a) ++ret;
++low;
}
printf("%d\n",ret);
}
}
return ;
}
/*
4
0 2
2 0
4 0
0 -4
2
4 16 20
16 4 20
*/

然后学习了下某位大神的写法

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
unordered_map<LL,vector<int>>ump;
unordered_map<LL,int>ret;
map<pair<LL,LL>,vector<pair<LL,int>>>Q;
struct Point {
LL x,y;
Point(LL x = ,LL y = ) {
this->x = x;
this->y = y;
}
} p[maxn];
LL calc(const Point &a,const Point &b) {
LL tmp = (a.x - b.x)*(a.x - b.x);
return tmp + (a.y - b.y)*(a.y - b.y);
}
set<LL>st;
int ans[maxn];
int main() {
#define NAME "astronomy"
freopen(NAME".in","r",stdin);
freopen(NAME".out","w",stdout);
int n,q;
while(~scanf("%d",&n)) {
if(!n) break;
ump.clear();
Q.clear();
memset(ans,,sizeof ans);
for(int i = ; i <= n; ++i) {
scanf("%I64d%I64d",&p[i].x,&p[i].y);
ump[p[i].x*p[i].x+p[i].y*p[i].y].push_back(i);
}
scanf("%d",&q);
for(int i = ; i < q; ++i){
LL a,b,c;
scanf("%I64d%I64d%I64d",&a,&b,&c);
Q[make_pair(a,b)].push_back(make_pair(c,i));
}
for(auto &it:Q){
st.clear();
for(auto &it2:it.second) st.insert(it2.first);
LL a = it.first.first,b = it.first.second;
ret.clear();
for(auto &x:ump[a]){
for(auto &y:ump[b]){
if(a == b && x < y) continue;
if(x == y) continue;
LL d = calc(p[x],p[y]);
if(st.find(d) == st.end()) continue;
ret[d]++;
}
}
for(auto &v:it.second)
ans[v.second] = ret[v.first];
}
for(int i = ; i < q; ++i)
printf("%d\n",ans[i]);
}
return ;
}
/*
4
0 2
2 0
4 0
0 -4
2
4 16 20
16 4 20
*/

CodeForcesGym 100524A Astronomy Problem的更多相关文章

  1. CodeForcesGym 100548G The Problem to Slow Down You

    The Problem to Slow Down You Time Limit: 20000ms Memory Limit: 524288KB This problem will be judged ...

  2. CodeForcesGym 100512D Dynamic LCA

    Dynamic LCA Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. ...

  3. CodeForcesGym 100517I IQ Test

    IQ Test Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Orig ...

  4. CodeForcesGym 100517B Bubble Sort

    Bubble Sort Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. ...

  5. CodeForcesGym 100517H Hentium Scheduling

    Hentium Scheduling Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForc ...

  6. CodeForcesGym 100524J Jingles of a String

    Jingles of a String Time Limit: 2000ms Memory Limit: 524288KB This problem will be judged on CodeFor ...

  7. CodeForcesGym 100212E Long Dominoes

    Long Dominoes Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on CodeForcesGym. ...

  8. CodeForcesGym 100753K Upside down primes

    Upside down primes Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForc ...

  9. CodeForcesGym 100753F Divisions

    Divisions Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. Or ...

随机推荐

  1. _bzoj1007 [HNOI2008]水平可见直线【单调栈】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1007 按斜率排序,去掉斜率相同时,截距较小的直线(即只保留该斜率下截距最大的直线).若当前直 ...

  2. 实现字符串的查找和替换 分类: c/c++ 2014-10-09 22:33 469人阅读 评论(0) 收藏

    在字符串中查找目标字符串并将其替换为指定字符串,返回替换的次数.接口为 int find_str_replace(char *&str,const char *find_str,const c ...

  3. NDK(10)Android.mk各属性简介,Android.mk 常用模板--未完

    参考 : http://blog.csdn.net/hudashi/article/details/7059006 1. Android.mk简介 Android.mk文件是GNU Makefile的 ...

  4. Web常见几种攻击与预防方式

    DoS和DDoS攻击 DoS(Denial of Service),即拒绝服务,造成远程服务器拒绝服务的行为被称为DoS攻击.其目的是使计算机或网络无法提供正常的服务.最常见的DoS攻击有计算机网络带 ...

  5. Elasticsearch--集群&吞吐量

    目录 高查询和高吞吐量 过滤器缓存 字段数据缓存和断路器 断路器 存储模块 索引缓冲和刷新率 索引刷新率 线程池的配置 一些通用的建议来配置高索引和查询吞吐量的集群 高查询和高吞吐量 过滤器缓存 过滤 ...

  6. swiper4初始化/swiper-init/data-swiper

    用data属性初始化swiper <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  7. iOS programming Delegation and Text Input

    iOS programming Delegation and Text Input  1.1 Text Fields    CGRect textFieldRect = CGRectMake(40, ...

  8. Linux下支持mysql支持远程ip访问

    示例代码: use mysql; SELECT `Host`,`User` FROM user; UPDATE user SET `Host` = '%' WHERE `User` = 'use**' ...

  9. WindowsService+Quartz.NET快速搭建

    新建一个Windows服务项目 nuget安装Quartz.NET,我这边使用的是2.3.3版本 1. Service改名 2. 添加安装程序,改名 3. ServiceInstaller->属 ...

  10. 使用Win7 64位旗舰版光盘映像安装Windows Home basic 64位操作系统

    工作当中需要安装Windows home basic 64位操作系统,苦于手头没有该版本的安装光盘,也没时间下载其安装映像.因此,在现有资源“cn_windows_7_ultimate_with_sp ...