Part 1.复杂度
- 首先时间复杂度是一定要算的。他能够告诉我们代码是否超时。
- 空间复杂度也非常重要,如果内存超限,一定会爆0。
Part 2.读入输出
- 对于一些数据量大的题目,我们需要 s c a n f scanf scanf 和 p r i n t f printf printf 的帮助,或者使用快读。当然, f r e a d fread fread 是比较快的。
f r e a d fread fread 示例:
#include<bits/stdc++.h>
#define int long long
#define il inline
#define rg register
#define F(i,l,r) for(int i=l,i##end=r;i<=i##end;++i)
#define G(i,l,r) for(int i=l,i##end=r;i>=i##end;--i)
using namespace std;
#ifdef LOCAL
#include"dbg.h"
#else
#define dbg(...) (__VA_ARGS__)
#endif
namespace Fread {
const int SIZE=1<<16;
char buf[SIZE],*S,*T;
inline char getchar() {
if(S==T) {
T=(S=buf)+fread(buf,1,SIZE,stdin);
if(S==T)return'\n';
}
return *S++;
}
} namespace Fwrite {
const int SIZE=1<<16;
char buf[SIZE],*S=buf,*T=buf+SIZE;
inline void flush() {
fwrite(buf,1,S-buf,stdout);
S=buf;
} inline void putchar(char c) {
*S++=c;
if(S==T)flush();
} struct NTR {
~NTR() {
flush();
}
} ztr;
}
#define getchar Fread::getchar
#define putchar Fwrite::putchar
#define Setprecision 10
#define between ' '
template<typename T>struct is_char {
static constexpr bool value=(std::is_same<T,char>::value||std::is_same<T,signed char>::value||std::is_same<T,unsigned char>::value);
};
template<typename T>struct is_integral_ex {
static constexpr bool value=(std::is_integral<T>::value||std::is_same<T,__int128>::value)&&!is_char<T>::value;
};
template<typename T>struct is_floating_point_ex {
static constexpr bool value=std::is_floating_point<T>::value||std::is_same<T,__float128>::value;
};
namespace Fastio {
struct Reader {
template<typename T>typename std::enable_if_t<std::is_class<T>::value,Reader&>operator>>(T&x) {
for(auto &y:x)*this>>y;
return *this;
} template<typename T>typename std::enable_if_t<is_integral_ex<T>::value,Reader&>operator>>(T&x) {
char c=getchar();
short f=1;
while(c<'0'||c>'9') {
if(c=='-')f*=-1;
c=getchar();
}
x=0;
while(c>='0'&&c<='9') {
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
x*=f;
return *this;
} template<typename T>typename std::enable_if_t<is_floating_point_ex<T>::value,Reader&>operator>>(T&x) {
char c=getchar();
short f=1,s=0;
x=0;
T t=0;
while((c<'0'||c>'9')&&c!='.') {
if(c=='-')f*=-1;
c=getchar();
}
while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();
if(c=='.')c=getchar();
else return x*=f,*this;
while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();
while(s--)t/=10.0;
x=(x+t)*f;
return*this;
} template<typename T>typename std::enable_if_t<is_char<T>::value,Reader&>operator>>(T&c) {
c=getchar();
while(c=='\n'||c==' '||c=='\r')c=getchar();
return *this;
} Reader&operator>>(char*str) {
int len=0;
char c=getchar();
while(c=='\n'||c==' '||c=='\r')c=getchar();
while(c!='\n'&&c!=' '&&c!='\r')str[len++]=c,c=getchar();
str[len]='\0';
return*this;
} Reader&operator>>(std::string&str) {
char c=getchar();
while(c=='\n'||c==' '||c=='\r')c=getchar();
while(c!='\n'&&c!=' '&&c!='\r')str.push_back(c),c=getchar();
return*this;
} Reader() {}
} cin;
const char endl='\n';
struct Writer {
typedef __int128 mxdouble;
template<typename T>typename std::enable_if_t<std::is_class<T>::value,Writer&>operator<<(T x) {
for(auto &y:x)*this<<y<<between;
*this<<'\n';
return *this;
} template<typename T>typename std::enable_if_t<is_integral_ex<T>::value,Writer&>operator<<(T x) {
if(x==0)return putchar('0'),*this;
if(x<0)putchar('-'),x=-x;
static int sta[45];
int top=0;
while(x)sta[++top]=x%10,x/=10;
while(top)putchar(sta[top]+'0'),--top;
return*this;
} template<typename T>typename std::enable_if_t<is_floating_point_ex<T>::value,Writer&>operator<<(T x) {
if(x<0)putchar('-'),x=-x;
mxdouble _=x;
x-=(T)_;
static int sta[45];
int top=0;
while(_)sta[++top]=_%10,_/=10;
if(!top)putchar('0');
while(top)putchar(sta[top]+'0'),--top;
putchar('.');
for(int i=0; i<Setprecision; i++)x*=10;
_=x;
while(_)sta[++top]=_%10,_/=10;
for(int i=0; i<Setprecision-top; i++)putchar('0');
while(top)putchar(sta[top]+'0'),--top;
return*this;
} template<typename T>typename std::enable_if_t<is_char<T>::value,Writer&>operator<<(T c) {
putchar(c);
return*this;
} Writer&operator<<(char*str) {
int cur=0;
while(str[cur])putchar(str[cur++]);
return *this;
} Writer&operator<<(const char*str) {
int cur=0;
while(str[cur])putchar(str[cur++]);
return*this;
} Writer&operator<<(std::string str) {
int st=0,ed=str.size();
while(st<ed)putchar(str[st++]);
return*this;
} Writer() {}
} cout;
}
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl
#define double long double
constexpr int inf=1e8,zro=0;
constexpr double zrod=0,eps=1e-18;
struct frac {
int fz,fm;
frac(int fz_=0,int fm_=1) {
fz=fz_,fm=fm_;
}
frac operator+(const frac &b)const {
return frac(fz*b.fm+fm*b.fz,fm*b.fm);
}
frac operator-(const frac &b)const {
return frac(fz*b.fm-fm*b.fz,fm*b.fm);
}
frac&reduce() {
int _=__gcd(fz,fm);
fz/=_,fm/=_;
return *this;
}
void print() {
/*cerr<<fz<<'/'<<fm<<endl;*/
}
};
using namespace std;
2.
f
r
e
o
p
e
n
freopen
freopen
很多选手会在调试时注释
f
r
e
o
p
e
n
freopen
freopen 但调试后并无删除注释,导致爆0。这非常可惜。
Part 3 数据越界问题
很多人赛事代码 R E RE RE 一般都是因为代码越界,一般代码越界有两种。
- 下标出现负数,在一些下标是由一个式子减去一个式子时,就要小心会不会是负数。
- 数组不够大,我们要看清楚代码中最大的小标在哪里,数组的大小一定不要小于它。
Part 4 题目阅读
题目的难度不一定是从简到难的。阅读题目非常重要,它不仅能让我们读懂题意,还能是我们简单评估难度。
“磨刀不误砍柴工”,拿到试卷后阅读所有题目是个好习惯。
Part 5 l o n g l o n g long long longlong 与 i n t int int
l o n g l o n g long long longlong 的存储范围是 2 64 2^{64} 264。
而 i n t int int 的存储范围是 2 32 2^{32} 232
要选择合适的类型。
Part 6 取模
- 模数一定要看清楚,例如之前我们校内考试,很多人把 993244853 993244853 993244853 抄成了 998244353 998244353 998244353。
- 取模是无时无刻的,防止爆 l o n g l o n g long long longlong 或 i n t int int。
Part 7 总结
总而言之,就是要仔细再仔细。不要掉以轻心,祝愿大家
C
S
P
−
2023
CSP-2023
CSP−2023 有一个完美的结局。