$Noip2011/Luogu1314$ 聪明的质监员 二分+巧妙前缀和
$Sol$
首先$W$一定是某个$w_i$.于是一种暴力方法就出炉了,枚举$W$再计算.
注意到,满足$S-Y$的绝对值最小的$Y$只可能是两种,一种是$<S$的最大的$Y$,一种是$>S$的最小的$Y$.那就分别求出来叭.分别求的时候这个$W$的值是可以二分的.但是这样并不能$A$掉这题,因为$check$的复杂度仍然是$O(NM)$的.看了题解之后发现$check$可以用前缀和吖,觉得很巧妙$qwq$.这样下来$check()$的复杂度变成$O(N+M).$
$Code$
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define il inline
#define Rg register
#define go(i,a,b) for(Rg int i=a;i<=b;++i)
#define yes(i,a,b) for(Rg int i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define int long long
#define db double
#define inf 2147483647
using namespace std;
il int read()
{
Rg int x=0,y=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
return x*y;
}
const int N=200010;
int n,m,S,as,minw=inf,maxw,sn[N],sv[N];
struct nd1{int w,v;}a[N];
struct nd2{int l,r;}b[N];
il int calc(int x)
{
Rg int ret=0;
mem(sn,0);mem(sv,0);
go(i,1,n)
if(a[i].w>=x)sn[i]=sn[i-1]+1,sv[i]=sv[i-1]+a[i].v;
else sn[i]=sn[i-1],sv[i]=sv[i-1];
go(i,1,m)
{
Rg int l=b[i].l,r=b[i].r;
ret+=(sn[r]-sn[l-1])*(sv[r]-sv[l-1]);
}
return ret;
}
il int ef1()
{
Rg int l=minw,r=maxw,mid,y,ret;
while(l<=r)
{
mid=(l+r)>>1;
y=calc(mid);
if(y<=S)ret=y,r=mid-1;
else l=mid+1;
}
return ret;
}
il int ef2()
{
Rg int l=minw,r=maxw,mid,y,ret;
while(l<=r)
{
mid=(l+r)>>1;
y=calc(mid);
if(y>=S)ret=y,l=mid+1;
else r=mid-1;
}
return ret;
}
main()
{
n=read(),m=read(),S=read();
go(i,1,n)a[i]=(nd1){read(),read()},minw=min(minw,a[i].w),maxw=max(maxw,a[i].w);
go(i,1,m)b[i]=(nd2){read(),read()};
Rg int y1=ef1(),y2=ef2();
as=min(abs(y1-S),abs(y2-S));
printf("%lld\n",as);
return 0;
}
随机推荐
- js+canvas五子棋人机大战ai算法
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- LeetCode Weekly Contest 6
leetcode现在每周末举办比赛,这是今天上午参加的比赛的题解.题目难度不算大,两个easy,一个medium,一个hard.hard题之前接触过,所以做得比较顺利. 1. Sum of Left ...
- Python语言的缺点
- [C#] 查标准正态分布表
C#里面要计算正态分布是一件比较麻烦的事情,一般是通过查表来实现的. static double[] ayZTFB = null; /// <summary> /// 计算标准正态分布表 ...
- behavior planning——11 create a cost function speed penalty
A key part of getting transitions to happen when we want them to is the design of reasonable cost ...
- python不得不知的几个开源项目
1.Trac Trac拥有强大的bug管理 功能,并集成了Wiki 用于文档管理.它还支持代码管理工具Subversion ,这样可以在 bug管理和Wiki中方便地参考程序源代码. Trac有着比较 ...
- xUtils框架的介绍(四)
今天介绍xUtils的最后一个模块--HttpUtils,拖了那么久,终于要结束了.另外,码字不易,如果大家有什么疑问和见解,欢迎大家留言讨论.HttpUtils是解决日常工作过程中繁杂的上传下载文件 ...
- functiils.lru_cache缩短递归时间
力扣上看到一道题: 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 使用普通递归解决,超出时间限 ...
- Talk is cheap. Show me the code.
Talk is cheap. Show me the code. -- Linux创始人 Linus Torvalds 2000-08-25 Stay hungry Stay foolish -- 乔 ...
- 如何安装Anaconda和Python
1.下载安装文件 https://www.anaconda.com/download/ 2.百度安装方法:https://jingyan.baidu.com/article/3f16e0031e875 ...