2019 CCPC - 网络选拔赛 A题^&^

一个多月前打的,但是由于一点事情一直没写关于A题的反思,今天有空果断补上:
Problem Description

Bit operation is a common computing method in computer science ,Now we have two positive integers A and B ,Please find a positive integer C that minimize the value of the formula (A xor C) & (B xor C) .Sometimes we can find a lot of C to do this ,So you need to find the smallest C that meets the criteria .

For example ,Let’s say A is equal to 5 and B is equal to 3 ,we can choose C=1,3… ,so the answer we’re looking for C is equal to 1.

If the value of the expression is 0 when C=0, please print 1.

Input

The input file contains T test samples.(1<=T<=100)

The first line of input file is an integer T.

Then the T lines contains 2 positive integers, A and B, (1≤A,B<232)

Output

For each test case,you should output the answer and a line for each answer.
分别考虑P点作直角顶点和非直角顶点。
此篇文章想说map的用法,自定义小于运算符,使得在map中查找的时候,统一斜率的向量都会加起来,虽然在map中依然会保存多个不同的向量。
复杂度为n^2log(n)
下面是AC代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2005;
struct P
{
ll x, y;
P(ll xx=0, ll yy=0) {
x = xx; y = yy;
}
P base()const{
if (x < 0 || (x == 0 && y < 0))return P(-x, -y);
return this;
}
bool operator<(const P&b)const {
P p1 = base(); P p2 =b.base();
//如果共线,考虑是相同的索引
return p1.x
p2.y<p1.y
p2.x;
}
P operator-(const P&b)const {
return P(x - b.x, y - b.y);
}
}a[N],qur[N];
int n, q;
map<P, int>m;
ll ans[N];
int main() {
while (~scanf("%d%d", &n, &q)) {
memset(ans,0,sizeof(ans));
for (int i = 0; i < n; i++)scanf("%lld%lld", &a[i].x, &a[i].y);
for (int i = 0; i < q; i++)scanf("%lld%lld", &qur[i].x, &qur[i].y);
for (int i = 0; i < q; i++) {
//求解作为直角顶点
m.clear();
for (int j = 0; j < n; j++)
m[a[j] - qur[i]]++;
for (int j = 0; j < n; j++) {
P p = a[j] - qur[i];
p = P(-p.y, p.x);
ans[i] += m.count§ ? m[p] : 0;
}
       //由于两条直角边都会枚举,所以除2
ans[i] /= 2;
}
for (int i = 0; i < n; i++) {
//作为非直角顶点,每次枚举点i,作为直角顶点,更新全部的q组询问点
m.clear();
for (int j = 0; j < n; j++) {
if (i != j)m[a[j] - a[i]]++;
}
for (int j = 0; j < q; j++) {
P p = qur[j] - a[i];
p = P(-p.y, p.x);
ans[j] += m.count§ ? m[p] : 0;
}
}
for (int i = 0; i < q; i++)printf("%lld\n", ans[i]);
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值