关于代码风格,个人推荐Google C++ Style Guide

关于代码风格,个人推荐Google C++ Style Guide
这篇文章里涉及的内容,是我反复找资料,反复自我实践,最终留下来的操作经验。

开数组开到全局变量,输入输出,long long

这个问题也是很多同学不注意的,并且,怎么教都不会改正的错误。
我觉得主要原因是,我不是他的第一个老师。真实是,不是零基础,是地板底下的基础。
和我一起学习过的,千万不要再搞事情了。
[参考]https://dmoj.ca/tips/#c-alloc

Allocating

Refrain from declaring big arrays as local variables, as it will often cause you to run out of stack space and fail with a Runtime Error.
Instead of doing:

  1. int main()
  2. {
  3. int N;
  4. scanf("%d", &N);
  5. int arr[N];
  6. for(int i = 0; i < N; i++) scanf("%d", &arr[i]);
  7. }

consider:

  1. int arr[100001];
  2. int main()
  3. {
  4. int N;
  5. scanf("%d", &N);
  6. for(int i = 0; i < N; i++) scanf("%d", &arr[i]);
  7. }

Declaring big arrays in global scope is a much safer approach as long as you know the maximum bound of N (and almost all problems give you upper bounds). Be wary of out of bounds array indices, though.

Input and Output

It is recommended for C++ users to use C-style input and output, namely scanf and printf instead of cin and cout for performance reasons.
If you must use cin and cout, you can put these two lines of code at the top of your main function:

  1. int main()
  2. {
  3. cin.sync_with_stdio(0);
  4. cin.tie(0);
  5. ...
  6. }

to speed up the cin stream. This will unsync cin with scanf and cout. Note that you should not use scanf after unsyncing with stdio.
Additionally, you should not use endl, but rather \n to output newlines when flushing is not required. endl’s flushing behavior can potentially cause your program to receive TLE instead of AC.

int, long, and long long

On the judge, int is 32-bit, long long is 64-bit, and long can be either 32- or 64-bit depending on which judge your submission is graded on. Therefore, it is recommended to use either int or long long, but not long.