CodeForces 709B Checkpoints

分类讨论。

先对$n$个坐标进行排序。如果$a$在$x[1]$左边,那么肯定是$x[n]$不走。如果$a$在$x[n]$右边,那么$x[1]$不走。

剩下的讨论一下是$x[1]$不走还是$x[n]$不走,几种情况都算一下取最小值即可。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar(); x = 0;while(!isdigit(c)) c = getchar();
    while(isdigit(c)) { x = x * 10 + c - ‘0‘; c = getchar();  }
}

LL INF=1e17;
const int maxn=100010;
LL x[maxn],a;
int n;

int main()
{
    scanf("%d%lld",&n,&a);
    for(int i=1;i<=n;i++) scanf("%lld",&x[i]);
    sort(x+1,x+1+n);

    if(n==1) {printf("0\n");return 0;}

    if(x[1]>=a) { printf("%lld\n",x[n-1]-a); return 0;}
    if(a>=x[n]) { printf("%lld\n",a-x[2]); return 0;}

    LL f1=INF,f2=INF,f3=INF,f4=INF;

    if(x[2]<=a) f1=2*(a-x[2])+(x[n]-a);
    else f1=x[n]-a;

    f2=2*(x[n]-a)+(a-x[2]);

    if(a<=x[n-1]) f3=2*(x[n-1]-a)+(a-x[1]);
    else f3=a-x[1];

    if(x[1]<=a) f4=2*(a-x[1])+(x[n-1]-a);

    printf("%lld\n",min(min(f1,f2),min(f3,f4)));

    return 0;
}
时间: 2024-08-01 10:42:00

CodeForces 709B Checkpoints的相关文章

CodeForces 709B Checkpoints (数学,最短路)

题意:给定你的坐标,和 n 个点,问你去访问至少n-1个点的最短路是多少. 析:也是一个很简单的题,肯定是访问n-1个啊,那么就考虑从你的位置出发,向左访问和向右访问总共是n-1个,也就是说你必须从1 - n-1 全访问一次, 或者是2 - n 全访问一次,有一段是访问了两次,加上就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <s

CodeForces 709B Checkpoints 模拟

题目大意:给出n个点的坐标,和你当前的坐标,求走过n-1个点的最短路程. 题目思路:走过n-1个点,为了使路程更短,那么不走的点只可能第一个点或最后一个点.模拟就行了,比较恶心. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<vector> 5 #include<stdio.h> 6 #include<stdlib.h> 7 #inc

Checkpoints codeforces 709B

http://codeforces.com/problemset/problem/709/B 题意:给出一条横向坐标轴,给出Vasya所在的坐标位置及其另外n个坐标.Vasya想要至少访问n-1个位置,问最短所需要走的距离为多少? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> using namespace std; #define maxn 1

Codeforces Round #604 (Div. 2)

https://codeforces.com/contest/1265 这场的2E是1C的不带checkpoints的版本. B - Beautiful Numbers 题意:给一个数组是一个[1,n]的permutation.对每个m∈[1,n]问[1,m]是否连续存在在这个数组中. 题解: 首先,[1,1]一定存在. 然后向指定方向扩展到2,若经过2以外的数,把2标记为不存在. 3已经被扩展,或3在区间左右?是:否. 所以每次就问新的数是不是在已有区间中或者左右,是则包含,否则扩展到有为止.

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store