题解:P4392 [BalticOI 2007] Sound 静音问题
Super_Diu
·
·
题解
第一眼觉得这是一道水题,暴力模拟就行。
但是一看数据范围:1\le n\le10^6,1\le m\le10^4,0\le c\le10^4,还是一道水题。
虽然有 5 个看不懂的标签,但是好像还漏了一个:枚举。
直接枚举就能过。
思路
-
从 1 到 n-m+1 枚举左端点 i。
-
如果 f=0,输出 NONE。
很好,非常完美的过了。
但是跑到了 640 毫秒。
这哪是一个 OIer 能忍受的呢?
优化思路
首先,把 cin/cout 换成 scanf/printf,这样能节省不少时间。
record。
什么?614 毫秒?继续!
不难发现,maxn 和 minn 在求最值的时候,如果 maxn-minn>c,说明最大值减去最小值已经不符合要求了,再算下去的话,差会再变大。
于是我们可以在求最值的时候,加一个判断:if(maxn-minn>c) break;。
record。
卡过常的都知道,万能头 `bits/stdc++.h` 因为包含了非常多平时用不到的头文件,特别耗时。
于是把万能头换成 `cstdio`,`INT_MAX/INT_MIN` 换成数字,`max/min` 函数换成三目(不然会编译错误)。
再用 `C++98` 提交(数字大一点,应该更高级吧?)
再拿出祖传的快读。
[record](https://www.luogu.com.cn/record/261415914)。
$355$ 毫秒?继续!吗?
到这里,已经是本蒟蒻的极限了,虽然时间复杂度是正解的 $2.9$ 倍,但空间复杂度是正解的 $\displaystyle \frac{1}{20}$。
# Code
```cpp line-numbers
#include <cstdio> // 只保留一个头文件,速度最快
typedef long long ll; // 可加可不加
using namespace std;
const ll N=1e6+10;
int n,m,c,a[N]; // 不需要开 long long
bool f;
struct ios{ // 快读
inline char gc(){
static const int IN_LEN=1<<18|1;
static char buf[IN_LEN],*s,*t;
return (s==t)&&(t=(s=buf)+fread(buf,1,IN_LEN,stdin)),s==t?-1:*s++;
}
template <typename _Tp> inline ios & operator >> (_Tp&x){
static char ch,sgn; ch = gc(), sgn = 0;
for(;!(ch>='0'&&ch<='9');ch=gc()){if(ch==-1)return *this;sgn|=ch=='-';}
for(x=0;ch>='0'&&ch<='9';ch=gc())x=x*10+(ch^'0');
sgn&&(x=-x); return *this;
}
}io;
#define cin io // 直接变成cin的样子,写得方便一些
int main(){
cin>>n>>m>>c;
for(int i=1;i<=n;++i) cin>>a[i]; // ++i稍微快一丁点
for(int i=1;i<=n-m+1;++i){
int maxn=-2147483648,minn=2147483647;
for(int j=i;j<i+m;++j){
maxn=maxn>a[j]?maxn:a[j]; // 用三目,舍去 max/min 函数
minn=minn<a[j]?minn:a[j];
if(maxn-minn>c) break; // 剪枝
}
if(maxn-minn<=c){
printf("%d\n",i); // 输出
f=1;
}
}
if(!f) puts("NONE");
return 0; // 完结撒花 ~
}
```