poj 1990 MooFest & hdu 3015 Disharmony Trees

题目:
poj1990

题意:
计算每两头牛的噪音的和。
两头牛的噪音 = max(牛1噪音,牛2噪音) * 两头牛的距离差

思路:
用两个树状数组,一个记录牛的总数,另一个记录牛的总距离。
把牛按 v 值从小到大排序(v相等时,x不影响噪音总和)
再遍历数组,
计算在点 x 的左边有多少头牛,右边有多少头牛,左边的总距离差,右边的总距离差,
ans += v * (左边的总距离差 + 右边的总距离差),
而左边的总距离差 = x * 左边有多少头牛 - 左边的总距离(1 ~ x 内牛的总距离),
右边的总距离差同理。
再逐个加入树状数组。

ps:ans 会爆 int

题目:
hdu3015

题意:

poj1990的换皮,两棵树的不和谐价值 = min(树1高度,树2高度)* 两棵树的距离差

思路:
poj1990的思路 + 离散化(如果用二分查找来计算 x 和 h 的话会TLE)

poj 1990 代码:

#include <iostream>
#include <algorithm>
#define ll long long
#define lowbit(a) ((a) & (-a))
using namespace std;
const int MAXN = 2e4 + 10;
struct node{
	int v, x;
	friend bool operator < (const node &a, const node &b){
		return a.v < b.v;
	}
}cow[MAXN];
int tree_cnt[MAXN];  //记录区间 1 ~ x 牛的总数 
int tree_dis[MAXN];  //记录区间 1 ~ x 内的牛到 x 的总距离 
int final;
void add(int *p, int x, int d){
	while (x <= final){
		p[x] += d;
		x += lowbit(x);
	}
}
int sum(int *p, int x){
	int cnt = 0;
	while (x > 0){
		cnt += p[x];
		x -= lowbit(x);
	}
	return cnt;
}
int main(){
//    freopen("_in.txt", "r", stdin);
//	freopen("_out1.txt", "w", stdout);
	int n;
	ll l_cnt, r_cnt, l_dis, r_dis, ans = 0;  //在点 x 的左边有多少头牛,右边有多少头牛,左边的总距离差,右边的总距离差
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		scanf("%d%d", &cow[i].v, &cow[i].x);
		final = max(final, cow[i].x);
	}
	sort(cow, cow+n);
	for (int i = 0; i < n; i++){
		l_cnt = sum(tree_cnt, cow[i].x);
		r_cnt = sum(tree_cnt, final) - l_cnt;
		l_dis = cow[i].x * l_cnt - sum(tree_dis, cow[i].x);
		r_dis = sum(tree_dis, final) - sum(tree_dis, cow[i].x) - cow[i].x * r_cnt;
		ans = ans + cow[i].v * (l_dis + r_dis);  //ans += v * (左边的总距离差 + 右边的总距离差) 
		add(tree_cnt, cow[i].x, 1);
		add(tree_dis, cow[i].x, cow[i].x);
	}
	printf("%lld", ans);
    return 0;
}

hdu 3015 代码

#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
#define lowbit(a) ((a) & (-a))
using namespace std;
const int MAXN = 1e5 + 10;
struct node{
	int h, x;
}t[MAXN];
ll tree_cnt[MAXN];  //记录区间 1 ~ x 树的总数 
ll tree_dis[MAXN];  //记录区间 1 ~ x 内的树到 x 的总距离 
int final;
int cmp0 (const node &a, const node &b){
	return a.x < b.x;
}
int cmp1 (const node &a, const node &b){
	return a.h > b.h;
}
void add(ll *p, int x, int d){
	while (x <= final){
		p[x] += d;
		x = x + lowbit(x);
	}
}
ll sum(ll *p, int x){
	ll cnt = 0;
	while (x > 0){
		cnt += p[x];
		x -= lowbit(x);
	}
	return cnt;
}
int main(){
//    freopen("_in.txt", "r", stdin);
//	freopen("_out1.txt", "w", stdout);
	int n;
	ll l_cnt, r_cnt, l_dis, r_dis, ans;  //在点 x 的左边(包括点 x 的位置)有多少头树,右边有多少头树,左边的总距离差,右边的总距离差
	int temp;
	while (~scanf("%d", &n)){
		for (int i = 0; i < n; i++)
			scanf("%d%d", &t[i].x, &t[i].h);
		//离散化 x 
		sort(t, t+n, cmp0);
		temp = t[0].x;
		t[0].x = 1;
		for (int i = 1; i < n; i++)
			if (t[i].x == temp)
				t[i].x = t[i-1].x;
			else{
				temp = t[i].x;
				t[i].x = i + 1;
			}
		final = t[n-1].x;
		//离散化 h 
		sort(t, t+n, cmp1);
		temp = t[n-1].h;
		t[n-1].h = 1;
		for (int i = 1; i < n; i++)
			if (t[n-1-i].h == temp)
				t[n-1-i].h = t[n-i].h;
			else{
				temp = t[n-1-i].h;
				t[n-1-i].h = i + 1;
			}
		ans = 0;
		memset(tree_cnt, 0, sizeof(ll) * (final + 5));
		memset(tree_dis, 0, sizeof(ll) * (final + 5));
		for (int i = 0; i < n; i++){
			l_cnt = sum(tree_cnt, t[i].x);  //如果有树再点 x上,那么 F * S = abs(x-x) * h = 0 * h = 0 ,没有影响 
			r_cnt = sum(tree_cnt, final) - l_cnt;
			l_dis = t[i].x * l_cnt - sum(tree_dis, t[i].x);
			r_dis = sum(tree_dis, final) - sum(tree_dis, t[i].x) - t[i].x * r_cnt;
			ans = ans + t[i].h * (l_dis + r_dis);  //ans += h * (左边的总距离差 + 右边的总距离差) 
			add(tree_cnt, t[i].x, 1);
			add(tree_dis, t[i].x, t[i].x);
		}
		printf("%lld\n", ans);
	}
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值