题目大意:给出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. 关于Unity项目中创建项目遇到的一些问题

    1.Unity调用Android的方法默认不是在UI线程执行,所以在Android上写一些页面的重绘的方法,让Unity去调用时,注意要在Android中添加对应的runOnUiThread才可以: ...

  2. LeetCode OJ 4. Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  3. 为什么 dll 改名字之后无法使用

    有人直接把dll名字改了,我的程序运行出错,说这是我程序的问题,难道真是这样吗? 总感觉直接改dll名字不对,但哪儿不对呢,带着这样的疑惑研究了一下,重新做了一下试验,结果程序抛出了错误: Could ...

  4. $and $not null 正则表达式

    查询MasterID大于1且MasterType等于TestType的文档: db.SysCore.find({$and:[{"MasterID":{$gt:1}},{" ...

  5. spring容器启动的加载过程(二)

    第六步: public abstract class AbstractApplicationContext extends DefaultResourceLoader implements Confi ...

  6. JIRA搭建

    请参考下面的文章 http://www.linuxidc.com/Linux/2014-09/106995.htm 所需下载的文件 链接: http://pan.baidu.com/s/1c0wad3 ...

  7. CSS 各类 块级元素 行级元素 水平 垂直 居中问题

    元素的居中问题是每个初学者碰到的第一个大问题,在此我总结了下各种块级 行级 水平 垂直 的居中方法,并尽量给出代码实例. 首先请先明白块级元素和行级元素的区别 行级元素 一块级元素 1 水平居中: ( ...

  8. 第三十四节,pickle数据类型转换二进制字节码模块

    在使用pickle模块时需要先 import pickle 引入模块 pickle.dumps()模块函数 功能:将python各种类型的数据转换成计算机识别的二进制字节码[有参] 使用方法:pick ...

  9. html 细线表格

    可以<table width="800px" cellpadding="0" border="1px" style="bor ...

  10. Beanstalkd

    摘要by ck:beanstalkd  和  kafka的本质区别是什么? Beanstalkd,一个高性能.轻量级的分布式内存队列系统,最初设计的目的是想通过后台异步执行耗时的任务来降低高容量Web ...