跳到正文

闲话

CSP考前须知

Lin_Yi_Rui约 8 分钟
原文
文章信息与版本

文章信息

发布
2025年10月31日
收录
2026年11月2日
更新
2026年11月20日
篇幅
约 8 分钟

版本

  1. v1当前

声明

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

试机阶段

  • 检查硬件
    包括:鼠标的左右键能否正常使用、键盘的每个按键都检查一遍、显示器是否正常等,若出问题需找老师更换。
    观察电源的位置 不要轻易触碰它。
  • 检查编译器
    写一段简单程序运行,加上文件读写,检查是否正常。
  • 模拟考前 3030 分钟
    模拟第二天考前 3030 分钟要做的事情,检查熟练程度。
    (具体操作会在后面讲到)
  • 观察环境
    看看空调位置等,根据温度准备好第二天的衣物。
    确定考场及最近厕所位置。

考前 3030 分钟

一般来说你会提前 3030 分钟进入考场,能接触到电脑,但没有题目。

  • 创建好文件夹及子文件夹

  • 编译选项
    在 DEV-C++ 中,点击上方工具 -> 编译选项 -> 编译时加入以下命令: 并勾选,输入以下命令可还原评测环境,警告某些本地评测正常但上交后会报错的内容。

    注:评测机的栈空间为无限,但受限于题目空间限制 所以我们开的栈空间应与题目空间上限一致。

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

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

  • 写好你的代码框架和文件读写
C++
#include<bits/stdc++.h>

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;
}
  • 写好对拍程序
C++
//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;
}
C++
//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;
}
C++
//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;
}
C++
//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;
}
  • 默写常考的模版
    说不定就考上了呢。

比赛过程

时间分配

  • 首先通读所有题目。
    规则怪谈:T1 T2 不一定谁简单。
  • 不要死磕一道题。
  • 多争部分分。

策略与技巧

对于不会正解的题目。

数据分治

在小数据是使用暴力,大数据再使用优化算法。

C++
if(n<=1000)
	work1();
else
	work2(); 

时间检测

计算运行时间(CPU 时间),若时间超过题目要求,这里立刻输出已经算出的答案。

C++
double start_time = double(clock())/CLOCKS_PER_SEC;
double time_limit = 0.8;//一般会比时间上限小一点
 
...

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×107∼1×1081 \times 10^7 \sim 1 \times 10^8 次左右。
还要注意常数的影响。

  • nn 在 1010 以下:可能是暴力搜索/全排列枚举所有情况。
  • nn 在 2020 以下:可以考虑二进制枚举 O(2n)O(2^n) 。
  • nn 在 4040 以下:可能是搜索+优化,比如折半查找等。
  • nn 在 100∼500100 \sim 500:O(n3)O(n^3)。
  • nn 在 1000∼100001000 \sim 10000:O(n2)O(n^2)。
  • nn 在 105∼10610^5 \sim 10^6:O(nlog⁡n)O(n \log n)。
  • nn 在 106∼10710^6 \sim 10^7:O(n)O(n)。
  • n≥107n \ge 10^7:O(log⁡n)O(\log n) 或 O(1)O(1),如果不超过 101610^{16},也有可能是 O(n)O(\sqrt{n}) 等。

警告

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

结束前 2020 分钟

检查复杂度

计算时间复杂度和空间复杂度是否正确。

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

C++
//空间计算工具
#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 文件,其他内容尽量都删除。

关于头文件

建议使用万能头。

C++
#include <bits/stdc++.h>

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

原文