为什么关闭同步流用cin会超时2个点,用scanf 不会!!

P1440 求m区间内的最小值

VxiaohuanV @ 2022-08-02 10:59:06

#include <bits/stdc++.h>
using namespace std;
#define M 2000005
#define ri register int 

int n,m;
int p[M];
int qu[M];
int main(){ 
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);

    cin>>n>>m;
    for(ri i=1;i<=n;i++) cin>>p[i];
    int l=1,r=1;qu[1]=1;cout<<0<<endl;
    for(ri i=2;i<=n;i++)
    {
        while(qu[l]<i-m) l++;
        cout<<p[qu[l]]<<endl;
        while(p[qu[r]]>=p[i]&&r>=l) r--;qu[++r]=i;
    }

    return 0;

} 

by _Remake_ @ 2022-08-02 11:01:24

endl很慢(


by _Remake_ @ 2022-08-02 11:02:27

@sfksf 换成"\n"直接过了


by TheSky233 @ 2022-08-02 11:04:23

endl 会刷新缓冲区,比较慢


by bikuhiku @ 2022-08-02 11:15:46

似乎读入1e7个数据(1\sim 10^7的整型)时,cin解绑关闭同步是4s多,而scanf能达到0.8s


by rxjdasiwzl @ 2022-08-02 12:39:39

@bikuhiku 在我这里读入 2\times10^7 个整数,std::cin 用时 1.155 s,scanf 用时 1.297 s。


|