我只是想知道两份几乎相同的代码,为什么时间差会这么大

P2251 质量检测

@[computerlover](/space/show?uid=28747) 因为scanf比cin快很多
by 1124828077ccj @ 2017-08-05 15:52:25


楼上+1 如果想较快地使用cin/cout 可以在main函数开头添加 ios::sync\_with\_stdio(false); @computerlover
by Tony_Chu_ @ 2017-08-05 16:21:11


@[2016陈常杰](/space/show?uid=14738) 可是我只用了一个cin和一个cout啊,输入数组中的元素都是用scanf的,一个cin和一个cout会造成这么大的时间差吗?
by LDlornd @ 2017-08-06 22:04:14


@[2016陈常杰](/space/show?uid=14738) 是不是因为这道题的数据量大全局变量多所导致的啊?
by LDlornd @ 2017-08-06 22:09:02


@[computerlover](/space/show?uid=28747) 说错了,是printf和cout。。。
by 1124828077ccj @ 2017-08-07 09:12:24


@[2016陈常杰](/space/show?uid=14738) 谢谢了
by LDlornd @ 2017-08-08 19:20:11


据说,c语言和c++的输入输出混着用会使是时间效率大大降低!
by ZJH365 @ 2018-02-22 16:47:00


@[LDlornd](/space/show?uid=28747) 这有个我的48ms更快的(逃) ```cpp #pr\ agma GCC optimize("O3") #include<cstdio> #include<algorithm> using namespace std; #define ll long long const int INF = 0x7fffffff; const int maxn = 1e6+10; int n, k, m, t[maxn << 2]; namespace io { #define getc() (S == T && (T = (S = B) + fread(B, 1, MAXBUF, stdin), S == T) ? 0 : *S++) const int MAXBUF = 1 << 22; char B[MAXBUF], *S = B, *T = B; template<class Type> inline Type read() { register Type aa = 0; register bool bb = 0; register char ch, *S = io::S, *T = io::T; for (ch = getc(); (ch < '0' || ch > '9') && ch != '-'; ch = getc()) ; for (ch == '-' ? bb = 1 : aa = ch - '0', ch = getc(); '0' <= ch && ch <= '9'; ch = getc()) aa = aa * 10 + ch - '0'; io::S = S, io::T = T; return bb ? -aa : aa; } int(*F)() = read<int>; template<> inline double read() { register double aa, bb; register char ch; register char *S = io::S, *T = io::T; while (ch = getc(), (ch<'0' || ch>'9')) ; aa = ch - '0'; while (ch = getc(), (ch >= '0'&&ch <= '9')) aa = aa * 10 + ch - '0'; if (ch == '.') { bb = 1; while (ch = getc(), ch >= '0'&&ch <= '9') bb *= 0.1, aa += bb*(ch - '0'); } io::S = S, io::T = T; return aa; } char buff[MAXBUF], *iter = buff; template<class T>inline void P(register T x, register char ch = '\n') { static int stack[110]; register int O = 0; register char *iter = io::iter; if (!x)*iter++ = '0'; else { (x < 0) ? x = -x, *iter++ = '-' : 1; for (; x; x /= 10) stack[++O] = x % 10; for (; O; *iter++ = '0' + stack[O--]); } *iter++ = ch, io::iter = iter; } inline void putc(register char ch) { *iter++ = ch; } inline void ouput() { fwrite(buff, 1, iter - buff, stdout), iter = buff; } } using io::P; int (*F)() = io::read<int>; void build(int x) { int a; for (m = 1; m < x; m <<= 1); for (register int i = m + 1; i <= x + m; ++i) a = F(), t[i] = a; for (register int i = m - 1; i; --i) t[i] = min(t[i << 1], t[i << 1 | 1]); } int query(register int l, register int r) { int ans = INF; for (l += m - 1, r += m + 1; l ^ r ^ 1; l >>= 1, r >>= 1) { if (~l & 1) ans = min(ans, t[l ^ 1]); if (r & 1) ans = min(ans, t[r ^ 1]); } return ans; } int main(){ int x, y; n = F(); k = F(); build(n); for(register int i = k + 1; i <= n + 1; ++i){ P(query(i - k, i - 1)), io::ouput(); } return 0; } ```
by 权御天下 @ 2018-06-09 23:04:50


神仙~
by 森岛帆高 @ 2019-05-13 22:52:27


|