AIM Tech Round 3 (Div. 2) B 数学+贪心
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 数学+贪心的更多相关文章
- 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 ...
- AIM Tech Round 3 (Div. 2)
#include <iostream> using namespace std; ]; int main() { int n, b, d; cin >> n >> ...
- AIM Tech Round 3 (Div. 2) A B C D
虽然打的时候是深夜但是状态比较好 但还是犯了好多错误..加分场愣是打成了降分场 ABC都比较水 一会敲完去看D 很快的就想出了求0和1个数的办法 然后一直wa在第四组..快结束的时候B因为低级错误被h ...
- AIM Tech Round 3 (Div. 2) B
Description Vasya takes part in the orienteering competition. There are n checkpoints located along ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 深入理解JNI(《深入理解android》(author : 邓凡平)读书札记)
JNI的技术特点: java能够调用native代码. native代码能够调用java代码. JNI的技术考虑: 实现java代码的平台无关型. java语言发展初期使用C和C++代码,避免重复 ...
- html/css技巧总结
.e-select .on{display:none} on为e-select的子元素,(之间有空格).e-select.on{display:block} 只有两种属性同时存在时才会起作用(之间无空 ...
- 缓存2 动态缓存 memcached
01准备下载好memcached.exe文件-->放置c:\memcached目录中02准备下载好php_memcache.dll文件-->放置php\ext扩展目录中03配置 php.i ...
- ebtables
ebtables是以太网桥防火墙,以太网工作在数据链路层,ebtables过滤数据链路层包.2.6内核内置了ebtables,要使用它必须先按装她的用户空间工具(ebtables-V2.0.6),安装 ...
- Git 多人协作开发
当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且你的远程仓库的默认名称是origin 查看远程库的信息,用git remote LV@LV-PC ...
- viewpager处理(一):让viewpager不能滑动
1.实现原理: 自定义viewpager,重写onTouchEvent方法,什么触摸事件都不响应即可让viewpager不能滑动. 2.代码如下 public class NoScrollViewPa ...
- Oracle where 0=1 or 1=1
本文转载自:http://www.cnblogs.com/junyuz/archive/2011/03/10/1979646.html sql where 1=1和 0=1 的作用 where 1 ...
- zookeeper集群实例
zookeeper是什么 Zookeeper,一种分布式应用的协作服务,是Google的Chubby一个开源的实现,是Hadoop的分布式协调服务,它包含一个简单的原语集,应用于分布式应用的协作服务, ...
- IOS 中常用站位符
CGPoint.CGRect等可以转化为字符串打印出来 如: NSLog(@"-------------%@",NSStringFromCGPoint(point)); ...
- Petit FatFs
FatFs is a generic FAT/exFAT file system module for small embedded systems. The FatFs module is writ ...