From e9fa69af9d7f1583cb5ddad837c04bb1b03d7939 Mon Sep 17 00:00:00 2001 From: Shuhao Zhang <594422141@qq.com> Date: Tue, 26 Feb 2019 00:41:11 +0800 Subject: [PATCH] add some tricks in Luogu Daily#86 --- docs/intro/common-tricks.md | 208 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 174 insertions(+), 34 deletions(-) diff --git a/docs/intro/common-tricks.md b/docs/intro/common-tricks.md index ce817fb2..44d9bd65 100644 --- a/docs/intro/common-tricks.md +++ b/docs/intro/common-tricks.md @@ -1,36 +1,176 @@ 本页面主要分享一下在竞赛中的小技巧。 -1. 利用局部性 - 局部性是指程序倾向于引用邻近于其他最近引用过的数据项的数据项,或者最近引用过的数据项本身。局部性分为时间局部性和空间局部性。 - - - 消除循环中的低效率,比如遍历字符串的时候: - ```c++ - for (int i = 0; i < strlen(s); ++i) - ; - // 不如 - int len = strlen(s); - for (int i = 0; i < len; ++i) - ; - ``` - - 循环展开。通过适当的循环展开可以减少整个计算中关键路径上的操作数量 - ```c++ - for (int i = 0; i < n; ++i) { - res = res OP a[i]; - } - // 不如 - int i; - for (i = 0; i < n; i += 2) { - res = res OP a[i]; - res = res OP a[i + 1]; - } - for (; i < n; ++i) { - res = res OP a[i]; - } - ``` - - 重新结合变换 - 增加了可以并行执行的运算数量 - ```c++ - for (int i = 0; i < n; ++i) res = (res OP a[i])OP a[i + 1]; - // 不如 - for (int i = 0; i < n; ++i) res = res OP(a[i] OP a[i + 1]); - ``` +注:本页面部分内容最初发表于[洛谷日报 #86](https://studyingfather.blog.luogu.org/some-coding-tips-for-oiers),由原作者整理并搬运至此,略有删改。 + +## 利用局部性 + +局部性是指程序倾向于引用邻近于其他最近引用过的数据项的数据项,或者最近引用过的数据项本身。局部性分为时间局部性和空间局部性。 + +- 消除循环中的低效率,比如遍历字符串的时候: + ```c++ + for (int i = 0; i < strlen(s); ++i) + ; + // 不如 + int len = strlen(s); + for (int i = 0; i < len; ++i) + ; + ``` +- 循环展开。通过适当的循环展开可以减少整个计算中关键路径上的操作数量 + ```c++ + for (int i = 0; i < n; ++i) { + res = res OP a[i]; + } + // 不如 + int i; + for (i = 0; i < n; i += 2) { + res = res OP a[i]; + res = res OP a[i + 1]; + } + for (; i < n; ++i) { + res = res OP a[i]; + } + ``` +- 重新结合变换,增加了可以并行执行的运算数量 + ```c++ + for (int i = 0; i < n; ++i) res = (res OP a[i])OP a[i + 1]; + // 不如 + for (int i = 0; i < n; ++i) res = res OP(a[i] OP a[i + 1]); + ``` + +## 善用namespace + +使用namespace能使程序可读性更好,便于调试。 + +```cpp +#include +#include +#include +#include +using namespace std; +long long n,m,a[100005],p[100005],aw[100005],atk[100005]; +namespace one_game +{ + //其实namespace里也可以声明变量 + void solve() + { + for(int y=0;;y++) + if((a[1]+p[1]*y)%atk[1]==0) + { + cout<<(a[1]+p[1]*y)/atk[1]<>T; + while(T--) + { + memset(a,0,sizeof(a)); + memset(p,0,sizeof(p)); + memset(aw,0,sizeof(aw)); + memset(atk,0,sizeof(atk)); + cin>>n>>m; + for(int i=1;i<=n;i++) + cin>>a[i]; + for(int i=1;i<=n;i++) + cin>>p[i]; + for(int i=1;i<=n;i++) + cin>>aw[i]; + for(int i=1;i<=m;i++) + cin>>atk[i]; + if(n==1&&m==1)one_game::solve();//solve 8-13 + else if(p[1]==1)p_1::solve();//solve 1-4 or 14-15 + else cout<<-1< +#include +int main() +{ + //For Windows + //对拍时不开文件输入输出 + //当然,这段程序也可以改写成批处理的形式 + while(1) + { + system("gen > test.in");//数据生成器将生成数据写入输入文件 + system("test1.exe < test.in > a.out");//获取程序1输出 + system("test2.exe < test.in > b.out");//获取程序2输出 + if(system("fc a.out b.out")) + { + //该行语句比对输入输出 + //fc返回0时表示输出一致,否则表示有不同处 + system("pause");//方便查看不同处 + return 0; + //该输入数据已经存放在test.in文件中,可以直接利用进行调试 + } + } +} +``` -- 2.11.0