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 ...
随机推荐
- javaScript中的一些知识
利用js动态生成table <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http ...
- Apache+Tomcat服务器集群配置
在实际应用中,如果网站的访问量很大,为了提高访问速度,可以与多个Tomcat服务器与Apache服务器集成,让他们共同运行servlet/jsp 组件的任务,多个Tomcat服务器构成了一个集群(Cl ...
- 理解 bashrc 和 profile(转)
转自:https://wido.me/sunteya/understand-bashrc-and-profile/ 在一般的 linux 或者 unix 系统中, 都可以通过编辑 bashrc 和 p ...
- 在OSchina上看到的清理到缓存的方法
/* * *文 件 名: DataCleanManager.java * 描 述: 主要功能有清除内/外缓存,清除数据库,清除sharedPreference,清除files和清除自定义目录 */ i ...
- YII2 运行概述(Overview)
运行机制概述 每一次 Yii 应用开始处理 HTTP 请求时,它都会进行一个近似的流程. 用户提交指向 入口脚本 web/index.php 的请求. 入口脚本会加载 配置数组 并创建一个 应用 实例 ...
- git(工作区,暂存区,管理修改,撤销修改,删除文件)
工作区和暂存区 984次阅读 Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的l ...
- HUD1862:EXCEL排序
Problem Description Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能. Input 测试输入包含若干测试用例.每个测试用例的第1行包含两个整数 N (< ...
- Angularjs中的ng-class
在angular中为我们提供了3种方案处理class:1:scope变量绑定.(不推荐使用)2:字符串数组形式.3:对象key/value处理. 我们继续其他两种解决方案:1字符串数组形式是针对cla ...
- 初识Iaas,paas
Iaas(Infrastructure-as-a-service),直译为基础设备作为一种服务. Paas(Platform as a service),直译为平台作为一种服务. 暂且忘掉这两个单词, ...
- Mysql5.7 安装
1 安装yum源 yum localinstall http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm2 参看源 vi ...