51nod 1110 距离之和最小V3
第1行:点的数量N。(2 <= N <= 10000)
第2 - N + 1行:每行2个数,中间用空格分隔,分别是点的位置及权值。(-10^5 <= X[i] <= 10^5,1 <= W[i] <= 10^5)
输出最小的带权距离之和。
5
-1 1
-3 1
0 1
7 1
9 1
20
中位数的题目,但我是用前缀和做的,从小到大排列,也可以证明在中间时最小
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
typedef long long ll;
struct node
{
ll x,w;
bool operator<(const node &a) const{
return a.x>x;
}
}e[];
int n;
ll sum_front[],sum_last[],l[],r[];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld%lld",&e[i].x,&e[i].w);
sort(e+,e+n+);
for(int i=;i<=n;i++)
{
sum_front[i]=sum_front[i-]+l[i-]*(e[i].x-e[i-].x);
l[i]=l[i-]+e[i].w;
}
for(int i=n;i;i--)
{
sum_last[i]=sum_last[i+]+r[i+]*(e[i+].x-e[i].x);
r[i]=r[i+]+e[i].w;
}
ll ans=;
for(int i=;i<=n;i++)
ans=min(ans,sum_front[i]+sum_last[i]);
printf("%lld\n",ans);
return ;
}
51nod 1110 距离之和最小V3的更多相关文章
- 51Nod 1110 距离之和最小 V3 中位数 思维
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].点P到点P[i]的带权距离 = 实际距离 ...
- 51 Nod 1110距离之和最小V3
1110 距离之和最小 V3 1 秒 131,072 KB 40 分 4 级题 X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].点P到点P[i]的带权距离 = 实际距离 * ...
- 1110 距离之和最小 V3
1110 距离之和最小 V3 基准时间限制:1 秒 空间限制:131072 KB X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].该点到其他点的带权距离 = 实际距离 * ...
- 51Nod 1108 距离之和最小 V2 1096 距离之和最小 中位数性质
1108 距离之和最小 V2基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注三维空间上有N个点, 求一个点使它到这N个点的曼哈顿距离之和最小,输出这个最小 ...
- 51nod1110 距离之和最小 V3
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].该点到其他点的带权距离 = 实际距离 * 权值.求X轴上 ...
- 51nod 1096 距离之和最小【中位数】
1096 距离之和最小 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 X轴上有N个点,求X轴上一点使它到这N个点的距离之和最小,输出这个最小的距离 ...
- 51nod 1096 距离之和最小(水题日常)
基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 X轴上有N个点,求X轴上一点使它到这N个点的距离之和最小,输出这个最小的距离之和. Input 第1行:点的数量 ...
- 51nod 1096 距离之和最小 1108 距离之和最小 V2
[题解] 很显然在一条坐标轴上到各个点距离之和最小的点就是它们的中位数.怎么证明呢?我们假设现在找的某个点x左边有a个点,右边有b个点(a>b).我们把x向左移动d个单位,并保证x左边依然有a个 ...
- 51nod 1096 距离之和最小 思维题,求中位数
题目: 在一条直线上,与两个点距离之和最小的点,是怎样的点? 很容易想到,所求的点在这两个已知点的中间,因为两点之间距离最短. 在一条直线上,与三个点距离之和最小的点,是怎样的点? 由两个点的规律,我 ...
随机推荐
- vc6中关于“新建”
1.windows api 编程:新建→工程→Win32 Application→一个空工程→新建→文件→C++ Source File2.windows mfc 编程:新建→工程→MFC AppWi ...
- rem 自适应布局 bootstrap 移动端适配
移动端适配用:rem 自使用布局用:bootstrap
- List of features and minimum Clang version with support
#1: C++11 Language Feature C++11 Proposal Available in Clang? Rvalue references N2118 Clang 2.9 ...
- Pyhton学习——Day25
#面向对象的几个方法#1.静态方法@staticmethod,不能访问类属性,也不能访问实例属性,只是类的工具包#2.类方法:@classmethod,在函数属性前加上类方法,显示为(cls)代表类, ...
- 注解实战Beforeclass和Afterclass
package com.course.testng;import org.testng.annotations.*; public class BasicAnnotation { //最基本的注解,用 ...
- UVA1583-Digit Generator(紫书例题3.5)
For a positive integer N , the digit-sum of N is defined as the sum of N itself and its digits. When ...
- input输入框获取焦点是背景黄色去除
input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px white inset !important;}
- 命令行 对MYSQL导入sql
1 use database name; //选择使用的数据库 2 mysql>source d:\datafilename.sql 导入sql
- tddl
淘宝根据自己的业务特点开发了TDDL(Taobao Distributed Data Layer 外号:头都大了 ?_Ob)框架,主要解决了分库分表对应用的透明化以及异构数据库之间的数据复制,它是一个 ...
- oracle存储过程中使用execute immediate执行sql报ora-01031权限不足的问题
oracle存储过程中使用execute immediate执行sql报ora-01031权限不足的问题 学习了:http://blog.csdn.net/royzhang7/article/deta ...