跳到正文

算法·理论

输入输出的优化技巧与方法

saka1985约 14 分钟
原文
文章信息与版本

文章信息

收录
2026年7月16日
更新
2026年7月18日
篇幅
约 14 分钟
获赞
1

版本

  1. v1当前

WHY大神们的输入输出那么快?

updateupdate:2026/7/142026/7/14 15:4015:40。

引言

当我们遇到超出时间限制的时候,

这些小技巧或许可以优化你的时间复杂度。

Part1:

当你写完程序后发现全都用的 cin/coutcin/cout ,

那就彻底疯狂?

不,这三行语句能教你的程序重新做人。

C++
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

我绝对不会告诉你我只是懒才都写的是0。

或者是:

C++
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

这样你的时间复杂度就跌至冰点。

现在的时间复杂度。

之前的时间复杂度。

Part2:

将 cin/coutcin/cout 替换成 scanf/printfscanf/printf 。

这样你的时间复杂度也会跌至冰点。

附上代码示例。

C++
    scanf("%d",&n);
    printf("%d",n);

作者比较推荐这个,因为上手好写。

Part3:

dalaodalao们会手写namespacenamespace,

当然我不咋会写。

这里附上_zyx2012大神写的 namespacenamespace 吧,供诸位参考。

C++
namespace io{
#ifdef LOCAL
	constexpr int len = 1;
#else
	constexpr int len = 1 << 21;
#endif
	class input{
		char buf[len],*bg = buf,*en = buf;
	public:
		inline char gc(){
			return (bg == en&&(en = (bg = buf) + fread(buf,1,len,stdin)) == buf?EOF:*(bg++));
		}

		template<class T>inline void getuint(T &v){
			v = 0;
			char ch,ch2;

			for (;(ch = gc()) < '0'||ch > '9'&&ch != EOF;);

			for (;ch >= '0'&&ch <= '9';ch = gc()) v = (v << 1) + (v << 3) + ch - 48;
		}

		template<class T>inline void getint(T &v){
			v = 0;
			char ch,ch2;
			bool op = 0;

			for (;(ch = gc()) < '0'||ch > '9'&&ch != EOF;) op ^= (ch == '-');

			for (;ch >= '0'&&ch <= '9';ch = gc()) v = (v << 1) + (v << 3) + ch - 48;

			if (op) v = -v;
		}

		template<class T>inline T getint(){
			T ret = 0;
			getint(ret);
			return ret;
		}

		template<class T>inline T getuint(){
			T ret = 0;
			getuint(ret);
			return ret;
		}

		inline int getstr(char *s,int len = INT_MAX){
			int cnt = 0;
			for (;--len&&(*s = gc()) != ' '&&*s != '\n';cnt++,s++);
			*s = '\0';
			return cnt;
		}

		inline void getstr(string &s){
			s.clear();
			for (char ch;(ch = gc()) != ' '&&ch != '\n';s += ch);
		}

		inline input &operator >> (short &x){getint(x);return *this;}
		inline input &operator >> (int &x){getint(x);return *this;}
		inline input &operator >> (long &x){getint(x);return *this;}
		inline input &operator >> (long long &x){getint(x);return *this;}
		inline input &operator >> (__int128 &x){getint(x);return *this;}
		inline input &operator >> (unsigned short &x){getint(x);return *this;}
		inline input &operator >> (unsigned &x){getint(x);return *this;}
		inline input &operator >> (unsigned long &x){getint(x);return *this;}
		inline input &operator >> (unsigned long long &x){getint(x);return *this;}
		inline input &operator >> (unsigned __int128 &x){getint(x);return *this;}
		inline input &operator >> (char &x){for (;(x = gc()) == ' '||x == '\n';);return *this;}
		inline input &operator >> (char *x){getstr(x);return *this;}
		inline input &operator >> (string &x){getstr(x);return *this;}
	}fin;

	class output{
		char buf[1 << 21],buf2[20],mp[1000][3];
		int top = 0,top2 = 0;
	public:
		output(){
			for (int i = 0,ci = 48;i < 1000;i += 100,ci++)
				for (int j = 0,cj = 48;j < 100;j += 10,cj++)
					for (int k = 0,ck = 48;k < 10;k++,ck++)
						mp[i + j + k][0] = ck,mp[i + j + k][1] = cj,mp[i + j + k][2] = ci;
		}
		inline void flush(){
			fwrite(buf,1,top,stdout);
			top = 0;
		}

		inline void pc(char ch){buf[top++] = ch;if (top >= 1 << 21) flush();}

		template<class T>inline void putint(T x){
			if (!x){pc('0');return ;}
			if (x < 0) pc('-'),x = -x;
			for (;x >= 1000;x /= 1000)
				buf2[top2++] = mp[x % 1000][0],buf2[top2++] = mp[x % 1000][1],buf2[top2++] = mp[x % 1000][2];
			if (x){
				buf2[top2++] = mp[x][0];
				if (x >= 10){
					buf2[top2++] = mp[x][1];
					if (x >= 100)
						buf2[top2++] = mp[x][2];
				}
			}
			for (;top2;pc(buf2[--top2]));
		}

		inline void putstr(const char *s,int len){
			for (;len--;pc(*(s++)));
		}

		inline void putstr(const string &s){
			for (auto i:s) pc(i);
		}

		inline output &operator << (short x){putint(x);return *this;}
		inline output &operator << (int x){putint(x);return *this;}
		inline output &operator << (long x){putint(x);return *this;}
		inline output &operator << (long long x){putint(x);return *this;}
		inline output &operator << (const __int128 &x){putint(x);return *this;}
		inline output &operator << (unsigned short x){putint(x);return *this;}
		inline output &operator << (unsigned int x){putint(x);return *this;}
		inline output &operator << (unsigned long x){putint(x);return *this;}
		inline output &operator << (unsigned long long x){putint(x);return *this;}
		inline output &operator << (const unsigned __int128 &x){putint(x);return *this;}
		inline output &operator << (char x){pc(x);return *this;}
		inline output &operator << (const char *x){putstr(x);return *this;}
		inline output &operator << (const string &x){putstr(x);return *this;}
		inline output &operator << (void (*func)(output &)){func(*this);return *this;}

		~output(){flush();}
	}fout;

	void flush(output &out){out.flush();}
	void endl(output &out){out << '\n';out.flush();}
}

using namespace io;

Part4:

也可以考虑用快读来优化, 这样可以大大缩短读取时间。

示例代码板子:

(引用自Jan1860大神)

C++
struct IandO{
	string OutputBuffer;
	void Reset()
	{
		OutputBuffer="";
	}
	int ReadInt()
	{
		int tp=1,res=0;
		char c;
		c=getchar();
		while(c<'0'||c>'9') 
		{
			if(c=='-') tp*=-1;
			c=getchar();
		} 
		while('0'<=c&&c<='9')
		{
			res=res*10+(c-'0');
			c=getchar();
		}
		return res*tp;
	}
	long long ReadLL()
	{
		long long tp=1,res=0;
		char c;
		c=getchar();
		while(c<'0'||c>'9') 
		{
			if(c=='-') tp*=-1;
			c=getchar();
		} 
		while('0'<=c&&c<='9')
		{
			res=res*10+(c-'0');
			c=getchar();
		}
		return res*tp;
	}
	__int128 ReadI128()
	{
		__int128 tp=1,res=0;
		char c;
		c=getchar();
		while(c<'0'||c>'9') 
		{
			if(c=='-') tp*=-1;
			c=getchar();
		} 
		while('0'<=c&&c<='9')
		{
			res=res*10+(c-'0');
			c=getchar();
		}
		return res*tp;
	}
	void Flush()
	{
		for(int i=0;i<OutputBuffer.size();i++) putchar(OutputBuffer[i]);
		OutputBuffer="";
	}
	void Outp(char c)
	{
		if(OutputBuffer.size()>5e4) Flush();
		OutputBuffer+=c;
	}
	void WriteInt(int n)
	{
		if(n==0)
		{
			Outp('0');
			return;
		}
		if(n<0)
		{
			Outp('-');
			n*=-1; 
		}
		if(n<10)
		{
			char c=n+'0';
			Outp(c);
			return;
		}
		WriteInt(n/10);
		char c=(n%10)+'0';
		Outp(c);
		return;
	}
	void WriteLL(long long n)
	{
		if(n==0)
		{
			Outp('0');
			return;
		}
		if(n<0)
		{
			Outp('-');
			n*=-1; 
		}
		if(n<10)
		{
			char c=n+'0';
			Outp(c);
			return;
		}
		WriteLL(n/10);
		char c=(n%10)+'0';
		Outp(c);
		return;
	}
	void WriteI128(__int128 n)
	{
		if(n==0)
		{
			Outp('0');
			return;
		}
		if(n<0)
		{
			Outp('-');
			n*=-1; 
		}
		if(n<10)
		{
			char c=n+'0';
			Outp(c);
			return;
		}
		WriteI128(n/10);
		char c=(n%10)+'0';
		Outp(c);
		return;
	}
	void WriteSpace()
	{
		Flush();
		putchar(' ');
	}
	void WriteEnter()
	{
		Flush();
		putchar('\n');
	}
}IO;
//注:最后别忘了IO.Flush()

特别鸣谢

@Jan1860

Jan1860

@_zyx2012

_zyx2012

两位大神的代码示例。

无良推广

我的网址。

用户编号 20113862011386

saka1985saka1985
2026/7/132026/7/13 刚红名

作者主页

End

附上 meme 的心情:

拒绝 TLETLE !!!

爆肝写完,感谢支持。

愿这篇文章可以帮到大家。

谢谢大家!

附

欢迎大家在讨论区指正或者提问,

我会尽我所能为你解答。

原文