Read-in optimization and output optimization

C++ is an amazing subject. . .

When reading in/out, we often use:

scanf , cin , printf , cout

However, in the algorithm competition, we pursue a principle:

Efficiency first!

As a result, all kinds of tricks and tricks were born. . .

The following read-in and output optimizations are one of them.

Read-in optimization:

The scanf that comes with C++ is already very fast, but the data of 100W takes 0.6~0.8s. Obviously, it is still slow.

Why? Because scanf brings a lot of special judgments that we don't need.

So, read-in optimization breaks out of the shell:

inline int read(){
	int date=0,w=1;char c=0;
	while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
	while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
	return date*w;
}

When calling, just:

int x;
x=read();

That's it.

Super efficient!

Output optimization:

With read-in optimization, there is output optimization:

inline void write(int x){
	if(x<0){putchar('-');x=-x;}
	if(x>9)write(x/10);
	putchar(x%10+'0');
}

When called, just:

int x=10;
write(x);

That's it.

Time is greatly reduced!

Summarize:

There are all kinds of tricks and tricks in C++, such as:

inline , register

Wait.

Make good use of them, you can get rid of those TLE points, and even reverse the AC!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324603034&siteId=291194637