http://codeforces.com/contest/709

题目大意:给一个一维的坐标轴,上面有n个点,我们刚开始在位置a,问,从a点开始走,走n-1个点所需要的最小路程。

思路:我们知道,如果你一会儿走左一会儿左右,最后访问n-1个点一定只会让距离更长的,所以我们的策略是刚开始全都往一端走,然后访问完n-1个点即可。刚开始我是分类讨论的。。。讨论的要死了。。。不过后来看了一下别人的代码,发现虽然思路是对的,但是过程想麻烦了。 具体看代码吧。

我的乱七八糟的分类讨论

//看看会不会爆int! 或者绝对值问题。
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define ALL(a) a.begin(), a.end()
const int maxn = + ;
const LL inf = 1e18;
int n;
LL a;
LL x[maxn]; int main(){
cin >> n >> a;
LL tmp = a;
for (int i = ; i < n; i++) scanf("%lld", x + i);
if (n == ) {printf("0\n"); return ;}
sort(x, x + n); x[n] = inf;
int pos = lower_bound(x, x + n, a) - x;
LL lb = a - x[], rb = x[n-] - a;
LL ans = inf;
if (pos == ) ans = min(ans, rb - x[n - ] + x[n - ]);
else if (pos == ){
ans = min(rb, lb * + rb - x[n - ] + x[n - ]);
ans = min( * abs(rb - x[n - ] + x[n - ]) + lb, ans);
}
else if (pos == n - ) {
if (x[pos] == a) ans = min(ans, lb - x[] + x[]);
else {
ans = min(lb, rb * + lb - x[] + x[]);
ans = min(ans, * (lb - x[] + x[]) + rb);
}
}
else if (pos == n) ans = min(ans, lb - x[] + x[]);
else {
ans = min( * lb + rb - x[n - ] + x[n - ], * rb + lb - x[] + x[]);
ans = min(ans, * (lb - x[] + x[]) + rb);
ans = min(ans, * (rb - x[n - ] + x[n - ]) + lb);
}
cout << ans << endl;
return ;
}

另一种写法,很简便

//看看会不会爆int! 或者绝对值问题。
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define ALL(a) a.begin(), a.end()
const int maxn = + ;
const LL inf = 1e18;
int n;
LL a;
LL x[maxn]; int main(){
cin >> n >> a;
for (int i = ; i < n; i++) scanf("%lld", x + i);
if (n == ) {printf("0\n"); return ;}
sort(x, x + n); x[n] = inf;
LL ans = min(abs(x[] - a), abs(x[n - ] - a)) + x[n - ] - x[];
LL tmp = min(abs(x[] - a), abs(x[n - ] - a)) + x[n - ] - x[];
cout << min(ans, tmp) << endl;
return ;
}

AIM Tech Round 3 (Div. 2) B 数学+贪心的更多相关文章

  1. codeforce AIM tech Round 4 div 2 B rectangles

    2017-08-25 15:32:14 writer:pprp 题目: B. Rectangles time limit per test 1 second memory limit per test ...

  2. AIM Tech Round 3 (Div. 2)

    #include <iostream> using namespace std; ]; int main() { int n, b, d; cin >> n >> ...

  3. AIM Tech Round 3 (Div. 2) A B C D

    虽然打的时候是深夜但是状态比较好 但还是犯了好多错误..加分场愣是打成了降分场 ABC都比较水 一会敲完去看D 很快的就想出了求0和1个数的办法 然后一直wa在第四组..快结束的时候B因为低级错误被h ...

  4. AIM Tech Round 3 (Div. 2) B

    Description Vasya takes part in the orienteering competition. There are n checkpoints located along ...

  5. AIM Tech Round 3 (Div. 2) A

    Description Kolya is going to make fresh orange juice. He has n oranges of sizes a1, a2, ..., an. Ko ...

  6. AIM Tech Round 3 (Div. 2) (B C D E) (codeforces 709B 709C 709D 709E)

    rating又掉下去了.好不容易蓝了.... A..没读懂题,wa了好几次,明天问队友补上... B. Checkpoints 题意:一条直线上n个点x1,x2...xn,现在在位置a,求要经过任意n ...

  7. AIM Tech Round 3 (Div. 2)D. Recover the String(贪心+字符串)

    D. Recover the String time limit per test 1 second memory limit per test 256 megabytes input standar ...

  8. AIM Tech Round 4 (Div. 2)ABCD

    A. Diversity time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  9. AIM Tech Round 4 (Div. 2)(A,暴力,B,组合数,C,STL+排序)

    A. Diversity time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

随机推荐

  1. js 中null 和 undifined

    1.一个重要问题 null == undifined (true) ; null === undefined (false) 为什么是这样的呢? undifined是未赋值的基本数据类型,未定义: n ...

  2. Spring框架_代理模式(静态代理,动态代理,cglib代理)

    共性问题: 1. 服务器启动报错,什么原因? * jar包缺少.jar包冲突 1) 先检查项目中是否缺少jar包引用 2) 服务器: 检查jar包有没有发布到服务器下:                 ...

  3. oracle 备份操作流程

    Oracle 库表导出步骤 例如,要导出wcsr用户下的所有表,已知用户名/密码:wcsr/wcsr_woer 首先打开cmd.exe 其次创建备份目录,最好目录不包含空格和中文名 md d:\ora ...

  4. wmic应用实例

    实例应用 1.磁盘管理 查看磁盘的属性 wmic logicaldisk list brief ::caption=标题.driveID=驱动器ID号.model=产品型号.Partitions=分区 ...

  5. Java编程中时区和时间相关的问题

    先说一个结论:时间戳是一个和时区无关的东西,在哪里都是一致的!但是转化为字符串之后,就和时区有关系了. 以下为内容: 参考链接:http://m.blog.csdn.net/article/detai ...

  6. 无论url请求什么.都可以拼接class类名.实例化.传递get参数-->给当前控制器-->传递给抽象父类-->都交给抽象父类.这个方法去处理call_user_func_array()

    <?phpdefine('DS','/');define('A_PATH',str_replace('\\','/',dirname(__FILE__)).DS); //01获取到主程序目录cl ...

  7. MySQL数据表中内容大小写区分的设置

    MYSQL在默认的情况下查询是不区分大小写的,例如:    ? 1 2 3 4 5 6 7 mysql> create table t1( -> name varchar(10)); Qu ...

  8. 2.1 sikuli 中编程运行

    1.用sikuli编程时,多用wait()语句,因为很多时候没有给它一定的识别时间,就容易出错. 比如下图,保证页面加载时间 1.Sikuli中 ,可以加# 进行注释 但是注释有的时候也会不起作用,比 ...

  9. 超棒的自定义超酷滚动条jQuery插件 - Perfect Scrollbar

    可能大家厌倦了千篇一律的页面滚动条,如果你希望能够设计出与众不同的页面UI设计的话,Perfect ScrollBar可能就是你寻找的解决方案. 这个滚动条来自于一个个人项目,一个简单但是非常棒的滚动 ...

  10. iOS8.0以后的相册

    在 iOS 8.0 后, 使用the Photos framework 代替 the Assets Library framework , The Photos framework 提供更特色和更好的 ...