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 ...
随机推荐
- 《Think in UML》读后感
- 终于下决心在cnblogs上安家了,^_^
以前在这个地方学到了很多东西,希望在这里安家以后,自己也可以有很多成长. mark一下,~~
- wordpress建站过程1
使用wordpress我们需要理解一些概念: 1.WordPress是一种使用PHP语言开发的博客平台,它的程序是由php构成的,所以想要使用word press必须会php. 2.Wordpress ...
- prestashop 基本设置
不跳转 要先改这个UPDATE`ps_shop_url`SET`domain_ssl`= replace(`domain_ssl` ,'us2.try.net','www.gelchaussures. ...
- 《初识PE》导入表
最近听别人讲的我晕晕乎乎的,于是上网上百度下,感觉这篇还不错. 链接:http://www.blogfshare.com/pe-export.html 一.导入表简介 在编程中常常用到"导 ...
- Entity Framework技巧系列之二 - Tip 6 - 8
提示6. 如何及何时使用贪婪加载 什么时候你需要使用贪婪加载? 通常在你的程序中你知道对查询到的实体将要进行怎样的操作. 例如,如果你查询一个订单以便为一个客户重新打印,你知道没有组成订单的项目即产品 ...
- wpa_supplicant_8_ti hostapd wpa_supplicant TI 官方的wpa_supplicant hostapd 移植到linux
在移植 wpa_supplicant_8_ti 的时候碰到很多头文件找不到.然后参考了下面的博客 http://blog.csdn.net/penglijiang/article/details/85 ...
- zabbix metrics
http://blog.csdn.net/hengyunabc/article/details/44072285
- hibernate —— 树状存储
package com.pt.treeStrut; import java.util.Set; import javax.persistence.CascadeType; import javax.p ...
- word project 2010破解
1.用这个工具提示失败: failed to inject memory Failed to inject memory!解决方法 浏览:6545 | 更新:2013-07-15 15:52 在激活o ...