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. Android 内存监测工具 DDMS --> Heap(转)

    DDMS 的全称是Dalvik Debug Monitor Service,它为我们提供例如:为测试设备截屏,针对特定的进程查看正在运行的线程以及堆信息.Logcat.广播状态信息.模拟电话呼叫.接收 ...

  2. 下拉的DIV+CSS+JS二级树型菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Chapter 1 First Sight——25

    "They are… very nice-looking." I struggled with the conspicuous understatement. 他们都很好看,我与轻 ...

  4. Android面试题集锦 (转)

    转自:http://xiechengfa.iteye.com/blog/1044721 一些常见的Android面试基础题做下总结,看看你能做出多少道? 1. Intent的几种有关Activity启 ...

  5. HUD1862:EXCEL排序

    Problem Description Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能.   Input 测试输入包含若干测试用例.每个测试用例的第1行包含两个整数 N (< ...

  6. JavaScript学习总结(十七)——Javascript原型链的原理

    一.JavaScript原型链 ECMAScript中描述了原型链的概念,并将原型链作为实现继承的主要方法.其基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法.在JavaScript中, ...

  7. httpclient调用方法

    /**  * GET请求  *   * @param url  *            请求url,参数拼在请求串中  */ public static String get(String url) ...

  8. EF中用Newtonsoft.Json引发的循环引用问题

    描述: 1.双向关系表a->b b->aList 2.在查询a引用b以后 3.用Newtonsoft.Json 去tojsonstring 4.一个只有6条数据的json串 出现了一屏幕字 ...

  9. 纯CSS做的一个Silder

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  10. js-call、apply

    这里主要就是做一些前人的总结,有时候会有自己的看法,首先把定义说一下 1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) 定义 ...