您的位置:首页 > 其它

The 2013 ACM-ICPC Asia Changsha Regional Contest - A

2013-11-24 19:14 399 查看
题意就是:给出你打印东西在每个范围内的单价,求出打印东西最小的花费。O(n^2)的算法会超时,又用到了二分查找将复杂度降为了n*log(n)。

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 printing less than 100 pages is 20 cents per page, but when printing not less than 100 pages, you just need to pay only 10 cents per page. It's easy to figure out that if you want to print 99 pages, the best choice is to print an
extra blank page so that the money you need to pay is 100 × 10 cents instead of 99 × 20 cents.

Now given the description of pricing strategy and some queries, your task is to figure out the best ways to complete those queries in order to save money.


Input

The first line contains an integer T (≈ 10) which is the number of test cases. Then T cases follow.

Each case contains 3 lines. The first line contains two integers n, m (0 < n, m ≤ 105). The second line contains 2n integers s1, p1, s2, p2, ..., sn, pn (0=s1 <
s2 < ... < sn ≤ 109, 109 ≥ p1 ≥ p2 ≥ ... ≥ pn ≥ 0). The price when printing no less than si but less than si+1 pages is pi cents
per page (for i=1..n-1). The price when printing no less than sn pages is pn cents per page. The third line containing m integers q1 .. qm (0 ≤ qi ≤
109) are the queries.


Output

For each query qi, you should output the minimum amount of money (in cents) to pay if you want to print qi pages, one output in one line.


Sample Input

12 30 20 100 100 99 100


Sample Output

010001000

#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#define LL long long
#define INF 1e9+1000

using namespace std;

LL c[1000010], s[1000010], cnt[1000010];
int main()
{
LL t, n, m, i;
scanf("%lld",&t);
while(t--)
{
scanf("%lld %lld",&n, &m);
for(i = 1; i <= n; i++)
{
scanf("%lld %lld",&s[i], &c[i]);
cnt[i] = s[i]*c[i];
}
cnt[n+1] = INF*INF;
for(i = n; i >= 2; i--)
{
cnt[i] = min(cnt[i], cnt[i+1]);
}
LL p, sum, l, r, mid;
while(m--)
{
scanf("%lld",&p);
l = 1, r = n+1;
mid = (l+r)/2;
while(l < r)//二分一定要注意边界;
{
if(s[mid] < p)
l = mid+1;
else if(s[mid] > p)
r = mid;
else
{
mid ++;
break;
}
mid = (l+r)/2;
}
mid --;
sum = c[mid]*p;
if(sum > cnt[mid+1] && p <= s
)
sum = cnt[mid+1];
printf("%lld\n",sum);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM 二分查找