【codeforces 709B】Checkpoints

【题目链接】:http://codeforces.com/contest/709/problem/B

【题意】



让你从起点开始走过n-1个点(至少n-1个)

问你最少走多远;

【题解】



肯定不多走啊;

则肯定要有一个点不走;

->哪个点呢;

就是排序之后,最左边或最右边那个点不走;

不可能是中间的点。

因为既然你要走的点不是最边上的点,那么你肯定会在去最边上的点的时候路过那个你选择不走的点;

这个点的选取就没有意义了;

然后对于两种情况;

还有两种可能,就是先往左一直(不回头不然更长)走然后再往右,或者先往右一直走然后再往左;



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x) typedef pair<int, int> pii;
typedef pair<LL, LL> pll; const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e6+100; int n,x;
int a[N]; int main()
{
//freopen("F:\\rush.txt", "r", stdin);
rei(n), rei(x);
rep1(i, 1, n)
rei(a[i]);
sort(a + 1, a + 1 + n);
if (n == 1) return puts("0"), 0;
int dis1 = min(abs(x - a[2]) + abs(a[n] - a[2]),abs(x-a[n])+abs(a[n]-a[2]));
int dis2 = min(abs(x - a[1]) + abs(a[n - 1] - a[1]),abs(x-a[n-1])+abs(a[n-1]-a[1]));
printf("%d\n", min(dis1, dis2));
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
上一篇:函数讲解


下一篇:Execution default-cli of goal org.mybatis.generator:mybatis-generator-maven-plugin:1.3.2:generate failed: Exception getting JDBC Driver: com.mysql.jdbc.Driver (mybatis逆向工程)