今天有个题一直tle,改了好几遍算法,从最开始的优先队列,变成了,双队列来降低时间复杂度都失败了,还是tle,最后还是走上了写快读函数的路,记得最开始学的时候都很好奇是什么东西,感觉好厉害的样子,可是现在我宁愿用不到,哈哈哈。
上代码:
void read(int& x)
{
int f = 1; //f为符号位
x = 0;
char s = getchar(); //读入一个字符
while (s > '9' || s < '0') //处理空格。换行什么的,并且读入符号
{
if (s == '-')
f = -1;
s = getchar();
}
while (s >= '0' && s <= '9') //不断地读入数字字符来计算得到数字
{
x = x * 10 + s - '0';
s = getchar();
}
x *= f; //乘以符号即可得到数字
}
2021.8.4:时隔20天,我再次因为关掉同步流的时候使用快读,以至于自我怀疑了一个下午,真是离谱啊,
2022.3.7
更新了快读的模板化
template <typename T>
void read(T &t)
{
char str = getchar();
int f = 1;
while (str < '0' || str>'9') {
if (str == '-') f = 1;
str = getchar();
}
int res = 0;
while (str >= '0' && str <= '9') {
res = res * 10 + str - '0';
str = getchar();
}
t = res*f;
}