Codeforces Round #665 (Div. 2) A. Distance and Axis

这是一道来自Codeforces的算法题目,要求在OX轴上找到一个整数点B,使得从原点O到B的距离与从点A到B的距离的绝对差等于k。初始点A坐标为n,k的范围在0到10^6之间。解题思路涉及判断B相对于A的位置,并计算最小步数。
摘要由CSDN通过智能技术生成

Title

CodeForces 1401 A. Distance and Axis

Time Limit

1 second

Memory Limit

256 megabytes

Problem Description

We have a point A A A with coordinate x = n x = n x=n on O X OX OX-axis. We’d like to find an integer point B B B (also on O X OX OX-axis), such that the absolute difference between the distance from O O O to B B B and the distance from A A A to B B B is equal to k k k.
在这里插入图片描述

Since sometimes it’s impossible to find such point B B B, we can, in one step, increase or decrease the coordinate of A A A by 1 1 1. What is the minimum number of steps we should do to make such point B B B exist?

Input

The first line contains one integer t t t ( 1 ≤ t ≤ 6000 1 \le t \le 6000 1t6000) — the number of test cases.

The only line of each test case contains two integers n n n and k k k ( 0 ≤ n , k ≤ 1 0 6 0 \le n, k \le 10^6 0n,k106) — the initial position of point A A A and desirable absolute difference.

Output

For each test case, print the minimum number of steps to make point B B B exist.

Sample Input

6
4 0
5 8
0 1000000
0 0
1 0
1000000 1000000

Sample Onput

0
3
1000000
0
1
0

Note

In the first test case (picture above), if we set the coordinate of B B B as 2 2 2 then the absolute difference will be equal to ∣ ( 2 − 0 ) − ( 4 − 2 ) ∣ = 0 |(2 - 0) - (4 - 2)| = 0 (20)(42)=0 and we don’t have to move A A A. So the answer is 0 0 0.

In the second test case, we can increase the coordinate of A A A by 3 3 3 and set the coordinate of B B B as 0 0 0 or 8 8 8. The absolute difference will be equal to ∣ 8 − 0 ∣ = 8 |8 - 0| = 8 80=8, so the answer is 3 3 3.
在这里插入图片描述

Source

CodeForces 1401 A. Distance and Axis

题意

O X OX OX轴上给出点 A A A的位置 n n n,和 k k k

每次操作可以移动A向左或向右一个单位。

求最小操作使得 ∣ ∣ O B ∣ − ∣ A B ∣ ∣ = k ||OB|-|AB||=k OBAB=k。(B可以在轴上的任意整数点)

思路

  • B B B A A A点左侧时,可以看做将 ∣ O A ∣ |OA| OA划分成两段。
    1. 如果 ∣ O A ∣ |OA| OA是奇数,则一定是奇数-偶数得奇数,偶数则一定得偶数
    2. 所以 ∣ O A ∣ |OA| OA可以得到 ≤ n \le n n的任意同奇偶性的 k k k
    3. 当不同奇偶性时 可以往右移 1 1 1个单位
  • 当B在A点右侧时
    • 易得答案是 ∣ O A ∣ |OA| OA,则结果为 k − n k-n kn(k>n时)

所以答案为 m a x ( i n t ( k % 2 ≠ n % 2 ) , k − n ) max(int(k \% 2 \ne n \% 2), k - n) max(int(k%2=n%2),kn)

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int maxn = 3e5 + 5;
const ll inf = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int T;
    cin >> T;
    while (T--) {
        int n, k;
        cin >> n >> k;
        cout << max(int(k % 2 != n % 2), k - n) << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值