CSP考前须知

· · 闲话

声明

以下内容基于山东 CSP,本地系统一律为 Windows。

试机阶段

-O2 -std=c++14 -g -Wall -Wextra -Wl,--stack=1024000000

(O2 优化、版本 c++14、打开调试、启用警告、启用更多警告、开栈空间 1024 \times 1000 \times 1000 字节)

using namespace std; typedef long long ll;

inline ll read() { ll x = 0,f = 1; char ch = getchar(); while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar(); } while(isdigit(ch)) { x = (x<<3)+(x<<1)+(ch^48); ch = getchar(); } return x*f; }

int main() { freopen("XXX.in","r",stdin); freopen("XXX.out","w",stdout);

return 0;

}

- **写好对拍程序**  
```cpp
//std.cpp 要检查的程序
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

inline int read()
{
    int x = 0,f = 1;
    char ch = getchar();
    while(!isdigit(ch))
    {
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while(isdigit(ch))
    {
        x = (x<<3)+(x<<1)+(ch^48);
        ch = getchar();
    }
    return x*f;
}

int main()
{
    freopen("data.in","r",stdin);
    freopen("std.out","w",stdout);

    return 0;
}
//bf.cpp 保证正解的程序(暴力代码)
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

inline int read()
{
    int x = 0,f = 1;
    char ch = getchar();
    while(!isdigit(ch))
    {
        if(ch == '-') f = -1;
        ch = getchar();
    }
    while(isdigit(ch))
    {
        x = (x<<3)+(x<<1)+(ch^48);
        ch = getchar();
    }
    return x*f;
}

int main()
{
    freopen("data.in","r",stdin);
    freopen("bf.out","w",stdout);

    return 0;
}
//data.cpp 数据生成器
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int main()
{
    freopen("data.in","w",stdout);
    srand(time(0));

    return 0;
}
//check.cpp 对拍
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int main()
{
    while(1)
    {
        system("data.exe");
        system("std.exe");
        system("bf.exe");
        if(system("fc std.out bf.out"))
            break;
    }

    return 0;
}

比赛过程

时间分配

...

for(int i ...) { if (i % 10000 == 0) //不要每次循环都检查是否超时 //改为定期检查是否超时 减小获取运行时间造成的开销 { double now_time = double(clock())/CLOCKS_PER_SEC; if(now_time-start_time > time_limit) { printf("%d\n",ans); break; } }

...

}

### 数据规模与时间复杂度
一般来说评测机每秒运算 $1 \times 10^7 \sim 1 \times 10^8$ 次左右。     
还要注意常数的影响。

- $n$ 在 $10$ 以下:可能是暴力搜索/全排列枚举所有情况。
- $n$ 在 $20$ 以下:可以考虑二进制枚举 $O(2^n)$  。
- $n$ 在 $40$ 以下:可能是搜索+优化,比如折半查找等。  
- $n$ 在 $100 \sim 500$:$O(n^3)$。  
- $n$ 在 $1000 \sim 10000$:$O(n^2)$。  
- $n$ 在 $10^5 \sim 10^6$:$O(n \log n)$。  
- $n$ 在 $10^6 \sim 10^7$:$O(n)$。  
- $n \ge 10^7$:$O(\log n)$ 或 $O(1)$,如果不超过 $10^{16}$,也有可能是 $O(\sqrt{n})$ 等。

### 警告
- 变量名不用 `x1` `y1` `y0` `yn` `j1` `k0` `jn`。
- 变量名不用完整单词 `next` `time` `case` `array`。
- 别用 `#define int long long`。

# 结束前 $20$ 分钟
## 检查复杂度
计算时间复杂度和空间复杂度是否正确。

内存大小  $=$ 数组元素个数 $\times$ 类型字节数 $\div 1024 \div 1024$ (单位是 $MB$)。

```cpp
//空间计算工具
#include <bits/stdc++.h>

using namespace std;

bool ST;
int a[N];
int b[N][N];
...
bool ED;

int main()
{
    cout << (&ED - &ST) / 1024.0 / 1024.0 << "MB" << endl;
    return 0;
}

检查文件读写

每年都有文件读写被注释掉的。

然后查看输入输出的文件名是否正确。

检查格式

通常来说,提交的的答案应当在 姓名+考号 的文件夹下。
其中需包含每个题目的文件夹。
每个题目的文件夹中是你所写的代码,仅有 .cpp 文件,其他内容尽量都删除。

关于头文件

建议使用万能头。

#include <bits/stdc++.h>

防止部分函数在本地测评时可以运行,但在评测机上报错。