zoj-3726-Alice's Print Service【二分+rmq】

答案就是 q*p[i](i为最大的s[i]<=q的位置,二分),s[i+1]*p[i+1],....s[n]*p[n] 之中最小的

要快速的查找 (i,n)区间的s*p查询手段有很多。这里我用的rmq。

/*
就是一个打印分段收费政策,印的越多,单张价格越低,
输入需要印刷的数量,求最小印刷费用

打印k页的资料,给出n中付费方案,
一次打印超过s1但不超过s2的每页收费p1,
超过s2不超过s3的收费p2.....数据保证
0=s1<s2<...<sn,p1>=p1>=p3>=...>=pn。
接下来m个查询,对于每个查询问最少花多少钱?
例如s1=0 s2=100  p1=20 p2=10 的时候,若要打印99页,
显然直接打印100页要更便宜一点..所以结果是1000..
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#define fmin(a,b) a>b?b:a
using namespace std;
typedef long long int ll;
const int maxs=100009;
const int max_log=18;
ll s[maxs],p[maxs];
ll mn[maxs][max_log];
void init(ll n)
{
	for(ll i=1;i<=n;++i)
		mn[i][0]=s[i]*p[i];
	ll k=(ll)(log(n*1.0)/log(2.0));
	for(ll j=1;j<=k;++j)
		for(ll i=1;i<=n+1-(1<<j);++i)
			if(i+(1<<j)-1<=n)
				mn[i][j]=fmin(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
}
ll get(ll l,ll r)
{
	ll k=(ll)(log((r-l+1)*1.0)/log(2.0));
	return fmin(mn[l][k],mn[r-(1<<k)+1][k]);
}
void solve(ll q,ll n)
{
	ll rb=lower_bound(s+1,s+n+1,q)-s;
//	printf("%lld\n",rb);
	if(s[rb]>q||rb>n) --rb;
	ll res=q*p[rb];
	if(rb+1<=n)
		res=fmin(res,get(rb+1,n));
	printf("%lld\n",res);
}
int main()
{
	ll t;
	ll n,m;
	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld%lld",&n,&m);
		init(n);
		for(ll i=1;i<=n;++i)
			scanf("%lld%lld",s+i,p+i);
		init(n);
		ll q;
		while(m--)
		{
			scanf("%lld",&q);
			solve(q,n);
		}
	}
	return 0;
} 

zoj-3726-Alice's Print Service【二分+rmq】

时间: 2024-08-12 14:58:33

zoj-3726-Alice's Print Service【二分+rmq】的相关文章

HDU 4791 &amp; ZOJ 3726 Alice&#39;s Print Service (数学 打表)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4791 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5072 Problem Description Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her pr

UVAlive 6611 Alice&#39;s Print Service 二分

Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her print service found some tricks to save money.For example, the price when printing less than 100 pages is 20 cents per page, but when printing not

HDU 4791 &amp;amp; ZOJ 3726 Alice&amp;#39;s Print Service (数学 打表)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4791 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5072 Problem Description Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her pr

2013 ACM/ICPC 长沙现场赛 A题 - Alice&#39;s Print Service (ZOJ 3726)

Alice's Print Service Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her print service found some tricks to save money. For example, the price when

HDU 4791 Alice&#39;s Print Service (2013长沙现场赛,二分)

Alice's Print Service Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1855    Accepted Submission(s): 454 Problem Description Alice is providing print service, while the pricing doesn't seem to

HDU 4791 Alice&#39;s Print Service 水二分

点击打开链接 Alice's Print Service Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1084    Accepted Submission(s): 240 Problem Description Alice is providing print service, while the pricing doesn't

HDU 4791 Alice&#39;s Print Service(2013长沙区域赛现场赛A题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4791 解题报告:打印店提供打印纸张服务,需要收取费用,输入格式是s1 p1 s2 p2 s3 p3...表示打印区间s1到s2张纸的单价是p1,打印区间s2 到s3的单价是p2....最后是sn到无穷大的单价是pn,让你求打印k张纸的总费用最少是多少?有m次查询. 因为s1*p1 > s2 * p2 > s3*p3......,很显然,加入k所在的那个区间是第x个区间,那么最低费用要么是k * p

HDU 4791 Alice&#39;s Print Service

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4791 题面: Alice's Print Service Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1596    Accepted Submission(s): 380 Problem Description Alice is pr

HDU 4791 Alice&#39;s Print Service 简单DP

连接:http://acm.hdu.edu.cn/showproblem.php?pid=4791 题意:打印问题,n次条件,打印量≥si时,每张纸的打印价格为pi(0≤n≤1e5),问打印m次询问,qi张时最少需要多少钱(0≤m≤1e5). 思路:如果对每次询问进行便利复杂度O(m*n)太大,超时.所以进行离线处理,将询问排序,从小到大依次处理,处理过程O(n+m),但排序过程是O(mlogm),所以总体的复杂度还是O(mlogm). 代码: #include <iostream> #inc

hdu 4791 Alice&#39;s Print Service (DP+离线处理)

Alice's Print Service Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1163    Accepted Submission(s): 270 Problem Description Alice is providing print service, while the pricing doesn't seem to