codeforces 709B B. Checkpoints(水题)
题目链接:
题意:
给了n个点,现在给一个起点,问最少访问n-1个点的最小行走距离是多少;
思路:
分情况讨论就好了;
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=1e3+20;
const double eps=1e-12; int n,m;
int a[N]; int main()
{
read(n);read(m);
For(i,1,n)read(a[i]);
sort(a+1,a+n+1);
if(n==1)cout<<"0";
else {
int ans=inf;
if(m>=a[n])ans=m-a[2];
else if(m<=a[1])ans=a[n-1]-m;
else
{
ans=min(ans,abs(m-a[1])+a[n-1]-a[1]);
ans=min(ans,abs(m-a[2])+a[n]-a[2]);
ans=min(ans,abs(a[n]-m)+a[n]-a[2]);
ans=min(ans,abs(a[n-1]-m)+a[n-1]-a[1]);
}
cout<<ans<<endl;
} return 0;
}
codeforces 709B B. Checkpoints(水题)的更多相关文章
- Codeforces Gym 100531G Grave 水题
Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...
- codeforces 706A A. Beru-taxi(水题)
题目链接: A. Beru-taxi 题意: 问那个taxi到他的时间最短,水题; AC代码: #include <iostream> #include <cstdio> #i ...
- codeforces 569B B. Inventory(水题)
题目链接: B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces 489A SwapSort (水题)
A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- codeforces 688A A. Opponents(水题)
题目链接: A. Opponents time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- CodeForces 534B Covered Path (水题)
题意:给定两个速度,一个一初速度,一个末速度,然后给定 t 秒时间,还每秒速度最多变化多少,让你求最长距离. 析:其实这个题很水的,看一遍就知道怎么做了,很明显就是先从末速度开始算起,然后倒着推. 代 ...
- Codeforces Gym 100286I iSharp 水题
Problem I. iSharpTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
- CodeForces 705A(训练水题)
题目链接:http://codeforces.com/problemset/problem/705/A 从第三个输出中可看出规律, I hate that I love that I hate it ...
- CodeForces Gym 100685C Cinderella (水题)
题意:给定 n 个杯子,里面有不同体积的水,然后问你要把所有的杯子的水的体积都一样,至少要倒少多少个杯子. 析:既然最后都一样,那么先求平均数然后再数一下,哪个杯子的开始的体积就大于平均数,这是一定要 ...
随机推荐
- Verilog学习笔记基本语法篇(十)········ 常用系统函数
$display 和 $write 任务 格式: $display (p1,p2,...,pn); $write (p1,p2,..,pn); 这两个函数和系统的任务作用是用来输出信息,即将参数p2到 ...
- RHEL7管道与重定向
文件描述符 可以理解为linux跟踪打开文件,而分配的一个数字,这个数字有点类似c语言操作文件时候的句柄,通过句柄就可以实现文件的读写操作 用户可以自定义文件描述符范围是:3-num,这个最大数字,跟 ...
- anriod TabHost
package com.example.yanlei.mytk; import android.os.Bundle; import android.support.v7.app.AppCompatAc ...
- view.performClick()触发点击事件
1.主要作用 自动触发控件的点击事件 2.界面的布局文件 activity_main.xml <RelativeLayout xmlns:android="http://schema ...
- Centos 7: 打开Samba防火墙端口
firewall-cmd --permanent --add-port=137/tcp firewall-cmd --permanent --add-port=138/tcp firewall-cmd ...
- 搭建Android 5.0开发环境
1.Android SDK的安装 下载地址:http://developer.android.com/index.html 访问网站的话请自备梯子 选择:adt-bundle-windows-x86_ ...
- SPC.NET,为5年的开发做个结尾
从08年到如今从事asp.net开发已五年,起初只是一个简单的喜好,想做个东西出来.于是SPC.NET的前生诞生了,直至今日. 不得不说,一个人开发一个软件是很辛苦的事情,以下是SPC.NET的一些特 ...
- redis-集群(cluster)扫盲篇(一)
什么是redis的集群 按我个人的理解,redis集群就是实现多个redis节点之间进行数据的共享. 集群有什么好处: 将数据自动split到多个节点进行存储. 当集群中的一部分节点失效或者无法进行通 ...
- Effective Java 33 Use EnumMap instead of ordinal indexing
Wrong practice: Putting sets into an array indexed by the type's ordinal /** * Added demo for the &q ...
- Effective Java 64 Strive for failure atomicity
Principle Failure atomic - A failed method invocation should leave the object in the state that it w ...