CodeForces 709B Checkpoints 模拟
题目大意:给出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 模拟的更多相关文章
- codeforces 709B Checkpoints
题目链接:http://codeforces.com/problemset/problem/709/B 题目大意: 第一行给出两个数 n,x.第二行 输入 n 个数. 要求:从x位置遍历 n-1 个位 ...
- CodeForces 709B Checkpoints (数学,最短路)
题意:给定你的坐标,和 n 个点,问你去访问至少n-1个点的最短路是多少. 析:也是一个很简单的题,肯定是访问n-1个啊,那么就考虑从你的位置出发,向左访问和向右访问总共是n-1个,也就是说你必须从1 ...
- Codeforces 709B 模拟
B. Checkpoints time limit per test:1 second memory limit per test:256 megabytes input:standard input ...
- Checkpoints codeforces 709B
http://codeforces.com/problemset/problem/709/B 题意:给出一条横向坐标轴,给出Vasya所在的坐标位置及其另外n个坐标.Vasya想要至少访问n-1个位置 ...
- codeforces 709B B. Checkpoints(水题)
题目链接: B. Checkpoints 题意: 给了n个点,现在给一个起点,问最少访问n-1个点的最小行走距离是多少; 思路: 分情况讨论就好了; AC代码: #include <iostre ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- Codeforces 389B(十字模拟)
Fox and Cross Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submi ...
- codeforces 591B Rebranding (模拟)
Rebranding Problem Description The name of one small but proud corporation consists of n lowercase E ...
- Codeforces 626B Cards(模拟+规律)
B. Cards time limit per test:2 seconds memory limit per test:256 megabytes input:standard input outp ...
随机推荐
- Dokan简介[转]
1. Dokan Library 简介 Dokan Library 帮助程序员在windows系统下轻松建立用户级文件系统,不需要写设备驱动,其与FUSE(Linux user mode f ...
- UNIX基础--用户和基本账户管理
账户类型 系统账户 系统账户运行服务. 系统用户是那些要使用诸如DNS. 邮件, web等服务的用户. 使用帐户的原因就是安全: 如果所有的用户都由超级用户来运行, 那它们就可以不受约束地做任何事情. ...
- StretchBlt 提高图片显示质量的方法
通过SetStretchMode(STRETCH_HALFTONE)实现,同时注意重新设置画刷起点 LRESULT CMainDlg::OnPaint(UINT uMsg, WPARAM wParam ...
- 利用POI获取Excel中图片和图片位置
利用POI获取Excel中图片和图片位置(支持excel2003or2007多sheet) 转自:http://blog.csdn.net/delongcpp/article/details/8833 ...
- Developing a Custom Membership Provider from the scratch, and using it in the FBA (Form Based Authentication) in SharePoint 2010
//http://blog.sharedove.com/adisjugo/index.php/2011/01/05/writing-a-custom-membership-provider-and-u ...
- ural 1352. Mersenne Primes
1352. Mersenne Primes Time limit: 1.0 secondMemory limit: 64 MB Definition. If the number 2N−1 is pr ...
- Linux openvswitch性能调优
Increasing the flow-eviction threshold The threshold is a type of limit on the number of flows that ...
- zookeeper的安装及集群配置
1.解压 2.修改配置文件 cp zoo_sample.cfg zoo.cfg vim zoo.cfg dataDir=/usr/local/zookeeperData 其余采用默认 参数说明: ti ...
- IIS启用GZip压缩
IIS启用GZip压缩,是提高网站速度和减轻服务器负载的一个优化手段和方法,经测试,网站启用GZip压缩后,速度快了3倍!而配置起来也相当的简单,因此被广大网站管理员使用.本文将详细介绍如何在IIS启 ...
- Minigui3.0 自定义遥控输入引擎
本人最近在从事minigui的开发工作,使用的gui版本为最新的3.0.12,平台环境为海思的HI3515. 在历经千辛万苦,终于将gui成功的移植到了开发板上,这里不多赘述,没有移植成功的朋友可以点 ...