FastIO

FastIO

写程序总有想不到正解的时候,时间复杂度不合要求,便只能打暴力来骗点分,所以如何优化暴力便成为我这种菜鸡钻研的课题了.

既然在算法层面找不到突破口,那我就走一点歪门邪道.

除了算法,对程序运行时间影响最大的因素是什么?

读入输出

所以贴上究级代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
namespace IO {
const int lim = 1 << 20 | 500;

char buf[lim + 5], *S, *T;

inline char gc() {
if (S == T) {
T = (S = buf) + fread(buf, 1, lim, stdin);
if (S == T) return EOF;
}
return *S++;
}

template < typename T >
inline T read() {
T x; bool f; char c;
for (f = 0; (c = gc()) < '0' || c > '9'; f = c == '-') ;
for (x = c ^ '0'; (c = gc()) >= '0' && c <= '9'; x = (x << 1) + (x << 3) + (c ^ '0')) ;
return f ? -x : x;
}

inline bool islegal(char c) { return c < -1 || (c > 32 && c < 127); }

inline void readstring(char *str) {
char c;
while (!islegal(c = gc())) ;
for (*str++ = c; islegal(c = gc()); *str++ = c) ;
*str = '\0';
}

char obuf[lim + 5], *OS;

inline void flush_stdout() {
if (OS - obuf) fwrite(obuf, 1, OS - obuf, stdout), OS = obuf;
}

inline void pc(char c) {
*OS++ = c;
if (OS - obuf == lim) flush_stdout();
}

template < typename T >
inline void print(T x, char c = '\n') {
if (x < 0) x = -x, pc('-');
static int tmp[40], cnt;
tmp[cnt = 0] = x % 10; x /= 10;
for (; x; x /= 10) tmp[++cnt] = x % 10;
for (; ~cnt; pc(tmp[cnt--] ^ '0')) ;
pc(c);
}

struct flusher {
flusher() { OS = obuf; }
~flusher() { flush_stdout(); }
} __flusher__;
}

调用方法

读入

1
n = IO::read < int > ();

输出

1
IO::print(ans);

实测

光贴代码没说服力
我们来看看真正的运行结果
emmm
对于一般情况速度都有明显提升,在这里我就懒得截屏了.

Helping poor children in CSSYZ!!!