NEERC 2004 Western Sub(spoj417 or poj 2970)

PRO IS HERE



就是校赛的题目啊,有木有。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<queue>

using namespace std;

#define FOR(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define nMax 101000
#define inf 0x7fffffff
#define LL long long
#define pb push_bacp
#define DB double
#define eps 1e-8
#define mp make_pair
#define xx first
#define yy second
struct BB{
	int a,b,d;
	void read(){
		scanf("%d%d%d",&a,&b,&d);
	}
	friend bool operator < (const BB& a,const BB& b) {
		if(a.d == b.d) return a.a > b.a;
		return a.d < b.d;
	}
}a[nMax];

typedef pair<int,double> Htp;
struct HEAP{
	Htp heap[nMax];
	int Count,root;
	void clear(){
		Count = 0,root=1;
	}
	void up(int x){
		Htp s = heap[x];
		for(int i=(x>>1);i>=1;i>>=1){
			if(heap[i]<s) {
				heap[x]=heap[i];
				x=i;
			}else break;
		}
		heap[x] = s;
	}
	void down(int x) {
		Htp s = heap[x];
		for(int i=(x<<1);i<=Count;i<<=1){
		    i += (i < Count && heap[i] < heap[i+1]);
			if(heap[i]>s) {
				heap[x] = heap[i];
				x = i;
			}else break;
		}
		heap[x] = s;
	}
	Htp top(){
		return heap[root];
	}
	void push(Htp p){
		heap[++Count] = p;
		up(Count);
	}
	void pop(){
		heap[root] = heap[Count];
		Count --;
		down(root);
	}
};

HEAP que;
//priority_queue<Htp> que;
int main(){
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	int Cas,n;
	scanf("%d",&Cas);
	while(Cas--){
        scanf("%d",&n);
        que.clear();
        //while(!que.empty()) que.pop();
		FOR(i,0,n-1) a[i].read();
		sort(a,a+n);
		DB T = 0;
		double ans = 0;
		FOR(i,0,n-1){
			T += a[i].b;
			que.push(mp(a[i].a,(double)a[i].b/a[i].a));
			while(T>a[i].d){
				Htp s = que.top();que.pop();
				if(s.xx * s.yy > T - a[i].d){
					ans += (T-a[i].d)/s.xx;
					que.push(mp(s.xx,s.yy-(T-a[i].d)/s.xx));
					T = a[i].d;
				}else {
					ans += s.yy;
					T -= s.xx * s.yy;
				}
			}
		}
		printf("%.2f\n",ans);
	}

	return 0;
}

维护一个最大堆即可,中间的各种计算开始弄的头晕,后来还是A了

比较了STL 和 自己的堆

STL:

MyHEAP

还是手写的快一点。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值