hdu 3015
这个题给你一堆树,每棵树的位置x和高度h都给你
f[i]代表这棵树的位置排名,s[i]代表这棵树的高度排名
问你任意两棵树的(f[i] - f[j])*min(s[i],s[j])和
(f[i]-f[i-1])*min(s[i],s[i-1]) + (f[i]-f[i-2])*min(s[i],s[i-2])
首先看看暴力,肯定过不去,毕竟n2复杂度
看这个式子,其实比s[i]大的那么多项都能合并
合并成 ((f[i] - f[i-1])+ (f[i] - f[i-2]) +.....)*s[i]
所以只要在尽快的时间内求出(f[i] - f[i-1] + f[i] - f[i-2] ........)
f[i]是第i棵数的坐标排名
基本就是区间求和 求出小于f[i]的有多棵树 求出小于f[i]的树的坐标和
创建两个树状数组,非别维护坐标和 and 前面树的数量
复杂度就是nlogn了
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e6+;
struct node
{
long long x,h;
int p1,p2;
} arr[maxn];
int c[maxn];
int d[maxn];
int n;
bool cmp2(node a,node b);
bool cmp1(node a,node b);
long long getsumd(int i);
long long getsumc(int i);
void addc(long long i,long long j);
void addd(long long i,long long j);
int main()
{
int m,i,j,k;
while (scanf("%d",&n) != EOF)
{
memset(c, , sizeof(c));
memset(d, , sizeof(d));
for(i=;i<=n;++i)
scanf("%lld%lld",&arr[i].x,&arr[i].h);
sort(arr+,arr+n+,cmp1);
arr[].p1 = ;
for(i=;i<=n;++i)
{
if(arr[i].x == arr[i-].x)
arr[i].p1 = arr[i-].p1;
else
arr[i].p1 = i;
}
sort(arr+,arr++n,cmp2);
arr[].p2 = ;
for(i=;i<=n;++i)
{
if(arr[i].h == arr[i-].h)
arr[i].p2 = arr[i-].p2;
else
arr[i].p2 = i;
}
for(i=;i<=n;++i)
{
addc(arr[i].p1,arr[i].p1);
addd(arr[i].p1, );
}
long long maxnn=;
for(i=;i<=n;++i)
{
long long k1 = (getsumd(arr[i].p1))*arr[i].p1 - getsumc(arr[i].p1);
//printf("%lld %lld\n",getsumc(n)-getsumc(arr[i].p1),(getsumd(n) - getsumd(arr[i].p1))*arr[i].p1);
k1 += (getsumc(n)-getsumc(arr[i].p1)) - (getsumd(n) - getsumd(arr[i].p1))*arr[i].p1; maxnn+=k1*arr[i].p2;
addc(arr[i].p1, -arr[i].p1);
addd(arr[i].p1, -);
}
printf("%lld\n",maxnn);
}
}
long long lowbit(long long k)
{
return k&(-k);
}
void addc(long long i,long long j)
{
while(i<=n)
{
c[i]+=j;
i+=lowbit(i);
}
}
void addd(long long i,long long j)
{
while(i<=n)
{
d[i]+=j;
i+=lowbit(i);
}
}
long long getsumc(int i)
{
long long su = ;
while(i>)
{
su+=c[i];
i-=lowbit(i);
}
return su;
}
long long getsumd(int i)
{
long long su = ;
while(i>)
{
su+=d[i];
i-=lowbit(i);
}
return su;
}
bool cmp1(node a,node b)
{
if(a.x<b.x)
return true;
return false;
}
bool cmp2(node a,node b)
{
if(a.h<b.h)
return true;
return false;
}
hdu 3015的更多相关文章
- Disharmony Trees HDU - 3015
Disharmony Trees HDU - 3015 One day Sophia finds a very big square. There are n trees in the square. ...
- HDU 3015 Disharmony Trees(树状数组)
题意:给你n棵树,每棵树上有两个权值X H 对于X离散化 :3 7 1 5 3 6 -> 2 6 1 4 2 5,对于H一样 然后F = abs(X1-X2) S=min(H1,H2) 求出 ...
- HDU 3015 Disharmony Trees
题解:在路边有一行树,给出它们的坐标和高度,先按X坐标排序.记录排名,记为rankx,再按它们的高度排序,记录排名,记为rankh.两颗树i,j的差异度为 fabs(rankx[i]-rankx[j] ...
- hdu 3015 Disharmony Trees (离散化+树状数组)
Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3015 Disharmony Trees 【 树状数组 】
题意:给出n棵树,给出横坐标x,还有它们的高度h,先按照横坐标排序,则它们的横坐标记为xx, 再按照它们的高度排序,记为hh 两颗树的差异度为 abs(xx[i] - xx[j]) * min(hh[ ...
- Disharmony Trees HDU - 3015 树状数组+离散化
#include<cstdio> #include<cstring> #include<algorithm> #define ll long long using ...
- HDU题解索引
HDU 1000 A + B Problem I/O HDU 1001 Sum Problem 数学 HDU 1002 A + B Problem II 高精度加法 HDU 1003 Maxsu ...
- [欧拉回路] hdu 3018 Ant Trip
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3018 Ant Trip Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5643 King's Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
随机推荐
- guide dpdk
Welcome to DPDK Guide! Contents: Setting up DPDK Important Prerequisites Setting up repositories Red ...
- JTable 查询
public JTable query(String table) throws SQLException { DefaultTableModel tablemodel = new DefaultTa ...
- delphi程序设计改进可读性一法
Delphi,Lazarus程序设计改进一法 作者:steven QQ:1565498246 Delphi/Lazarus有一个思想就是方法.函数名调用,后边可以不使用括号(),比如调用函数Now,这 ...
- libnet 库使用(一)
该库的相关资料主要从源码包中获得(假设当前路径为源码包路径): ./sample 中有代码示例 ./doc/html 中html文件可以通过浏览器打开,参看函数定义 想要的基本上sample中都 ...
- pkg_resources.DistributionNotFound: The 'catkin-pkg==0.4.9' distribution was not found
个人感觉是python2与python3在ros中的差异导致的, 问题一:Traceback (most recent call last): File "/usr/bin/rosdep& ...
- SpringBoot中文乱码解决方案
转载:https://blog.csdn.net/wangshuang1631/article/details/70753801 方法一,修改application.properties文件 增加如下 ...
- Select isnull(,)
类似于 select isnull(...,1) from table 这样的 isnull函数防止查询结果为空,防止接下来需要这个值时可能造成空指针异常
- ueditor 功能定制
方法一:用js传参 var editor = new UE.ui.Editor({initialFrameHeight:200,initialFrameWidth:640,toolbars:[[&qu ...
- hdu 6073
题意: 给出一个二部图,U.V分别是二部图的两个点集,其中,U中每个点会有两条边连到V中两个不同的点. 完美匹配定义为:所有点都成功匹配. 思路:已知一定是完美匹配了呀(也一定存在),我们先把度数为一 ...
- canvas 实现飞碟射击游戏
var canvas = document.getElementById('canvas'); var cxt = canvas.getContext('2d'); // 射击角度 var ang = ...