题解:P10815 【模板】快速读入
本蒟蒻第一篇题解
题意
给定一个长度为n的整数序列a,求
思考
看到这题,这不就是个水题吗,一看数据,震惊我三百年,n高达唐人,如此庞大的输入量,寻常的cin会超时,TLE on #4,所以考虑使用快速读入。
快读模板
int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
这时有人会问,为什么这样就会快一些呢,众所周知,getchar()函数的读入速度会快于cin的读入速度,由于输入是一个一个字符输入的,因此想到使用读入一个字符+转化成整形的方式代替缓慢的输入 。
rc 但是一交,还是T掉了,而本蒟蒻并不会更加高级的写法,这时候想到一个函数getchar_unlocked(),它比直接getchar()要快得多,将getchar()改成getchar_unlocked()(linux环境),交上去就AC了。
code
#include<bits/stdc++.h>
using namespace std;
int read(){
int x=0,f=1;
char ch=getchar_unlocked();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar_unlocked();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar_unlocked();
}
return x*f;
}
int n=read();
long long ans;
int main(){
for(int i=1;i<=n;i++){
ans+=read();
}
cout<<ans;
return 0;
}
完结撒花!!!