聪明的质检员
来源:互联网 发布:网络空间安全专业待遇 编辑:程序博客网 时间:2024/04/23 23:50
二分+前缀和优化,不优化就是o(nmlogn)了
对参数w进行二分答案,通过w对y进行计算,如果找大了,就说明参数w找小了,答案在mid右面,找小了反之。
#include<cstdio> #include<iostream>#include<cstring>#define LL long longusing namespace std;LL sum[299999],cnt[299999];int n,m;LL s;LL minn=99999999999;int ll[299999],rr[299999],w[299999],v[299999];LL checkw(int x){ LL ans=0; memset(sum,0,sizeof(sum)); memset(cnt,0,sizeof(cnt)); for(int i=1;i<=n;i++) if(w[i]>=x) { sum[i]=sum[i-1]+v[i]; cnt[i]=cnt[i-1]+1; } else cnt[i]=cnt[i-1],sum[i]=sum[i-1];//前缀和 for(int i=1;i<=m;i++) { ans+=(sum[rr[i]]-sum[ll[i]-1])*(cnt[rr[i]]-cnt[ll[i]-1]);//算y } minn=min(minn,((s-ans)>=0)?(s-ans):(ans-s));//找最优解 return ans;}int main(){ scanf("%d%d%lld",&n,&m,&s); int l=1,r; for(int i=1;i<=n;i++) { scanf("%d%d",&w[i],&v[i]); r=max(r,w[i]);//找r的最大限度 } r+=5;//防止二分出错 for(int i=1;i<=m;i++) scanf("%d%d",&ll[i],&rr[i]); while(l<=r) { int mid=(r+l)>>1; LL y=checkw(mid); if(y>s) l=mid+1; else if(y<s) r=mid-1; else {printf("0");return 0;} }//二分答案 printf("%lld",minn); return 0;}
阅读全文
1 0
- 聪明的质检员
- 聪明的质检员
- 聪明的质检员
- 【二分】聪明的质检员
- NOIP2011 聪明的质检员
- 【NOIP2011】聪明的质检员
- NOIP2011聪明的质检员
- noip2011聪明的质检员
- [P1314]聪明的质检员
- 【noip2011】聪明的质检员
- NOIP2011 聪明的质检员
- 【二分】NOIP2011聪明的质检员
- VIJOS 1740 聪明的质检员
- 2014.11.5聪明的质检员
- NOIP2011 聪明的质检员 题解
- 【NOIP2011】聪明的质检员 二分
- NOIP2011提高组 聪明的质检员
- 【NOIP2011提高组】聪明的质检员
- CKM3视图选择
- IRP(I/O Request Package)详解
- JAVA事务的概念
- Makefile中添加shell脚本
- Java 线程安全
- 聪明的质检员
- 宝石排列问题
- FCIS+code | Fully Convolutional Instance-aware Semantic Segmentation
- 注册表数据提取工具RegRipper
- mybatis sql insert 一条数据 自动返回 该数据的id
- 为什么java 2个一样的数字比较 是false
- Python namedtuple使用详解
- 深度学习python库安装经验,Windows下安装Anaconda3 pycharm tensorflow keras theano中遇到的问题
- 字符串处理