杭电多校:权值线段树

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6637

题目大致意思:输入n行数据,第i行数据有两个值,分别为ai和bi,每一行都可以定义一个xi(0<=xi<=1),使得ai的值变成ai*xi,而bi的值变成bi*(1-xi),然后定义到第k行的答案为max(第一行到第k行处理之后ai的值,处理之后bi的值),而且你要使得这个max要最小。

Sample Input
1
3
1 3
3 1
2 2

Sample Output
3/4
1/1
2/1

解法:可以使用优先队列来维护,可以参考上一篇文章:https://blog.csdn.net/weixin_44911598/article/details/98844433

也可以使用权值线段树来维护,因为我们遍历的时候是按照编号来遍历的,但是寻找值的时候是按照ai/(ai+bi)的权值来寻找值的,而且每个的cnt可以定义为(ai+bi),sum就定义为ai。我们可以把问题转换成去前面寻找第now_b个最小值,用now_pos来记录找到的哪个第now_b个最小值,使用now_val来记录找到这个第now_b个最小值剩下多少,query函数返回的就是前面sum的所有的值,记为res。那么我们的答案就是(B[now_pos].a*now_val+res*B[now_pos].s)/B[now_pos].s。B数组是A数组离散之后的按照ai/(ai+bi)为权值排序的一个数组。

代码如下:

#include<bits/stdc++.h>
#define FRD freopen("in.txt", "r", stdin)
#define __ctz(x) __builtin_ctz(x)               //返回二进制下末尾有几个零
#define __ffs(x) __builtin_ffs(x)               //返回二进制下最后一个1的位置,从1开始
#define ms(x) memset(x,0,sizeof(x))
#define all(x) x.begin(),x.end()
#define pb(x) push_back(x)
#define lson (id<<1)
#define rson (id<<1|1)
#define mid ((l+r)>>1)
using namespace std;
typedef long long int LL;
const int N = 3e5;
LL n;
struct Node{
	LL a, b, s;
	int pos1, pos2;
} A[N], B[N];
bool cmp1(const Node &A, const Node &B) {
	return A.pos1 < B.pos1;
}
bool cmp2(const Node &A, const Node &B) {
	return A.a*B.s < B.a*A.s;
}
LL cnt[N*400], sum[N*400];
void push_up(int id) {
	cnt[id] = cnt[lson]+cnt[rson];
	sum[id] = sum[lson]+sum[rson];
}
void build(int id, int l, int r) {
	cnt[id] = sum[id] = 0;
	if (l == r) return ;
	build(lson,l,mid);
	build(rson,mid+1,r);
}
void update(int pos2, int pos1, int l, int r, int id) {
	if (l == r) {
		cnt[id] += A[pos1].s;
		sum[id] += A[pos1].a;
		return ;
	}
	if (pos2 <= mid) {
		update(pos2,pos1,l,mid,lson);
	} else {
		update(pos2,pos1,mid+1,r,rson);
	}
	push_up(id);
}
inline LL read() {
    char ch = getchar();
    int x = 0, f = 1;
    while(ch < '0' || ch > '9') {
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while('0' <= ch && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
LL now_pos = 0, now_val = 0;
LL query(LL C, int l, int r, int id) {
	if (l == r) {
		now_pos = l;
		now_val = C;
		return 0;
	}
	if (cnt[lson] < C) {
		return sum[lson]+query(C-cnt[lson],mid+1,r,rson);
	} else {
		return query(C,l,mid,lson);
	}
}
int main() {
    #ifndef ONLINE_JUDGE
        int startTime = clock();
        FRD;
        freopen("o2.txt", "w", stdout);
    #endif
    int T; scanf("%d", &T);
	while (T--) {
		n = read();
		for (int i = 1; i <= n; i++) {
			scanf("%lld%lld", &A[i].a,&A[i].b);
			A[i].s = A[i].a+A[i].b;
			A[i].pos1 = i;
		}
		sort(A+1,A+n+1,cmp2);
		for (int i = 1; i <= n; i++) {
			A[i].pos2 = i;
			B[i] = A[i];
		}
		sort(A+1,A+n+1,cmp1);
		build(1,1,n);
		LL now_b = 0;
		for (int i = 1; i <= n; i++) {
			now_b += A[i].b;
			update(A[i].pos2,i,1,n,1);
			LL res = query(now_b,1,n,1);
			now_val *= B[now_pos].a;
			LL now_max = B[now_pos].s;
			LL gcd = __gcd(now_val,now_max);
			now_val /= gcd, now_max /= gcd;
			printf("%lld/%lld\n", res*now_max+now_val,now_max);
		}
	}
    #ifndef ONLINE_JUDGE
        printf("Time = %dms\n", clock() - startTime);
    #endif
    return 0;
}

代码总结:权值线段树在query找到(l==r)的时候肯定是第K小的值,所以在(l==r)的时候我们就可以记录下第K下值的时候的状态now_val=C和now_pos=l,找到第K小的时候剩下多少的值和第在离散之后的位置K下。关于结构题的离散化,不能使用map来离散,容易出问题,需要自己手动写一个离散化的代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值