HDU 4791 Alice ' s Print Service

Source: Internet
Author: User
Tags blank page

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4791

Surface:

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 Descriptionalice 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 was printing less than pages are cents per page, but when printing wasn't less than pages, You just need-pay is only cents per page. It's easy-to-figure-out-if-you-want to print-pages, the best choice are to print a extra blank page so that the MO Ney need to pay is 100x10 cents instead of 99x20 cents.
Now given the description of pricing strategy and some queries, your task was to figure out the best ways to complete those Queries in order to save money.
Inputthe first line contains a integer T (≈10) which is the number of test cases. Then T cases follow.
Each case contains 3 lines. The first line contains the 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 was printing no less than Si, and less than SI+1 pages are PI cents per page (for i=1..n-1). The price was printing no less than SN pages are PN cents per page. The third line containing M integers Q1. QM (0≤QI≤109) is the queries.
Outputfor each query qi, your should output the minimum amount of money (in cents) to pay if you want to print Qi pages, on E output in one line.
Sample Input
12 30 20 100 100 99 100

Sample Output
010001000

Source2013 Asia Changsha Regional Contest


Test instructions

The more you print, the more favorable the price range (and possibly the same). Asked given the number of times N, printing how many of the most economical, that is, although more printing, but the unit price is low, so the whole price is naturally low. Ask how much it cost at the end.


Solving:

The minimum cost of each interval is preprocessed first, that is, its critical value multiplied by the unit price, and then ordered from small to large. For each set of samples, calculate the cost of it within its own interval, and then use the dichotomy (directly using the Upper_bound function) to find the last position p that is less than its cost. The minimum spend array is traversed starting at 0 until p. If the traversal process is found, there is a legal interval, that is, the interval corresponding to the number of sheets is greater than the number of itself, then stop traversing, output the current cost. In fact, this kind of writing, is water over it (700ms), the complexity is relatively high.

The better solution is to preprocess the optimal value of each interval, and then each input a number, as long as the number belongs to which interval can be found. (The meaning of Minn refers to the optimal value of the print critical tensor in the interval range) and the pretreatment process is minn[i]=min (minn[i],minn[i+1]); The initial value of Minn[i] is the cost of printing the critical tensor directly in that interval. I can traverse it from large to small. Because the number of i+1 is greater than the number of I, so minn[i] can be directly taken, minn[i+1] value, and because Minn[i] is from large to small, minn[i+1] save is that is the back of all the best values.


Code:

#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 longusing 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\n", 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\n", 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\n", Val); }}}}return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 4791 Alice ' s Print Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.