poj1723 SOLDIERS
soldiers真乃神题也!
行列显然可以分开处理。
行好办,显然就是一个货仓选址问题,取中位数即可。
列呢??
??????
因为懒得推式子,用不了二分,我决定使用枚举大法!一算复杂度O(n^2),勉强可以卡过。
于是我做好了卡常数的准备,正在实现枚举及求值时,得到了援助:
你把士兵按照x排序,然后把x[i] - i即可转化为行上面的。
我略加思索:排好后是x[1], x[1] + 1, x[i] + 2, ..., x[i] + (i - 1)
那么我事先减去,便是x[1], x[1], x[1], ..., x[1]
妙啊,妙啊!
关于为何排序:使用了一点贪心的思想:如果一个A在B左边,那么排好队之后A也一定在B左边。
然后我抱着试一试的心态,过了样例,一口气AC了!!!
我们学到了什么?
转换:减去i
#include <cstdio>
#include <algorithm>
#include <cstring>
using std::sort;
inline void read(int &x) {
x = ;
bool f = ;
char c = getchar();
while(c < '' || c > '') {
if(c == '-') {
f = ;
}
c = getchar();
}
while(c <= '' && c >= '') {
x = (x << ) + (x << ) + c - '';
c = getchar();
}
if(f) x = -x;
return;
}
inline void max(int &a, int b) {
if(a < b) a = b;
return;
}
inline void min(int &a, int b) {
if(a > b) a = b;
return;
}
inline int ab(int x) {
return (x < ) ? ((~x) + ) : x;
}
const int N = , INF = 0x7f7f7f7f; int x[N], y[N], n; int main() {
int lx = -INF, sx = INF;
int xx, yy;
read(n);
for(int i = ; i <= n; i++) {
read(x[i]);
read(y[i]);
} long long ans = ; ///solve y -> 1
sort(y + , y + n + );
int k = y[(n + ) >> ];
for(int i = ; i <= n; i++) {
ans += ab(y[i] - k);
} /// solve x -> []
sort(x + , x + n + );
for(int i = ; i <= n; i++) {
x[i] -= (i - );
}
sort(x + , x + n + );
k = x[(n + ) >> ];
for(int i = ; i <= n; i++) {
ans += ab(x[i] - k);
} printf("%lld", ans);
return ;
}
AC代码
poj1723 SOLDIERS的更多相关文章
- [POJ1723]SOLDIERS(中位数)
题意 给出n个点的坐标,它们只能往上.下.左.右一格一格地移动,求使其移动至水平线上的最小步数. 思路 转载 先易后难,对于纵向的问题,我们推个公式,,这个很容易看出是货仓选址问题,k取y[i]的中位 ...
- POJ1723 SOLDIERS 兄弟连
SOLDIERS 有一个性质:在一个长为n的序列a中找一个数 \(a_k\) 使得 \(\sum\limits_{i=1}^n abs(a_i-a_k)\) 最小,则 \(a_k\) 是a的中位数. ...
- POJ1723,1050,HDU4864题解(贪心)
POJ1723 Soldiers 思维题. 考虑y坐标,简单的货舱选址问题,选择中位数即可. 再考虑x坐标,由于直接研究布置方法非常困难,可以倒着想:不管如何移动,最后的坐标总是相邻的,且根据贪心的思 ...
- $Poj1723/AcWing123\ Soldiers$ 排序
$Poj$ $AcWing$ $Description$ $Sol$ 分别处理$x$坐标和$y$坐标.$y$坐标显然很好处理,就是排个序然后取中位数就好了.$x$没有$y$那么直接叭.所以我首先写了个 ...
- OpenJudge/Poj 1723 SOLDIERS
1.链接地址: http://bailian.openjudge.cn/practice/1723/ http://poj.org/problem?id=1723 2.题目: 总时间限制: 1000m ...
- [转]13 Hours: The Secret Soldiers of Benghazi
转:http://www.imfdb.org/wiki/13_Hours:_The_Secret_Soldiers_of_Benghazi The following weapons were use ...
- hdu 4412 Sky Soldiers(区间DP)
Sky Soldiers Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- csu 1749: Soldiers ' Training(贪心)
1749: Soldiers ' Training Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 37 Solved: 18[Submit][Stat ...
- POJ 1723 SOLDIERS
SOLDIERS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 1 ...
随机推荐
- 【学亮IT手记】angularJS+select2多选下拉框实例
永远保持对大部分知识的好奇心,学习从不枯燥,也没有被逼学习一说,乐此不疲才是该有的心态和境界!!! 引入相关js库: html部分代码: angularJS定义数据源变量:
- redis的配置文件解释
redis的守护进行 守护进程(Daemon Process),也就是通常说的 Daemon 进程(精灵进程),是 Linux 中的后台服务进程.它是一个生存期较长的进程,通常独立 于控制终端并且周期 ...
- 关于controller的书写
private Logger log = LoggerFactory.getLogger(ReportFormController.class); // 读取配置文件 ResourceBundle r ...
- spec文件中的 %pre %post %preun %postun
转载http://meinit.nl/rpm-spec-prepostpreunpostun-argument-values RPM has 4 parts where (shell) scripts ...
- 死锁问题分析(个人认为重点讲到了gap间隙锁,解决了我一些不明报死锁的问题)
线上某服务时不时报出如下异常(大约一天二十多次):“Deadlock found when trying to get lock;”. Oh, My God! 是死锁问题.尽管报错不多,对性能目前看来 ...
- js对json解析获取对应属性的值,JSON.stringify()和JSON.parse()
JSON.stringify() 该方法,将一个JSON对象转化为字符串string JSON.parse() 该方法,将一个字符串转化为JSON对象object 对于JSON对象,获取其对应键值 可 ...
- Java调用.NET 的Web Service服务故障排除
参考路径:http://blog.sina.com.cn/s/blog_4c925dca01014y3r.html
- js函数使用prototype和不适用prototype的区别
js中类定义函数时用prototype与不用的区别 原创 2017年06月05日 12:25:41 标签: 函数 / prototype / class 首先来看一个实例: function Li ...
- C# 23种设计模式汇总
创建型模式工厂方法(Factory Method)在工厂方法模式中,工厂方法用来创建客户所需要的产品,同时还向客户隐藏了哪种具体产品类将被实例化这一细节.工厂方法模式的核心是一个抽象工厂类,各种具体工 ...
- How the Microsoft Bot Framework Changed Where My Friends and I Eat: Part 1
Bots are everywhere nowadays, and we interact with them all of the time. From interactions on our ph ...