codeforces 931 A. Friends Meeting

本文介绍了一道关于两个朋友如何以最小花费在坐标轴上某一点相遇的问题。通过贪心算法,文章详细解释了如何计算两人平摊移动步数以达到最小总花费的方法。

Description

Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b.

Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2, the third — by 3 and so on. For example, if a friend moves first to the left, then to the right (returning to the same point), and then again to the left his tiredness becomes equal to 1 + 2 + 3 = 6.

The friends want to meet in a integer point. Determine the minimum total tiredness they should gain, if they meet in the same point.

Input

The first line contains a single integer a (1 ≤ a ≤ 1000) — the initial position of the first friend.

The second line contains a single integer b (1 ≤ b ≤ 1000) — the initial position of the second friend.

It is guaranteed that a ≠ b.

Output

Print the minimum possible total tiredness if the friends meet in the same point.

Examples

Input
3
4
Output
1


Input
101
99
Output
2


Input
5
10
Output
9

Note

In the first example the first friend should move by one to the right (then the meeting happens at point 4), or the second friend should move by one to the left (then the meeting happens at point 3). In both cases, the total tiredness becomes 1.

In the second example the first friend should move by one to the left, and the second friend should move by one to the right. Then they meet in the point 100, and the total tiredness becomes 1 + 1 = 2.

In the third example one of the optimal ways is the following. The first friend should move three times to the right, and the second friend — two times to the left. Thus the friends meet in the point 8, and the total tiredness becomes 1 + 2 + 3 + 1 + 2 = 9.


题意:两个人分别站在a1和a2,他们走一步可以移动1,花费是这样的:走第一步花费1,走第二步花费2,走第i步花费i…以此类推。问总共最少需要多少花费让两个人相遇。
思路:贪心,相遇的总步数是固定的,但前面的步数花费少,所有应该尽可能让两个人平摊步数。总步数分奇偶的情况,但使用一些技巧可以避免讨论。
代码:

#include<stdio.h>

int main() {
    int a1=0, a2=0;
    int d=0;
    int b=0,ans=0;
    scanf("%d %d", &a1, &a2);
    d = a1 > a2 ? a1 - a2 : a2 - a1;
    b = d / 2;
    //b是两个人平摊的步数,即有两个1+2+...+b的和
    //d&1是在处理d是奇数的情况,需要其中一个人多走一步,多一次b+1的花费
    ans = b*(b+1) + (d & 1)*(b + 1);
    printf("%d\n", ans);
}
题目描述 Every fall, all movies are shown to a full house at one of the most popular cinema theatres in Yekaterinburg because students like to spend their time sitting in a cosy chair and watching a movie instead of attending lectures and seminars. Unfortunately, the distance between the rows of seats in the cinema hall is small, and people constantly stumble over other people's feet as they get to their seats before the film exhibition. Entering the hall, a visitor chooses from which end of the row (left or right) he will make his way to his seat. He chooses it in such a way that the number of people over whose feet he will stumble will be minimal. If these numbers are equal for the left and right ends, the visitor chooses the end of the row which is closer to his seat. Student of the Department of Philosophy Vasya is an enthusiastic movie-goer and an equally enthusiastic hater of mathematics. He was the first to buy a ticket to the first exhibition of a new movie. When Vasya entered the hall and sat down in his seat, he saw that other seats in the row were still unoccupied. Vasya knew that by the time the exhibition started the hall would be full. Therefore, quite a number of other visitors would stumble over his feet while getting to their seats. Despite his hatred for mathematics, Vasya was able to instantly estimate the maximal number of people that would stumble over his feet before the exhibition. Can you do the same? 每年秋天,叶卡捷琳堡最受欢迎的电影院之一的所有电影都会座无虚席,因为学生们喜欢坐在舒适的椅子上看电影,而不是参加讲座和研讨会。不幸的是,电影院里的两排座位之间的距离很小,人们在电影放映前就座时经常被别人绊倒。进入大厅后,访客可以选择从排的哪一端(左端或右端)就座。他选择的方式是让他绊倒的人数最少。如果左右两端的数字相等,则访客会选择最靠近他座位的那排末端。 哲学系学生瓦夏 (Vasya) 是一名狂热的电影爱好者,同时也是一名数学狂热爱好者。他是第一个购买新电影首映门票的人。当瓦夏走进大厅,在自己的座位上坐下时,他看到这一排的其他座位还空着。瓦夏知道,当展览开始时,大厅里已经挤满了人。因此,不少其他观众在入座时都会被他的脚绊倒。尽管瓦夏讨厌数学,但他还是能够立即估计出在展览前被他绊倒的最大人数。你能做同样的事吗? 输入 Input The only input line contains the total number of seats n in the row where Vasya is sitting and the number of his seat k (1 ≤ k ≤ n ≤ 50; n is even). These integers are separated with a space. The seats in the row are numbered starting with one. 唯一的输入行包含Vasya 所在行的 座位总数 n以及他的座位号k (1 ≤ k ≤ n ≤ 50; n 为偶数)。这些整数之间用空格分隔。一排的座位是从一开始编号的。 输出 Output Output the maximal number of people who would stumble over Vasya's feet. 输出最多有多少人会被 Vasya 绊倒。 样例输入 复制 4 1 样例输出 复制 1给出C++代码
最新发布
09-04
### Codeforces 1732A Bestie 题目解析 对于给定的整数数组 \(a\) 和查询次数 \(q\),每次查询给出两个索引 \(l, r\),需要计算子数组 \([l,r]\) 的最大公约数(GCD)。如果 GCD 结果为 1,则返回 "YES";否则返回 "NO"[^4]。 #### 解决方案概述 为了高效解决这个问题,可以预先处理数据以便快速响应多个查询。具体方法如下: - **预处理阶段**:构建辅助结构来存储每一对可能区间的 GCD 值。 - **查询阶段**:利用已有的辅助结构,在常量时间内完成每个查询。 然而,考虑到内存限制以及效率问题,直接保存所有区间的结果并不现实。因此采用更优化的方法——稀疏表(Sparse Table),它允许 O(1) 时间内求任意连续子序列的最大值/最小值/GCD等问题,并且支持静态RMQ(Range Minimum Query)/RANGE_GCD等操作。 #### 实现细节 ##### 构建稀疏表 通过动态规划的方式填充二维表格 `st`,其中 `st[i][j]` 表示从位置 i 开始长度为 \(2^j\) 的子串的最大公约数值。初始化时只需考虑单元素情况即 j=0 的情形,之后逐步扩展至更大的范围直到覆盖整个输入序列。 ```cpp const int MAXN = 2e5 + 5; int st[MAXN][20]; // Sparse table for storing precomputed results. vector<int> nums; void build_sparse_table() { memset(st,-1,sizeof(st)); // Initialize the base case where interval length is one element only. for(int i = 0 ;i < nums.size(); ++i){ st[i][0]=nums[i]; } // Fill up sparse table using previously computed values. for (int j = 1;(1 << j)<=nums.size();++j){ for (int i = 0;i+(1<<j)-1<nums.size();++i){ if(i==0 || st[i][j-1]!=-1 && st[i+(1<<(j-1))][j-1]!=-1) st[i][j]=__gcd(st[i][j-1],st[i+(1<<(j-1))][j-1]); } } } ``` ##### 处理查询请求 当接收到具体的 l 和 r 参数后,可以通过查找对应的 log₂(r-l+1) 来定位合适的跳跃步长 k ,进而组合得到最终答案。 ```cpp string query(int L,int R){ int K=(int)(log2(R-L+1)); return __gcd(st[L][K],st[R-(1<<K)+1][K])==1?"YES":"NO"; } ``` 这种方法能在较短时间内完成大量查询任务的同时保持较低的空间开销,非常适合本题设定下的性能需求。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值