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.
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.
Print the minimum possible total tiredness if the friends meet in the same point.
3 4
1
101 99
2
5 10
9
题意:给你两个数ab,表示两个朋友的位置,现在这两个人要见面,第k次移动时,花费就是k,而且两人花费叠加,问你两人见面的最小花费。
思路:ab求绝对值,得到两人间距,然后除以二,表示两人都往中间跑,前n项和算一半距离,然后乘2就可以了,注意间距是奇数偶数时的特判处理。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll a,b,flag;
int main()
{
while(~scanf("%lld%lld",&a,&b))
{
ll tmp=abs(a-b);
flag=0;
if(tmp%2!=0)flag=1;
tmp/=2;
ll ans=((1+tmp)*tmp)/2;
ans*=2;
if(flag==1)ans+=tmp+1; //如果是奇数,那么需要一个人在多走一步,当然要取步数小的那个人
printf("%lld\n",ans);
}
return 0;
}