Now you need to print something, for example, 99 sheets of paper, 10 RMB for each piece of printing less than 100 sheets, 5 RMB for each piece of 100 sheets and more than 100 sheets, in this case, you can print up to 100 images to reduce the cost. Now, a number N is given, indicating N segments, and S1, P1, S2, p2 ...... SN, Pn, indicates that when the printed paper is greater than or equal to S1 and less than S2, each paper charges P1 yuan. There are m inquiries, asking you to give you x sheets of paper each time, what is the minimum cost.
Idea: An O (n) pre-processing can be performed from the back to indicate the minimum cost of Multi-print paper when the required paper is smaller than SI. From the back to the front, Min [Si] = min (SI * Pi, Min [Si + 1]), after that, you only need to compare min [Si] with the normal method to minimize the charges.
Because the output is large, 10 ^ 6 is determined by cout, and printf is required.
#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 100100#define eps 1e-7#define INF 0x7FFFFFFF#define seed 131//#define long long ll;#define unsigned long long ull;#define lson l,m,rt<<1#define rson r,m+1,rt<<1|1long long s[MAXN],p[MAXN];long long minm[MAXN];int main(){ long long n, m, i, j; long long t; long long x,ans; scanf("%I64d",&t); while(t--){ scanf("%I64d%I64d",&n,&m); for(i=0;i<n;i++){ scanf("%I64d%I64d",&s[i],&p[i]); } minm[n-1] = s[n-1] * p[n-1]; for(i=n-2;i>=0;i--){ long long tt = s[i] * p[i]; minm[i] = min(tt,minm[i+1]); } for(i=0;i<m;i++){ scanf("%I64d",&x); int pp = upper_bound(s,s+n,x)-s; pp--; if(pp==n-1){ ans = x * p[n-1]; } else ans = min(minm[pp+1],x*p[pp]); printf("%I64d\n",ans); } } return 0;}/*12 1000 20 100 109993 1000 20 100 10 1000 2*/