hdu4791-Alice's Print Service
题目链接: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 + 5; 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 = 1;i <= n;i++) { scanf("%lld%lld",&s[i],&p[i]); ps[i] = s[i]*p[i]; } minn[n+1] = 1ll<<50;//就是因为这 for(int i = n;i >= 1;i--) minn[i] = min(ps[i],minn[i+1]); long long sum; for(int i = 1;i <= m;i++) { scanf("%lld",&q); int pos = lower_bound(s+1,s+n+1,q) - s;//返回大于或等于val的第一个元素位置 if(pos == 1) { printf("0\n"); continue; } if(s[pos] == q) sum = min(q*p[pos],minn[pos]); else sum = min(q*p[pos-1],minn[pos]); printf("%lld\n",sum); } } return 0; }