• 洛谷P1314 聪明的质监员


    P1314 聪明的质监员

    题目描述

    小T 是一名质量监督员,最近负责检验一批矿产的质量。这批矿产共有 n 个矿石,从 1到n 逐一编号,每个矿石都有自己的重量 wi 以及价值vi 。检验矿产的流程是:

    1 、给定m 个区间[Li,Ri];

    2 、选出一个参数 W;

    3 、对于一个区间[Li,Ri],计算矿石在这个区间上的检验值Yi:

    这批矿产的检验结果Y 为各个区间的检验值之和。即:Y1+Y2...+Ym

    若这批矿产的检验结果与所给标准值S 相差太多,就需要再去检验另一批矿产。小T

    不想费时间去检验另一批矿产,所以他想通过调整参数W 的值,让检验结果尽可能的靠近

    标准值S,即使得S-Y 的绝对值最小。请你帮忙求出这个最小值。

    输入输出格式

    输入格式:

    输入文件qc.in 。

    第一行包含三个整数n,m,S,分别表示矿石的个数、区间的个数和标准值。

    接下来的n 行,每行2个整数,中间用空格隔开,第i+1 行表示 i 号矿石的重量 wi 和价值vi。

    接下来的m 行,表示区间,每行2 个整数,中间用空格隔开,第i+n+1 行表示区间[Li,Ri]的两个端点Li 和Ri。注意:不同区间可能重合或相互重叠。

    输出格式:

    输出文件名为qc.out。

    输出只有一行,包含一个整数,表示所求的最小值。

    输入输出样例

    输入样例#1:
    5 3 15 
    1 5 
    2 5 
    3 5 
    4 5 
    5 5 
    1 5 
    2 4 
    3 3 
    输出样例#1:
    10
    

    说明

    【输入输出样例说明】

    当W 选4 的时候,三个区间上检验值分别为 20、5 、0 ,这批矿产的检验结果为 25,此

    时与标准值S 相差最小为10。

    【数据范围】

    对于10% 的数据,有 1 ≤n ,m≤10;

    对于30% 的数据,有 1 ≤n ,m≤500 ;

    对于50% 的数据,有 1 ≤n ,m≤5,000;

    对于70% 的数据,有 1 ≤n ,m≤10,000 ;

    对于100%的数据,有 1 ≤n ,m≤200,000,0 < wi, vi≤10^6,0 < S≤10^12,1 ≤Li ≤Ri ≤n 。

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #define maxn 200010
    #ifdef WIN32
    #define LL "%I64d"
    #else
    #define LL "%lld"
    #endif
    using namespace std;
    int n,m,w[maxn],v[maxn],L[maxn],R[maxn];
    long long s;
    long long check(int x){
        int cnt=0;
        long long sum=0,res=0;
        for(int i=1;i<=m;i++){
            sum=0;cnt=0;
            for(int j=L[i];j<=R[i];j++)
                if(w[j]>=x){
                    cnt++;
                    sum+=v[j];
                }
            res+=1LL*cnt*sum;
        } 
        return res;
    }
    int main(){
        freopen("Cola.txt","r",stdin);
        scanf("%d%d"LL,&n,&m,&s);
        int l=0x7fffffff,r=0;
        for(int i=1;i<=n;i++){
            scanf("%d%d",&w[i],&v[i]);
            l=min(l,w[i]);r=max(r,w[i]);
        }
        for(int i=1;i<=m;i++)scanf("%d%d",&L[i],&R[i]);
        long long ans=1e12;
        while(l<=r){
            int mid=(l+r)>>1;
            long long now=check(mid);
            ans=min(ans,abs(now-s));
            if(now>s)l=mid+1;
            else r=mid-1;
        }
        printf(LL,ans);
    }
    55分 二分答案+暴力check
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #include<cstring>
    #define maxn 200010
    #ifdef WIN32
    #define LL "%I64d"
    #else
    #define LL "%lld"
    #endif
    using namespace std;
    int n,m,w[maxn],v[maxn],L[maxn],R[maxn];
    long long sum[maxn][2];
    long long s;
    long long check(int x){
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;i++){
            if(w[i]>=x){
                sum[i][0]=sum[i-1][0]+1;
                sum[i][1]=sum[i-1][1]+v[i];
            }
            else{
                sum[i][0]=sum[i-1][0];
                sum[i][1]=sum[i-1][1];
            }
        }
        long long res=0;
        for(int i=1;i<=m;i++)
            res+=(sum[R[i]][0]-sum[L[i]-1][0])*(sum[R[i]][1]-sum[L[i]-1][1]);
        return res;
    }
    int main(){
        freopen("Cola.txt","r",stdin);
        scanf("%d%d"LL,&n,&m,&s);
        int l=0x7fffffff,r=0;
        for(int i=1;i<=n;i++){
            scanf("%d%d",&w[i],&v[i]);
            l=min(l,w[i]);r=max(r,w[i]);
        }
        for(int i=1;i<=m;i++)scanf("%d%d",&L[i],&R[i]);
        long long ans=1e12;
        while(l<=r){
            int mid=(l+r)>>1;
            long long now=check(mid);
            ans=min(ans,abs(now-s));
            if(now>s)l=mid+1;
            else r=mid-1;
        }
        printf(LL,ans);
    }
    100分 二分答案+前缀和
  • 相关阅读:
    浮点数如何存储与表示(精度问题)
    快速幂详解(幂运算与矩阵)
    斐波那契数列的四种解法(头递归、尾递归、迭代与矩阵快速幂)
    机器码与位运算
    pytorch中参数dim的含义(正负,零,不传)
    查看显卡名称 pytorch
    使用国内源快速下载Python包(以matplotlib为例)
    基于Attention的机器翻译模型(Visualizing A Neural Machine Translation Model)
    GNU Parallel-安装+使用
    统计文件个数、删除空文件等Linux命令
  • 原文地址:https://www.cnblogs.com/thmyl/p/7707755.html
Copyright © 2020-2023  润新知