题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4791
题目解释:给你一组数据s1,p1,s2,p2...sn,pn,一个数字q,问你当要打印q张资料时,最少花费是多少?值得注意的是p1>p2>p3>...>pn,就是因为看清这个条件,走了很多弯路。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std; const int N = 1e5 + ;
long long s[N],p[N],ps[N],minn[N],q;
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i = ;i <= n;i++)
{
scanf("%lld%lld",&s[i],&p[i]);
ps[i] = s[i]*p[i];
}
minn[n+] = 1ll<<;//就是因为这
for(int i = n;i >= ;i--)
minn[i] = min(ps[i],minn[i+]);
long long sum;
for(int i = ;i <= m;i++)
{
scanf("%lld",&q);
int pos = lower_bound(s+,s+n+,q) - s;//返回大于或等于val的第一个元素位置
if(pos == )
{
printf("0\n");
continue;
}
if(s[pos] == q)
sum = min(q*p[pos],minn[pos]);
else
sum = min(q*p[pos-],minn[pos]);
printf("%lld\n",sum);
}
}
return ;
}