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 ...
随机推荐
- 1.Date对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Infix to postfix without '(' and ')'
#include<iostream> #include<stack> #include<string> #include<deque> using na ...
- DELPHI中MessageBox的用法 (转)
MessageBox对话框 输入控件的 ImeName属性把输入法去掉就默认为英文输入了 MessageBox对话框是比较常用的一个信息对话框,其不仅能够定义显示的信息内容.信息提示图标,而且可以 ...
- Delphi用ADOquery主从表例子(转总结)
http://blog.csdn.net/kandy_zheng/article/details/1639184 在sql server 的northwide 中建立主表 create table s ...
- [ An Ac a Day ^_^ ] FZU 2030 括号问题 搜索
FZU一直交不上去 先写在这吧 #include<stdio.h> #include<iostream> #include<algorithm> #include& ...
- PureLayout
PureLayout 是 iOS & OS X Auto Layout 的终极 API——非常简单,又非常强大.PureLayout 通过一个全面的Auto Layout API 扩展了 UI ...
- UVALive 7077 Little Zu Chongzhi's Triangles (有序序列和三角形的关系)
这个题--我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了--结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小, ...
- C# 反射相关的东西
public class PlugingManager { //插件装载器 public ArrayList Plugins = new ArrayList(); ...
- cPanel下载及安装信息 /usr/local/cpanel/bin/adduser-->realadduser
cPanel下载及安装信息 2011年03月31日 发布在 编程开发 留下评论 说到cPanel,懂的人都知道玩意得研究研究. ISO安装 http://layer2.cpanel.net/Cen ...
- Chapter 2 Open Book——1
The next day was better… and worse. 明天会更好也会更坏. It was better because it wasn't raining yet, though t ...