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. 洛谷 P2056 [ZJOI2007]捉迷藏 || bzoj 1095: [ZJOI2007]Hide 捉迷藏 || 洛谷 P4115 Qtree4 || SP2666 QTREE4 - Query on a tree IV

    意识到一点:在进行点分治时,每一个点都会作为某一级重心出现,且任意一点只作为重心恰好一次.因此原树上任意一个节点都会出现在点分树上,且是恰好一次 https://www.cnblogs.com/zzq ...

  2. 题解报告:hdu 1503 Advanced Fruits(LCS加强版)

    Problem Description The company "21st Century Fruits" has specialized in creating new sort ...

  3. 面试王牌 JAVA 多态只针对方法 不针对属性

    子类是永远继承父类的非私有化方法,当子类中重写父类方法时,调用的是子类的方法,没有重写时,调用的是父类中的方法 1 多态是只针对方法,而不是属性的,但是写法上,子类重写父类的属性,编译器是不会报错的 ...

  4. angular.module 参数的意思

    定义一个module需要两个参数,第一个作为module的名字,第二个则是指出这个module都依赖哪些别的modules

  5. Jmeter+Jenkins+Ant自动化集成环境搭建

    搭建环境: JDK:jdk1.8.0_92 Ant:apache-ant-1.9.7 Jmeter: apache-jmeter-3.0 Jenkins:jenkins-2.19.3 具体环境配置 1 ...

  6. JS 事件添加onclick写法注意。

    自定义函数添加onclick事件写法注意. 错误写法:element.onclick = addclass(className); 正确写法:element.onclick = function(){ ...

  7. AJPFX:关于面向对象及java的一些机制的思考

    1.变量的作用域和生命周期什么关系? 2.java除了在类体和方法体(包括参数)中可以声明变量外,其他位置一定不可以声明变量吗?比如高级别的应用里是不是有例外呢? 3.java源文件中代码的组织方式一 ...

  8. 微信小程序组件解读和分析:十三、radio单选项目

    radio单选项目组件说明: radio:单选项目. radio-group: 单项选择器,内部由多个<radio/>组成. radio单选项目示例代码运行效果如下: 下面是WXML代码: ...

  9. linux命令规范

    Linux文件后缀: 系统文件:*.conf    *.rpm 系统与脚本:*.c  *.php 存档文件和压缩文件:*.tar   *.gz ……… Linux文件命名规则: 1.大小写敏感 2.除 ...

  10. [转]1小时内打造你自己的PHP MVC框架

    简介 MVC框架在现在的开发中相当流行,不论你使用的是JAVA,C#,PHP或者IOS,你肯定都会选择一款框架.虽然不能保证100%的开发语言都会使用框架,但是在PHP社区当中拥有*多数量的MVC框架 ...