程序设计竞赛高分必备
O2优化
目前算法编程竞赛中基本支持C++11,开启O2优化。
- O0:不做任何优化,这是默认的编译选项。
- O1:优化会消耗较多的编译时间,它主要对代码的分支,常量以及表达式等进行优化。
- O2:会尝试更多的寄存器级的优化以及指令级的优化,它会在编译期间占用更多的内存和编译时间。
- O3:在O2的基础上进行更多的优化,例如使用伪寄存器网络,普通函数的内联,以及针对循环的更多优化。
- Os:主要是对代码大小的优化,不用做更多的关心。 通常各种优化都会打乱程序的结构,让调试工作变得无从着手。并且会打乱执行顺序,依赖内存操作顺序的程序需要做相关处理才能确保程序的正确性。
C++程序中的O2开关如下所示:
#pragma GCC optimize(2)
只需将这句话放到程序的开头即可打开O2优化开关,其他优化只需修改括号中的数。
C++快读快写
ll read() {
ll ans = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1; ch = getchar();
}
while (ch >= '0' && ch <= '9')
ans = (ans << 1) + (ans << 3) + (ch ^ 48), ch = getchar();
return ans * f;
}
void write(ll x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(char(x % 10 + '0'));
}
OI/ACM竞赛造数据对拍脚本
@REM This is a script for generating data and checking against.
COLOR A
@REM generate executable
G++ data.cpp -o data.exe
G++ std1.cpp -o std1.exe
G++ std2.cpp -o std2.exe
@REM make standard data
for /L %%i in (1,1,10) do (
data.exe > data%%i.in
std1.exe < data%%i.in > data%%i.out
std2.exe < data%%i.in > fcdata%%i.out
)
@REM checking against
for /L %%i in (1,1,10) do (
fc data%%i.out fcdata%%i.out
if errorlevel==1 pause
)
EXIT
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 10, INF = 0x3f3f3f3f;
int main() {
system("g++ data.cpp -o data.exe");
system("g++ std1.cpp -o std1.exe");
system("g++ std2.cpp -o std2.exe");
char com[300];
for (int i = 1; i <= 10; i++) {
clock_t start = clock();
sprintf(com, "data.exe > data%d.in", i), system(com);
sprintf(com, "std1.exe < data%d.in > data%d.out", i, i), system(com);
sprintf(com, "std2.exe < data%d.in > fcdata%d.out", i, i), system(com);
clock_t end = clock();
if (system(com)) {
printf("Test %d wa\n", i); return 0;
} else {
printf("Test %d AC with Time: %d\n", i, (end - start) / CLOCKS_PER_SEC);
}
}
return 0;
}

浙公网安备 33010602011771号