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 110000
#define INF 1e9+7

int a[maxn];

int main()
{
    int n, x;

    while(scanf("%d", &n)!=EOF)
    {
        scanf("%d", &x);

        for(int i=0; i<n; i++)
            scanf("%d", &a[i]);

        sort(a, a+n);

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

        if(a[n-1]<=x)//若数组a的元素全部比x小
        {
            printf("%d\n", x-a[1]);
        }
        else if(a[0]>=x)//若数组a的元素全部比x大
        {
            printf("%d\n", a[n-2]-x);
        }
        else
        {
            int ans1, ans2;

            if(a[n-2]<x)//若数组a的元素有n-1个比x小
                ans1=x-a[0];
            else
                ans1=min(a[n-2]-x, x-a[0])*2+max(a[n-2]-x, x-a[0]);

            if(a[1]>x)//若数组a的元素有n-1个比x大
                ans2=a[n-1]-x;
            else
                ans2=min(x-a[1], a[n-1]-x)*2+max(x-a[1], a[n-1]-x);

            printf("%d\n", min(ans1, ans2));
        }
    }
    return 0;
}

时间: 2024-10-13 21:06:09

Checkpoints codeforces 709B的相关文章

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> #i

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

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