NYOJ 933 Bob's Print Service
生活随笔
收集整理的這篇文章主要介紹了
NYOJ 933 Bob's Print Service
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Bob's Print Service
時(shí)間限制:1000?ms ?|? 內(nèi)存限制: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.
題意:先給出兩個(gè)整數(shù)n、m,然后給出n個(gè)區(qū)間的臨界點(diǎn)和大于這個(gè)臨界點(diǎn)的每張紙的價(jià)格。然后給出m個(gè)k,問打印紙張數(shù)不低于k的最小花費(fèi)。
解題思路:先預(yù)處理一下,用一個(gè)數(shù)組v記錄處理后的結(jié)果,v[i]表示打印張數(shù)不低于臨界點(diǎn)s[i]時(shí)所花費(fèi)的最小的錢數(shù),然后二分查找出不低于要查找的紙張數(shù)的位置a,求出打印k張本該支付的錢數(shù)ans=k*p[a-1],將ans與記錄的v[a]比較,求出的最小值即為最小花費(fèi)。
#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;//二分查找,返回查找元素的最后一個(gè)可安插位置,即“元素值>查找值”的第一個(gè)元素的位置LL ans = k * p[a-1];if(a < n)ans = min(ans, v[a]);printf("%lld\n",ans);}}return 0; }總結(jié)
以上是生活随笔為你收集整理的NYOJ 933 Bob's Print Service的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NYOJ 875 小M的操作数
- 下一篇: 说说私域流量