HDOJ--4791--Alice's Print Service

题意:现在你要打印一些东西,比如需要99张纸,打印100张以下时话费10元每张,100张及100张以上时需要5元每张,此时你可以选择打印100张,使得花费更小。现给一个数字n,表示n个区间段,然后有s1,p1,s2,p2......sn,pn,表示打印纸张大于等于s1而小于s2时,每张纸话费p1元,现有m个询问,问每次给你x张纸,所需的最小花费是多少。

思路:可以从后往前做一个O(n)的预处理,表示需要的纸张少于si时,多打印纸需要的最小花费。从后往前,则min[ si ] = min ( si*pi , min[ si+1 ] ),之后每次查询只需比较min[si]和正常方法的话费取最小即可。

因为输出量较大 10^6 用cout果断T了,需要用printf

#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|1

long 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;
}

/*
1
2 100
0 20 100 10

999
3 100
0 20 100 10 1000 2
*/

HDOJ--4791--Alice's Print Service

时间: 2024-12-15 01:41:41

HDOJ--4791--Alice's Print Service的相关文章

HDU 4791 Alice&#39;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 pr

HDU 4791 Alice&#39;s Print Service (2013长沙现场赛,二分)

Alice's Print Service Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1855    Accepted Submission(s): 454 Problem Description Alice is providing print service, while the pricing doesn't seem to

HDU 4791 Alice&#39;s Print Service(2013长沙区域赛现场赛A题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4791 解题报告:打印店提供打印纸张服务,需要收取费用,输入格式是s1 p1 s2 p2 s3 p3...表示打印区间s1到s2张纸的单价是p1,打印区间s2 到s3的单价是p2....最后是sn到无穷大的单价是pn,让你求打印k张纸的总费用最少是多少?有m次查询. 因为s1*p1 > s2 * p2 > s3*p3......,很显然,加入k所在的那个区间是第x个区间,那么最低费用要么是k * p

HDU 4791 Alice&#39;s Print Service 水二分

点击打开链接 Alice's Print Service Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1084    Accepted Submission(s): 240 Problem Description Alice is providing print service, while the pricing doesn't

HDU 4791 Alice&#39;s Print Service 简单DP

连接:http://acm.hdu.edu.cn/showproblem.php?pid=4791 题意:打印问题,n次条件,打印量≥si时,每张纸的打印价格为pi(0≤n≤1e5),问打印m次询问,qi张时最少需要多少钱(0≤m≤1e5). 思路:如果对每次询问进行便利复杂度O(m*n)太大,超时.所以进行离线处理,将询问排序,从小到大依次处理,处理过程O(n+m),但排序过程是O(mlogm),所以总体的复杂度还是O(mlogm). 代码: #include <iostream> #inc

hdu 4791 Alice&#39;s Print Service (DP+离线处理)

Alice's Print Service Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1163    Accepted Submission(s): 270 Problem Description Alice is providing print service, while the pricing doesn't seem to

HDU 4791 &amp; ZOJ 3726 Alice&#39;s Print Service (数学 打表)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4791 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5072 Problem Description Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her pr

UVAlive 6611 Alice&#39;s Print Service 二分

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

2013 ACM/ICPC 长沙现场赛 A题 - Alice&#39;s Print Service (ZOJ 3726)

Alice's Print Service Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

HDU 4791 &amp;amp; ZOJ 3726 Alice&amp;#39;s Print Service (数学 打表)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4791 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5072 Problem Description Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her pr