题解:P1693 [USACO19FEB] Sleepy Cow Herding B
题目大意
一数轴上有三点,每次把最大或最小的一点牛移到另外两点的中间,求最大和最小的移动次数。
思路
首先
排序。
最大值
把端点移到中间点的一侧,然后继续选取移动后的端点重复上述操作直到三点相邻,由于有两个端点,所以有两种方法,大的那一个就是最大值。
最小值
第一种是三个点已相邻,直接输出两个
代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a[3];
cin >> a[0] >> a[1] >> a[2];
sort(a , a + 3);
int x = a[1] - a[0] , y = a[2] - a[1];
if(x == 1 && y == 1)
{
cout << 0 << endl;
cout << 0 << endl;
return 0;
}
if (a[2] - a[1] == 2 || a[1] - a[0] == 2)
{
cout << 1 << endl;
}
else
{
cout << 2 << endl;
}
cout << max(x , y) - 1;
return 0;
}