codechef Taxi Driver
题意:
给N个点求任意两个点的“距离”总和:
A,B的“距离”定义为:min(|ax-bx|,|ay-by|) (n<200000)
好题!
解析:
看着没思路
先是公式化简:让 ax=sx+sy;
ay=sx-sy;
bx=tx+ty;
by=tx-ty;
于是:min(|ax-bx|,|ay-by|)=min(ax-bx,bx-ax,ay-by,by-ay)=min(sx-tx+sy-ty,tx-sx+ty-sy,sx-tx+sy-ty,tx-sx+ty-sy);
=|sx-tx|+|sy-ty|
这里就明显了吧:
sx=(ax+ay)/2 sy=(ax-ay)/2
于是分别按sx,sy排序;
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <vector> #define ll long long
#define N 222222 ll a[N],b[N];
using namespace std; int main()
{
int T;
scanf("%d",&T);
while (T--)
{
int n,c,d;
scanf("%d%d%d",&n,&c,&d);
for (int i=;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
a[i]=(ll) c*x+d*y;
b[i]=(ll) c*x-d*y;
} sort(a+,a+n+);
sort(b+,b+n+); ll ans=;
ll ans1=;
for (int i=;i<=n;i++)
{
ans1+=a[i];
ans+=a[i]*i-ans1;
}
ans1=;
for (int i=;i<=n;i++)
{
ans1+=b[i];
ans+=b[i]*i-ans1;
}
printf("%lld\n",ans>>);
}
return ;
}
codechef Taxi Driver的更多相关文章
- Lesson 29 Taxi!
Text Captain Ben Fawcett has bought an unusual taxi and has begun a new serivice. The 'taxi' is a sm ...
- CF 1100E Andrew and Taxi(二分答案)
E. Andrew and Taxi time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Lyft Level 5 Challenge 2018-Final Round(Open Div.2) B. Taxi drivers and Lyft
http://codeforces.com/contest/1075/problem/B Palo Alto is an unusual city because it is an endless c ...
- ACM: 限时训练题解-Heavy Coins-枚举子集-暴力枚举
Heavy Coins Bahosain has a lot of coins in his pocket. These coins are really heavy, so he always ...
- C++程序结构---1
C++ 基础教程Beta 版 原作:Juan Soulié 翻译:Jing Xu (aqua) 英文原版 本教程根据Juan Soulie的英文版C++教程翻译并改编. 本版为最新校对版,尚未定稿.如 ...
- BBC票选出的100部最经典美国电影,你看过几部?
BBC票选出的100部最经典美国电影,你看过几部? 导读:BBC票选出的100部最经典美国电影,你看过几部? 2015-07-27欧美内参欧美内参欧美内参 微信号zoujinoumei 功能介绍< ...
- 人一生必看的100部电影(全球最佳电影排名榜TOP250)
人一生必看的100部电影(全球最佳电影排名榜TOP250) 人的一生能看多少部电影?假设我们每周都看一部,从10岁看到80岁将会看3640部.但是我们也不可能喜欢这全部的电影.大多数的可能,我们会根据 ...
- (转) Data structures
Data structures A data structure is a group of data elements grouped together under one name. Thes ...
- As3.0 Interface 与类的使用
来源:http://blog.sina.com.cn/s/blog_4d65c19e0100bfkb.html 抽象类:又叫抽象基类:可以包含一般类所包含的所有特性,例如,字段,属性,方法,抽象类不能 ...
随机推荐
- win应用只允许单个实例运行,并将已运行实例窗口置顶
关键词:windows,c++,桌面应用,单个实例,窗口置顶 目标:1.判断本程序是否已有一个实例在运行.2.若有,则激活已在运行的实例(将其窗口置顶),并退出当前运行. 1.使用semaphore来 ...
- docker 的容器入门
Linux Namespace LXC所实现的隔离性主要是来自kernel的namespace, 其中pid, net, ipc, mnt, uts 等namespace将container的进程, ...
- python基础一 day9 函数升阶(2)
def max(a,b): return a if a>b else bprint(max(1, 2)) # 函数进阶# a = 1# def func():# print(a)# func() ...
- nodeJS和npm的环境配置
1.windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native),只需要登陆官网(http://nodejs.org/),便可以看到首页的“INSTALL” ...
- 695. Max Area of Island@python
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- Hibernate-04
HQL查询语法 查询:public class Dome{ Session session = HibernaeUitls.openSession(); Transaction tx = sessio ...
- Mysql when case 批量更新
UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END WHERE id ...
- Oracle 11G RAC 修改IP
实验环境 类别 修改前 修改后 PUBLIC 172.18.4.182 rac1 192.168.56.10 rac1 172.18.4.184 rac2 192.168.56.20 rac2 PRI ...
- Linux基础学习-RHEL7.4之YUM更换CentOS源
1.配置YUM本地源 1.挂载镜像 [root@qdlinux ~]# mount /dev/cdrom /mnt 2.查看是否挂载成功 [root@qdlinux ~]# df -h Filesys ...
- C第10章-----通过引用传递
#include <stdio.h> #include <math.h> void metersToFeetAndInches(double meters,unsigned i ...