题目大意:给出n个点的坐标,和你当前的坐标,求走过n-1个点的最短路程。

题目思路:走过n-1个点,为了使路程更短,那么不走的点只可能第一个点或最后一个点。模拟就行了,比较恶心。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0x3f3f3f3f
#define MAX 10000005
#define Temp 1000000000 using namespace std; long long a[MAX]; int main()
{
int n,k,index,i,d;
long long lsum1,rsum1,lsum2,rsum2;
while(scanf("%d%d",&n,&k)!=EOF)
{
index=-;
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
sort(a+,a+n+);
if(n==)//如果只有一个点,输出0
{
printf("0\n");
continue;
}
if(k<=a[])//如果起始坐标在最左边,则达到第n-1个点结束
{
printf("%lld\n",a[n-]-k);
continue;
}
else if(k>=a[n])//如果起始坐标在最右边,则到达第二个点结束
{
printf("%lld\n",k-a[]);
continue;
}
else if(n==)//如果只有两个点
{
long long ans=min(k-a[],a[]-k);
printf("%lld\n",ans);
}
else
{
for(int i=;i<=n;i++)
{
if(a[i]>k)
{
index=i-;
break;
}
}
if(index>=n-)
{
lsum1=(k-a[]);//不拿N点
lsum2=(a[n]-k)*+(k-a[]);//不拿1点
rsum1=(k-a[])*+(a[n]-k);//不拿1点
long long ans=min(min(lsum1,lsum2),rsum1);
printf("%lld\n",ans);
continue;
} else if(index<=)
{
lsum1=(a[n]-k);//不拿1
lsum2=(k-a[])*+(a[n-]-k);//不拿n
rsum1=(a[n-]-k)*+(k-a[]);//不拿n
long long ans=min(min(lsum1,lsum2),rsum1);
printf("%lld\n",ans);
continue;
}
lsum1=(k-a[])*+(a[n-]-k);
lsum2=(a[n-]-k)*+(k-a[]);
rsum1=(k-a[])*+(a[n]-k);
rsum2=(a[n]-k)*+(k-a[]);
long long ans=min(min(rsum1,rsum2),min(lsum1,lsum2));
printf("%lld\n",ans);
}
}
return ;
}

CodeForces 709B Checkpoints 模拟的更多相关文章

  1. codeforces 709B Checkpoints

    题目链接:http://codeforces.com/problemset/problem/709/B 题目大意: 第一行给出两个数 n,x.第二行 输入 n 个数. 要求:从x位置遍历 n-1 个位 ...

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

    题意:给定你的坐标,和 n 个点,问你去访问至少n-1个点的最短路是多少. 析:也是一个很简单的题,肯定是访问n-1个啊,那么就考虑从你的位置出发,向左访问和向右访问总共是n-1个,也就是说你必须从1 ...

  3. Codeforces 709B 模拟

    B. Checkpoints time limit per test:1 second memory limit per test:256 megabytes input:standard input ...

  4. Checkpoints codeforces 709B

    http://codeforces.com/problemset/problem/709/B 题意:给出一条横向坐标轴,给出Vasya所在的坐标位置及其另外n个坐标.Vasya想要至少访问n-1个位置 ...

  5. codeforces 709B B. Checkpoints(水题)

    题目链接: B. Checkpoints 题意: 给了n个点,现在给一个起点,问最少访问n-1个点的最小行走距离是多少; 思路: 分情况讨论就好了; AC代码: #include <iostre ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. Codeforces 389B(十字模拟)

    Fox and Cross Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submi ...

  8. codeforces 591B Rebranding (模拟)

    Rebranding Problem Description The name of one small but proud corporation consists of n lowercase E ...

  9. Codeforces 626B Cards(模拟+规律)

    B. Cards time limit per test:2 seconds memory limit per test:256 megabytes input:standard input outp ...

随机推荐

  1. hdu_5927_Auxiliary Set(xjb搞)

    题目链接:hdu_5927_Auxiliary Set 题意: 给一棵n个节点的树,最开始全部都是重点,现在有q个询问,每次给你一些轻点,并叫你输出整棵树的重点数量, 轻点可能会变为重点,如果这个轻点 ...

  2. submit 防止重复提交 --禁止提交

    <form action="/apply/apply" method="POST" id="indentForm"> <p ...

  3. AnimatorController动画遮罩

    游戏中,我们的人物在某些时候,可能在一个时间点在做两件事,这样就需要同时播放两个动画,但我们肯定不能简单将其叠加,比如移动着开枪.攻击.挥手等等,那么在Unity中我们应该怎样实现这样的功能呢?且听我 ...

  4. 自己写deque

    //deque /* what is a deque? In Chinese, it's called "双端队列". It's different from a queue. I ...

  5. 【转】Java线程与Linux内核线程的映射关系

    Linux从内核2.6开始使用NPTL (Native POSIX Thread Library)支持,但这时线程本质上还轻量级进程. Java里的线程是由JVM来管理的,它如何对应到操作系统的线程是 ...

  6. VS中调试时监控线程的最后错误代码

    进行调试的时候,监控线程的最后错误代码是非常有用的. 在 Microsoft Visual studio 中,Microsoft 的调试程序支持一个非常有用的特性,即可以配置 Watch 窗口,以便始 ...

  7. EnumPrinters用法

    看API描述,不怎么理解,还是在网上找到用法,记载一下: 1: DWORD dwNeeded = 0, dwNum = 0; 2: EnumPrinters(PRINTER_ENUM_LOCAL, N ...

  8. Ubuntu14.04下安装redis

    1.首先在官网上下载redis压缩包 redis-3.2.0.tar.gz 2.解压到到当前文件夹(这里可以解压到随意位置) tar zvxf redis-3.2.0.tar.gz 3.切换到redi ...

  9. WimTool(Wim映像处理工具) V1.30.2011.501 免费绿色版

    软件名称: WimTool(Wim映像处理工具) V1.30.2011.501 免费绿色版软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinX ...

  10. windows任务计划程序路径设置

    用任务计划启动程序,特别是脚本,比如我要启动python脚本,其中有一句是这么写的 BasePath = removeLastSlash(os.path.abspath("..\\..\\& ...