HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)

题目链接  2017 CCPC Harbin Problem K

题意  给定若干物品,每个物品可以覆盖一个区间。现在要覆盖区间$[1, t]$。

   求选出来的物品的$\frac{∑a_{i}}{∑b_{i}}$的最小值。

首先二分答案,那么每个物品的权值就变成了$x * b_{i} - a_{i}$

在判断的时候先把那些权值为正的物品全部选出来,

然后记录一下从$1$开始可以覆盖到的最右端点的位置。

接下来开始DP,按照区间的端点升序排序(左端点第一关键字,右端点第二关键字)

问题转化为能否用剩下的物品覆盖尚未覆盖的区间。

那么直接用树状数组优化DP就可以了。

#include <bits/stdc++.h>

using namespace std;

#define	rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define	dec(i, a, b)	for (int i(a); i >= (b); --i)
#define	fi		first
#define	se		second

const int N = 1e5 + 10;

struct node{
	int s, t;
	double a, b;
	bool flag;
	void scan(){ scanf("%d%d%lf%lf", &s, &t, &a, &b); }

	friend bool operator < (const node &a, const node &b){
		return a.s == b.s ? a.t < b.t : a.s < b.s;
	}
} a[N];

int T;
int n, m;
double c[N];
double l, r;

void update(int x, double val){
	for (; x; x -= x & -x) c[x] = min(c[x], val);
}

double query(int x){
	if (x == 0) return 0;
	double ret = 1e9;
	for (; x <= m; x += x & -x) ret = min(ret, c[x]);
	return ret;
}

bool check(double x){
	int mx = 0;
	double ss = 0;
	rep(i, 0, m) c[i] = 1e9;

	rep(i, 1, n){
		if (a[i].b * x - a[i].a > 0){
			ss += a[i].b * x - a[i].a;
			a[i].flag = true;

			if (a[i].s - 1 <= mx){
				mx = max(mx, a[i].t);
			}
		}

		else a[i].flag = false;
	}

	if (mx == m) return true;
	update(mx, 0);

	rep(i, 1, n){
		if (!a[i].flag){
			double mi = query(a[i].s - 1);
			update(a[i].t, mi + a[i].a - a[i].b * x);
		}

		else{
			double mi = query(a[i].s - 1);
			update(a[i].t, mi);
		}
	}

	return ss >= query(m);
}

int main(){

	scanf("%d", &T);
	while (T--){
		scanf("%d%d", &n, &m);
		rep(i, 1, n) a[i].scan();
		sort(a + 1, a + n + 1);

		l = 0, r = 1e3 + 10;

		rep(i, 1, 50){
			double mid = (l + r) / 2.00;
			if (check(mid)) r = mid;
			else l = mid;
		}

		printf("%.3f\n", l);
	}

	return 0;

}

原文地址:https://www.cnblogs.com/cxhscst2/p/8861918.html

时间: 2024-12-11 13:43:55

HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)的相关文章

HDU 6447 - YJJ&#39;s Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]

Problem DescriptionYJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination.One day, he is going to travel from city A to southeastern city B. Let us assume th

HDU 5542 - The Battle of Chibi - [离散化+树状数组优化DP]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5542 Problem DescriptionCao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is to have a spy in Cao Cao's army.

hdu 3450 树状数组优化dp

题意就不说了: hdu2227差不多只不过这道题不是递增  而是相邻差不超过d   其实是为都一样,  也有差别: 这道题没说范围  并且树之间的大小关系对结果有影响,所以树状数组里根据原来id来求,由于数值很大 所以还是用到离散化  这里的离散化感觉和映射差不多,离散化用来查找id: 当num[i]的id是x是,dp[i]表示方案数  很明显dp[i]=sun(dp[j],j<i&&abs(num[j]-num[i])<=d)  这里在树状数组里面就是求sum[1到num[i

HDU - 5542 The Battle of Chibi(LIS+树状数组优化)

The Battle of Chibi Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is to have a spy in Cao Cao's army. But all generals and soldiers of Cao Cao were loya

HDU 4455 Substrings --递推+树状数组优化

题意: 给一串数字,给q个查询,每次查询长度为w的所有子串中不同的数字个数之和为多少. 解法:先预处理出D[i]为: 每个值的左边和它相等的值的位置和它的位置的距离,如果左边没有与他相同的,设为n+8(看做无穷). 考虑已知w=k的答案,推w = k+1时,这时每个区间都将增加一个数,即后n-k个数会增加到这些区间中,容易知道,如果区间内有该数,那么个数不会加1,,即D[i] > k时,结果++,即查询后n-k个数有多少个D[i] > k 的数即为要加上的数,然后最后面还会损失一个区间,损失的

ACM学习历程—51NOD 1685 第K大区间2(二分 &amp;&amp; 树状数组 &amp;&amp; 中位数)

http://www.51nod.com/contest/problem.html#!problemId=1685 这是这次BSG白山极客挑战赛的E题. 这题可以二分答案t. 关键在于,对于一个t,如何判断它是否能成为第k大. 将序列中大于t的置为1,小于t的置为-1,等于t的置为0.那么区间中位数大于t的和就大于0,小于t的就小于0.于是就是判断区间和大于0的个数是否小于等于k. 维护前缀和sum(i),然后统计之前sum(j)小于sum(i)的有多少个,就是以i为右值的区间和大于0的个数.于

2017 CCPC 哈尔滨站 HDU 6242

Geometry Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1091    Accepted Submission(s): 208Special Judge Problem Description Alice is interesting in computation geometry problem recen

POJ3111 K Best —— 01分数规划 二分法

题目链接:http://poj.org/problem?id=3111 K Best Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 11380   Accepted: 2935 Case Time Limit: 2000MS   Special Judge Description Demy has n jewels. Each of her jewels has some value vi and weight wi.

hdu 2227 dp+树状数组优化

很容易可以想到状态转移方程: dp[i] = segma: dp[j] ( j < i && a[j] <= a[i] ), 其中dp[i]表示以a[i]结尾的不降子序列的个数. 但是n非常大,n^2的算法必然TLE,仔细想想其实式子右边是一个区间和的形式,即小于等于a[i]的a[j]的dp[j]的和,所以可以将a[i]离散化成t后将dp[i]的值存在t位置上,每次求解dp[i]就是查询一个前缀和,可以用树状数组来维护. 举个例子:对于1,50,13,34这四个数,离散化后变成