NYOJ 933 Bob's Print Service
生活随笔
收集整理的這篇文章主要介紹了
NYOJ 933 Bob's Print Service
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Bob's Print Service
時間限制:1000?ms ?|? 內存限制:65535?KB 難度:3 描述For example,the price when printing lwss than 100 pages is 20 cents per page,but when printing not less than 100 pages,you just?need to pay only 10 cents 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. 輸入
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 S1,P1,S2,P2,……,Sn,Pn(0 = S1 < S2 < …… < Sn <= 10^9 ,
10^9 >= P1 >= P2 …… >= Pn >= 0).
The price when printing no less that S(i) but less that S(i+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 <= 10^9) are the queries.
題意:先給出兩個整數n、m,然后給出n個區間的臨界點和大于這個臨界點的每張紙的價格。然后給出m個k,問打印紙張數不低于k的最小花費。
解題思路:先預處理一下,用一個數組v記錄處理后的結果,v[i]表示打印張數不低于臨界點s[i]時所花費的最小的錢數,然后二分查找出不低于要查找的紙張數的位置a,求出打印k張本該支付的錢數ans=k*p[a-1],將ans與記錄的v[a]比較,求出的最小值即為最小花費。
#include<cstdio> #include<algorithm> using namespace std; typedef long long LL; const int N = 1e5 + 10; LL s[N], p[N], v[N]; int main() {int t, m, n, i, k;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);for(i = 0; i < n; i++)scanf("%lld%lld",&s[i],&p[i]);for(i = n-1; i >= 0; i--){if(i == n-1)v[i] = s[i] * p[i];elsev[i] = min(v[i+1], s[i] * p[i]);}while(m--){scanf("%d",&k);int a = upper_bound(s, s+n, k) - s;//二分查找,返回查找元素的最后一個可安插位置,即“元素值>查找值”的第一個元素的位置LL ans = k * p[a-1];if(a < n)ans = min(ans, v[a]);printf("%lld\n",ans);}}return 0; }總結
以上是生活随笔為你收集整理的NYOJ 933 Bob's Print Service的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NYOJ 875 小M的操作数
- 下一篇: 说说私域流量