HDU 4791 Alice's Print Service(2013长沙区域赛现场赛A题)

时间:2023-03-09 19:13:27
HDU 4791 Alice'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 * px,要么多打印一点,多打印的时候可以确定打印的张数一定是x区间后面的某个区间的左端点的张数,因为已经超出我要打印的张数了,所以任意一个区间的中间的数量一定会大于这个区间的左端点的数量,所以,现在我要决定的就是打印多少张了,很显然,打印的张数要么是我现在输入的k张,要么是某个区间的左端点的值.但因为数据是10^5,所以我可以从后往前扫一遍,就可以确定这个可能导致费用最低的区间是哪个区间.然后就是确定k应该属于哪个区间的时候用二分查找就行了.

 #include <cstdio>
#include <cstring>
#include<iostream>
#include <algorithm> using namespace std;
#define LL __int64//待会记得改
#define maxn 100010
struct node
{
LL tot,p;
LL s1,s2;
int next;
}all[maxn];
int find_s1(LL d,node* temp,int l,int r)
{
while(l < r)
{
int mid = (l + r) / ;
if(d <= temp[mid].s1) r = mid;
else if(d > temp[mid].s1) l = mid + ;
}
if(!(d >= temp[l].s1 && d < temp[l].s2)) return l-;
return l;
}
LL Min(LL a,LL b)
{
return a <= b? a:b;
}
LL Max(LL a,LL b)
{
return a >= b? a:b;
}
int main()
{
// freopen("in","r",stdin);
int T,n,m;
scanf("%d",&T);
LL d,s = ,t1,t2;
while(T--)
{
scanf("%d%d",&n,&m);
scanf("%I64d",&s);
for(int i = ;i < n-;++i)
{
scanf("%I64d%I64d",&all[i].p,&all[i].s2);
all[i].s1 = s;
all[i].tot = all[i].s1 * all[i].p;
s = all[i].s2;
}
all[n-].s1 = s;
all[n-].s2 = 0x7fffffff;
scanf("%I64d",&all[n-].p);
all[n-].tot = all[n-].p * all[n-].s1;
all[n-].next = n;
all[n] = all[n-];
int pp = n;
for(int i = n-;i >= ;--i)
{
all[i].next = pp;
if(all[pp].tot > all[i].tot) pp = i;
}
for(int i = ;i < m;++i)
{
scanf("%I64d",&d);
int q = find_s1(d,all,,n-);
t1 = all[q].p * d;
if(all[q].f != n-) //前n-1个
{
t2 = all[all[q].next].p * Max(all[all[q].next].s1,d);
printf("%I64d\n",Min(t1,t2));
}
else printf("%I64d\n",t1); //第n个的情况
}
}
return ;
}