Tính cục bộ nghĩa là chương trình có xu hướng truy cập dữ liệu gần dữ liệu vừa truy cập, hoặc chính dữ liệu vừa truy cập. Có cục bộ thời gian và cục bộ không gian.
for(inti=0;i<N;i++){// Nội dung vòng lặp}// Rút gọn bằng macro#define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)// Khi dùng: f(i, 0, N)// a là container STLf(i,0,a.size()){...}
Macro hữu ích khác:
1
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
// NOI 2018 屠龙勇士, code 40 điểm phần分#include<algorithm>#include<cmath>#include<cstring>#include<iostream>usingnamespacestd;longlongn,m,a[100005],p[100005],aw[100005],atk[100005];namespaceone_game{// Trong namespace cũng có thể khai báo biếnvoidsolve(){for(inty=0;;y++)if((a[1]+p[1]*y)%atk[1]==0){cout<<(a[1]+p[1]*y)/atk[1]<<endl;return;}}}// namespace one_gamenamespacep_1{voidsolve(){if(atk[1]==1){// solve 1-2sort(a+1,a+n+1);cout<<a[n]<<endl;return;}elseif(m==1){// solve 3-4longlongk=atk[1],kt=ceil(a[1]*1.0/k);for(inti=2;i<=n;i++)k=aw[i-1],kt=max(kt,(longlong)ceil(a[i]*1.0/k));cout<<k<<endl;}}}// namespace p_1intmain(){intT;cin>>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(inti=1;i<=n;i++)cin>>a[i];for(inti=1;i<=n;i++)cin>>p[i];for(inti=1;i<=n;i++)cin>>aw[i];for(inti=1;i<=m;i++)cin>>atk[i];if(n==1&&m==1)one_game::solve();// solve 8-13elseif(p[1]==1)p_1::solve();// solve 1-4 or 14-15elsecout<<-1<<endl;}return0;}
Dùng macro để debug
Khi test local thường thêm debug prints; nộp OJ phải xóa, tốn thời gian. Có thể dùng macro:
12345678
#define DEBUG#ifdef DEBUG// chạy khi DEBUG được định nghĩa#endif// hoặc#ifndef DEBUG// chạy khi DEBUG chưa được định nghĩa#endif
#ifdef kiểm tra có định nghĩa hay không; #ifndef thì ngược lại.
Chỉ cần để code debug trong #ifdef DEBUG, code nộp trong #ifndef DEBUG; khi nộp chỉ cần comment #define DEBUG. Hoặc dùng -DDEBUG khi compile. Nhiều OJ bật -DONLINE_JUDGE; tận dụng để tiết kiệm thời gian.
Đối拍
Đối拍 là phương pháp kiểm tra bằng cách so sánh output của hai chương trình để判断 đúng sai.
Cần chạy nhiều lần, nên dùng batch để tự động.
Cụ thể cần generator và hai chương trình cần so sánh.
Mỗi lần chạy generator ghi input, rồi redirect cho hai chương trình, ghi output vào file, cuối cùng dùng fc (Windows) hoặc diff (Linux) để so sánh. Nếu sai, dùng luôn input đó để debug.
Khung chương trình đối拍:
1 2 3 4 5 6 7 8 91011121314151617181920
#include<cstdio>#include<cstdlib>intmain(){// Cho Windows// Đối拍 không mở file IO// Có thể viết thành batchwhile(true){system("gen > test.in");// Generator ghi dữ liệusystem("test1.exe < test.in > a.out");// Output chương trình 1system("test2.exe < test.in > b.out");// Output chương trình 2if(system("fc a.out b.out")){// Dòng này so sánh output// fc trả 0 là giống, khác là có khác biệtsystem("pause");// Dễ xem khác biệtreturn0;// Dữ liệu nằm trong test.in, có thể dùng debug}}}
Bộ nhớ池
Khi cấp phát động, dùng new/malloc thường xuyên sẽ tốn thời gian và tạo碎片, có thể làm TLE/MLE.
Giải pháp: “memory pool” — cấp phát sẵn một khối, khi cần thì lấy trong đó.
Trong OI, thường có thể ước lượng bộ nhớ tối đa và cấp phát một lần.
Ví dụ:
1 2 3 4 5 6 7 8 91011
// Cấp phát mảng int 32-bit động:int*newarr(intsz){staticintpool[MAXN],*allocp=pool;returnallocp+=sz,allocp-sz;}// Cây đoạn mở节点 động:Node*newnode(){staticNodepool[MAXN<<1],*allocp=pool-1;return++allocp;}
Last updated on this page:, Update history Found an error? Want to help improve? Edit this page on GitHub! Contributors to this page:H-J-Granger, Ir1d, ChungZH, Marcythm, StudyingFather, billchenchina, Suyun514, Psycho7, greyqz, Xeonacid, partychicken All content on this page is provided under the terms of the CC BY-SA 4.0 and SATA license, additional terms may apply