POJ1990 MooFest
2024-10-19 19:52:33
题目大意:一群牛参加完牛的节日后都有了不同程度的耳聋(汗……),第i头牛听见别人的讲话,别人的音量必须大于v[i],当两头牛i,j交流的时候,交流的最小声音为max{v[i],v[j]}*他们之间的距离。现在有n头牛,求他们之间两两交流最少要的音量和。
首先我们把v[i]从小到大排序,这样扫的时候max(v[i], v[j]) == v[i] (j < i)了。同时为了重复计算,我们只统计j < i的牛和 i 配对对答案的贡献。
关键是解决距离的绝对值的问题。用两个树状数组维护。
一个求对于当前的 i,dis[j] < dis[i]的牛有几头(cnt),一个维护这些牛的Σval(_sum)。这样对于那些dis[j] < dis[i]的牛,对答案的贡献就是val[i] * (cnt * dis[i] - _sum)。对于那些dis[j] > dis[i]的牛,我们还要开一个全局变量sum,表示当前的Σval[i],那么这这 j 头牛的sum' = sum - _sum, cnt' = (i - 1 - cnt),所以这部分牛对答案的贡献就是val[i] * (sum' - cnt' * dis[i])。最后把两部分相加。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 2e4 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n;
ll sum = , ans = ;
struct Node
{
int val, id;
bool operator < (const Node &oth)const
{
return val < oth.val || (val == oth.val && id < oth.id);
}
}a[maxn]; struct Bit
{
ll c[maxn];
Bit() {Mem(c, );}
int lowbit(int x)
{
return x & -x;
}
void add(int pos, int d)
{
for(; pos < maxn; c[pos] += d, pos += lowbit(pos));
}
ll query(int pos)
{
ll ret = ;
for(; pos; ret += c[pos], pos -= lowbit(pos));
return ret;
}
}C1, C2; int main()
{
n = read();
for(int i = ; i <= n; ++i) a[i].val = read(), a[i].id = read();
sort(a + , a + n + );
for(int i = ; i <= n; ++i)
{
int cnt = C1.query(a[i].id), _sum = C2.query(a[i].id);
C1.add(a[i].id, ); C2.add(a[i].id, a[i].id);
ll tot = cnt * a[i].id - _sum;
tot += sum - _sum - (i - cnt - ) * a[i].id;
ans += tot * a[i].val;
sum += a[i].id;
}
write(ans); enter;
return ;
}
最新文章
- 关于PHP扩展开发(收藏)
- Windows Azure Virtual Network (11) 创建VNet-to-VNet的连接
- java.io.StreamCorruptedException: invalid type code: AC错误的解决方法
- php中mysql数据库异步查询实现
- [译]GotW #6b Const-Correctness, Part 2
- css中关于transform、transition、animate的区别
- Push segues can only be used when the.....
- hdu 4970 Killing Monsters(数学题)
- Win8.1下VM与Hyper-v冲突解决方法
- oracle中事务处理--事务隔离级别
- L264 how cats are psychopaths
- Java基础——可变参数
- 【LeetCode】201. Bitwise AND of Numbers Range
- SSH(Struts,Spring,Hibernate)和SSM(SpringMVC,Spring,MyBatis)的区别
- Json.net 时间格式处理
- Class文件分析
- 小白的Unity5之路(一)
- docker compose,link,Odoo
- 561. Array Partition I【easy】
- java编程--04比较几个常用的日期时间相关类的区别
热门文章
- 获取window.location.href路径参数
- MySql——安装与配置与启动和停止
- laravel框架的rabbitmq使用示例[多队列封装]
- php 中全局变量global 的使用
- 树莓派3(Raspbain系统)安装.net环境
- 浅谈 .NET Framework 与 .NET Core 的区别与联系
- Linux.net &;&; mono
- js获取文件输入框的真实目录
- The Dangers of the Large Object Heap(转载,LOH内存碎片情景重现)
- OLEDB不使用SQL语句直接打开数据表