HDU 4791 Alice'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 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 ≤ 10
5 ). The second line contains 2n integers s
1, p
1 , s
2, p
2 , ..., s
n, p
n (0=s
1 < s
2 < ... < s
n ≤ 10
9 , 10
9 ≥ p
1 ≥ p
2 ≥ ... ≥ p
n ≥ 0).. The price when printing no less than s
i but less than s
i+1 pages is p
i cents per page (for i=1..n-1). The price when printing no less than s
n pages is p
n cents per page. The third line containing m integers q
1 .. q
m (0 ≤ q
i ≤ 10
9 ) are the queries.
 
Output
For each query q
i, you should output the minimum amount of money (in cents) to pay if you want to print q
i pages, one output in one line.
 
Sample Input

   
   
   
   
1 2 3 0 20 100 10 0 99 100

 
Sample Output

   
   
   
   
0 1000 1000

 
Source
2013 Asia Changsha Regional Contest
제목:
    인쇄 장 수가 많 을 수록 가격 구간 이 할인 된다.주어진 장 수 를 n 으로 할 때 몇 장 을 인쇄 하 는 것 이 가장 경제적 이 냐 고 물 었 다. 즉, 인쇄 가 많 지만 단가 가 낮 기 때문에 전체 가격 도 자연히 낮 아진 다.마지막 에 얼마 쓰 냐 고.
문제 풀이:
    먼저 각 구간 의 최소 비용, 즉 임계값 에 단 가 를 곱 한 다음 에 어 릴 때 부터 줄 을 서 는 것 이다.각 그룹의 사례 에 대해 서 는 그 자체 구간 에서 의 비용 을 계산 한 후 이분법 (upper bound 함수 로 직접 사용) 으로 그 비용 보다 적은 마지막 위치 P 를 찾 습 니 다.P 까지 최소 소비 배열 을 0 부터 옮 겨 다 닙 니 다.옮 겨 다 니 는 과정 에서 합 법 적 인 구간, 즉 이 구간 의 대응 장 수 는 그 자체 의 장수 보다 크 면 옮 겨 다 니 는 것 을 멈 추고 현재 대 가 를 출력 합 니 다.사실 이렇게 쓰 면 물이 지나 간 셈 이지 (700 ms) 복잡 도가 비교적 높다.
    비교적 좋 은 해법 은 각 구간 의 최 적 치 를 미리 처리 한 다음 하나의 수 를 입력 할 때마다 이 수가 어느 구간 에 속 하 는 지 찾 으 면 된다.(minn 의 의 미 는 이 구간 범위 내 에서 임계 장 수 를 인쇄 하 는 최 적 화 된 값 을 말 합 니 다) 예비 처리 과정 은 minn [i] = min (minn [i], minn [i + 1] 입 니 다.minn [i] 의 초기 값 은 이 구간 에서 임계 장 수 를 직접 인쇄 하 는 대가 입 니 다.i. 큰 것 부터 작은 것 까지 한 번 옮 겨 다 니 면 됩 니 다.i + 1 의 장 수 는 i 의 장수 보다 크기 때문에 minn [i] 는 'minn [i + 1' 의 값 을 직접 취 할 수 있 고 minn [i] 은 큰 것 에서 작은 것 으로 구 할 수 있 기 때문에 minn [i + 1] 은 뒤의 모든 최 적 치 를 저장 합 니 다.
코드:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <cstdlib>
#include <set>
#include <algorithm>
#include <string>
#include <iomanip>
#define LL long long
using namespace std;
struct cost
{
	int num;
	long long minn;
}store[100010];
bool cmp(cost a,cost b)
{
	return a.minn<b.minn;
}
LL mini[100010];
int main()
{
	int storep[100010],stores[100010];
	int t,n,m,p,tmp;
	bool flag;
	LL val;
    scanf("%d",&t);
	while(t--)
	{
      scanf("%d%d",&n,&m);
	  for(int i=0;i<n;i++)
	  {
		  scanf("%d%d",&stores[i],&storep[i]);
		  store[i].num=i;
		  store[i].minn=1LL*storep[i]*stores[i];
	  }
	  sort(store,store+n,cmp);
	  for(int i=0;i<n;i++)
	  {
		  mini[i]=store[i].minn;
	  }
	  for(int i=0;i<m;i++)
	  {
		  scanf("%d",&tmp);
		  if(tmp>=stores[n-1])
		  {
			  printf("%lld
",1LL*tmp*storep[n-1]); } else { flag=false; p=lower_bound(stores,stores+n,tmp)-stores; //cout<<"storep: "<<storep[p]<<endl; val=1LL*storep[p-1]*tmp; // for(int i=0;i<p+1;i++) //cout<<"price: "<<storep[i]<<endl; //cout<<"val: "<<val<<endl; p=upper_bound(mini,mini+n,val)-mini; for(int i=0;i<p;i++) { if(stores[store[i].num]>=tmp) { flag=true; printf("%lld
",store[i].minn); break; } } if(!flag) { if(stores[store[p].num]>=tmp&&mini[p]<=val) { flag=true; printf("%lld",store[p].minn); } } if(!flag) { printf("%lld
",val); } } } } return 0; }

좋은 웹페이지 즐겨찾기