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 ...
随机推荐
- 浙大pat 1062题解
1061. Dating (20) 时间限制 50 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Sherlock Holmes ...
- memo用法总结
添加 mmo1.Lines.add('新加的一行');//追加一行文字 mmo1.Lines.Insert(1,'新插入一行');//在指定位置插入一行 删除 mmo1.Lines.Delete(1) ...
- [ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂
从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了 ...
- CodeForces 577C Vasya and Petya's Game 数学
题意就是给你一个1到n的范围 你每次可以问这个数是否可以被某一个数整除 问你要猜多少数才能确定这个数…… 一开始一点思路也没有 后来查了一下才知道 每个数都可以分为几个质数的整数次幂相乘得到…… #i ...
- Gentoo安装详解(四)-- 声卡设置
硬件检测 To choose the right driver, first detect the used audio controller. You can use lspci for this ...
- WTL消息以及处理函数声明
MSG_WM_CREATE LRESULT OnCreate(LPCREATESTRUCT lpCreateStruct); MSG_WM_INITDIALOG LRESULT OnInitDialo ...
- NOIP2010-普及组复赛-第四题-三国游戏
题目描述 Description 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏. 在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有 N 位武将(N为偶数且不 ...
- TCP小结
TCP/IP协议实现了不同主机,不同操作系统之间信息交流.由4层构成,从上往下依次为: 1.应用层,包括http,ftp等协议,用于实现某一项具体的功能. 2.传输层,包括TCP和UDP,一个可靠,一 ...
- 二十一、oracle pl/sql分类一 存储过程
存储过程用于执行特定的操作,当建立存储过程时,既可以指定输入参数(in),也可以指定输出参数(out),通过在过程中使用输入参数,可以将数据传递到执行部分:通过使用输出参数,可以将执行部分的数据传递到 ...
- Access to the path "Library\UnityAssemblies\UnityEngine.xml" is denied.
这个问题基本上是重新打开UnityEditor,导入工程的时候VisualStudio还开着导致的. 解决方法是关掉Visual Studio,再重新打开.