hdu 4791 dp预处理+二分

题意: 打印东西,给出区间(张数)对应费用(到达一定张数就都按某更低的价格),m次询问,问最优费用。给的时候按张数递增给的。

dp出当前张数到最后的最小值。对于询问q,然后二分处》=q的最小的一个张数的价格。min(这个价格*p,dp[这+1])即可。nlogn;后来看网上有些人用线段树,没必要的。

ps:开始竟然因为犯中间数据爆int的初级错误!,不该不该!

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
struct node
{
   long long si;
    long long pi;
    long long pay;
};
long long dp[100005];
node v[100008];
int n,m;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&v[i].si,&v[i].pi);
            v[i].pay=v[i].si*v[i].pi;
        }
        dp[n-1]=v[n-1].pay;
        for(int i=n-2;i>=0;i--)
        {
           dp[i]=min(v[i].pay,dp[i+1]);
        }
        int x;
       while(m--)
       {
           scanf("%d",&x);
           int r=n,l=0,mid;
           while(l+1<r)
           {
               mid=(r+l)/2;
               if(v[mid].si>x)
               {
                   r=mid;
               }
               else
               {
                   l=mid;
               }
           }
          long long ans=v[l].pi*x;
           if(l+1<n&&dp[l+1]<ans)
            ans=dp[l+1];
          printf("%I64d\n",ans);
       }
    }
    return 0;
}
时间: 2024-10-05 05:32:28

hdu 4791 dp预处理+二分的相关文章

机房测试3:三角tri(dp预处理+二分+dfs剪枝)

题目: 分析: 考虑最暴力的暴搜,怎么才能优化呢? 如果我们确切地知道第k大的路径权值和有多大,那么在dfs里面加一个限制就可以求出所有前k大的路径了. 很显然答案是满足单调性的. 可以二分一个答案,dfs一遍,看满足这个答案的有多少条路径,如果超过k条,就往大的走. 最后把二分出来的答案跑一遍dfs,输出路径即可. 时间复杂度:O(log(n*n)*玄学dfs) #include<bits/stdc++.h> using namespace std; #define N 1005 #defi

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 水二分

点击打开链接 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 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长沙区域赛现场赛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 4185 Oil Skimming(二分匹配)

Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 883    Accepted Submission(s): 374 Problem Description Thanks to a certain "green" resources company, there is a new profitable

HDU 4417 划分树+二分

题意:有n个数,m个询问(l,r,k),问在区间[l,r] 有多少个数小于等于k. 划分树--查找区间第k大的数.... 利用划分树的性质,二分查找在区间[l,r]小于等于k的个数. 如果在区间第 i 大的数tmp>k,则往下找,如果tmp<k,往上找. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #incl