幽沉谢世事,俯默窥唐虞。这篇文章主要讲述CF#-931A Friends Meeting(思维)相关的知识,希望能为你提供帮助。
题干:
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.
题目大意:
数轴上有两人分别在a,b两点,每个人每走一个单位坐标,增加疲劳值,且递增,如左边的人走三步,疲劳值为1+2+3=6。两个人都能走,现希望两人碰面,且求两个人的最小疲劳值的和。
解题报告:
其实很好想啦,如果距离是偶数,找中间点就是了。如果是奇数,那就先找中间点-1,然后单独算一个人多走的一步。
这里的距离可以用他们的坐标的奇偶来看。是一样的。
AC代码:
#include< bits/stdc++.h>
using namespace std;
long long ans;
int main()
int a,b;
cin> > a> > b;
int sum = a+b;
int half;
if(sum%2 == 0)
half = sum/2;
sum = abs(half-a);
for(int i = 1; i< =sum; i++)
ans +=i;
ans+=ans;
else
int i;
half = sum/2;
a=min(a,b);
sum = abs(half-a);
for(i = 1; i< =sum; i++)
ans+=i;
ans+=ans;
ans+=i;
printf("%lld\\n",ans);
return 0 ;
【CF#-931A Friends Meeting(思维)】
推荐阅读
- HDU - 1870愚人节的礼物(水题模拟 思想类似于栈())
- 什么是真正的敏捷开发(敏捷开发与瀑布开发有何不同)
- CF#468 div2 D. Peculiar apple-tree(思维)
- HDU - 1301Jungle Roads(并查集+最小生成树)(内附最小生成树两种算法 克鲁斯特尔算法&&普里姆算法)
- client-go gin的简单整合七-继续完善
- 全链路监控pinpoint安装部署
- 金蝶云星空与钉钉集成解决方案(钉钉审批)
- Mysqldump使用手册
- 1行Python代码,获取对方的IP地址