Coding skills
As title.
ysh
Resource
Process
拿到題目
要尬麻
1
讀題敘
除非你要通靈
3
確認範圍,是否能暴力解
5
猜演算法
2
看輸入
4
二分搜?
有信心
做啊 等什麼
沒信心
刻暴力解
通靈就是這樣來的

善用
assert
為什麼?
因為區賽題目超級爛
當然也可以用這招作假解
回頭譴責某 TOI
int main() {
int n;cin>>n;
assert(n <= 2e5);
return 0;
}Code
int main() {
int n;cin>>n;
assert(n <= 2e5);
return 0;
}時間剪枝
邪教,但有試有機會
舉例來說,時限為一秒
#include<bits/stdc++.h>
using namespace std;
int main() {
auto t = clock();
int ans_for_now = INT_MAX;
int n;cin>>n;
vector<int>f(n);
srand(time(NULL));
for(int &i : f) {
cin>>i;
}
while(clock() - t < 900) {
int l = rand() % n;
int r = rand() % n;
ans_for_now = max(ans_for_now,abs(f.at(l) - f.at(r)));
}
cout<<ans;
return 0;
}#define int long long
一樣是邪教
#include<bits/stdc++.h>
using namespace std;
int main() {
int a;cin>>a;
cout<<a;
}#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int a;cin>>a;
cout<<a;
}segment fault?
可憐
Program received signal SIGSEGV, Segmentation fault.terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc還記得
跟
嗎
恭喜你獲得了傳說中的 RE
有可能…
- 陣列戳出去
- 遞迴過深
- 陣列開太大
- 記憶體不足
-
while沒有中止 - 程式心情不好 不想工作
但錯誤訊息只有這樣是要怎麼 debug ??
有三招
- 第一招 火眼金睛 直接debug
- 第二招 STL .at()
如果你使用的是 vector 或 deque 可以將 f[i] 改成 f.at(i)
這樣程式在呼叫時會先確認你是不是有戳出去,有的話會直接將程式中止,並輸出以下訊息:
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 4294967295) >= this->size() (which is 3)GDB
究極黑魔法
Coding skills
By ysh勝皓
Coding skills
- 15