wiwiho
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # 競賽基本知識 Wiwi Ho ###### tags: `CRC` --- https://hackmd.io/@wiwiho/crc1082algo ![](https://i.imgur.com/KqGWkCw.png) --- # 複雜度分析 --- # 什麼是複雜度? ---- 有一個函數 $f(n)$ 它的值跟 $n$ 有什麼關係? ---- $f(n)=n^2+2n-1$ ---- $n^2$ 對 $f(n)$ 的值影響最大 $\Rightarrow$ $f(n) \in O(n^2)$ ---- ## Big-O notation 對於兩個函數 $f(n)$ 和 $g(n)$ 若存在正數 $c$ 和 $n_0$ 滿足所有的 $n \geq n_0$ 都符合 $f(n) \leq cg(n)$ 則 $f(n) \in O(g(n))$ ---- ![](https://i.imgur.com/IGmX8TO.png) ---- ## 複雜度怎麼求 只留最高次項 且把係數拿掉 ---- ## 比大小 複雜度越大 表示函數值成長越快 ---- ## 怎麼比(嚴謹定義) $\lim_{n \to \infty} \frac{g(x)}{f(x)}$ - 無限大:$O(g(n))>O(f(n))$ - $0$:$O(g(n))<O(f(n))$ - 非零常數:$O(g(n))=O(f(n))$ ---- 如果 $O(g(n))>O(f(n))$ $O(f(n)) \in O(g(n))$ $\Rightarrow 2n^2+n-1 \in O(2n^2+n-1) \in O(n^2) \in O(n^3)$ ---- 複雜度非唯一 通常會選最小而且寫出來最簡單的 --- # 用詞 ---- 常數:$O(1)$ ---- 線性:$O(n)$ ---- 其他應該很好懂 (? e.g. 對數($O(\log n)$)、根號($\sqrt{n}$)…… --- # STL 的複雜度 ---- 查表 cppreference.com --- # 時間複雜度 ---- 表示程式的執行時間如何隨輸入的數值成長 $\Rightarrow$ 用來比較演算法好壞的方法 ---- ## 會用多少時間? ---- 不能準確計算 ---- 把所有參數的最大值代入後 約 $10^8$ 到 $10^9$ 為一秒 ---- ## 範例 ---- ```cpp int n; cin >> n; vector<int> v(n); for(int i = 0; i < n; i++) cin >> v[i]; for(int i = 0; i < n - 1; i++){ //迴圈 1 for(int j = 0; j < n - i - 1; j++){ //迴圈 2 if(v[j] > v[j + 1]) swap(v[j], v[j + 1]); } } for(int i = 0; i < n; i++) cout << v[i] << " "; cout << "\n"; ``` ---- ```cpp int n; cin >> n; stack<int> st; for(int i = 0; i < n; i++){ int t; cin >> t; while(!st.empty() && st.top() <= t) st.pop(); if(st.empty()) cout << "-1 "; else cout << st.top() << " ", st.pop(); st.push(t); } cout << "\n"; ``` --- # 常數 ---- 被丟掉的常數 ---- 常數重要嗎? ---- 當然重要! ---- ```cpp for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ for(int k = 0; k < 10; k++) /*...*/; } } ``` ---- 時間複雜度 $O(n^2)$ 如果 $n \leq 10000$ $n^2=10^8$ ---- 好像勉強還行? ---- 那個 10 呢? $10n^2=10^9$ ---- 可能不太行 ---- 如果常數會造成時間剛好超過 那就很重要 ---- ## 看不到的常數 ---- 加、減、乘、除、模都是 $O(1)$ 但除和模需要比較多時間 ---- unordered_set、unordered_map 的插入刪除尋找都是 $O(1)$ 但常數很大 --- # 輸入輸出 --- # 各種流 ---- 輸入流、輸出流? ---- 什麼是流? ---- ![](https://i.imgur.com/B25QcX0.png) ---- 輸出流:把東西給它,它會負責輸出到某地方 輸入流:跟它要一個東西,它會負責從某個地方拿給你 ---- ![](https://i.imgur.com/52KjFsS.png) --- # 標準流 ---- - 標準輸入流 - cin - 標準輸出流 - cout - 標準錯誤流 - cerr ---- 預設:在 terminal 輸入輸出 可以重新導向成在檔案輸入輸出 ---- ## 錯誤流 一種輸出流 用來輸出錯誤訊息 不會被 judge 讀到 ---- ## 重新導向 ---- ## 在程式碼 ```cpp freopen("filename", "w", stdout); freopen("filename", "w", stderr); freopen("filename", "r", stdin); ``` ---- ## 在 terminal ``` command < in command > out command 2> err command < in > out 2> err command < in > out ``` ---- ## 緩衝區 ---- ## cin 輸入的東西先到緩衝區 程式讀取的時候會從緩衝區拿資料 ---- ## cout 輸出的東西先到緩衝區 flush 後再真的輸出到它該去的地方 ---- ## cerr 沒有緩衝區 會馬上輸出到它該去的地方 --- ## stringstream ---- 一種 iostream $\Rightarrow$ 是輸入流也是輸出流 ---- ```cpp stringstream ss; ss << 5; int t; ss >> t; cout << t << "\n"; ``` --- # 輸入輸出優化 ---- ![](https://i.imgur.com/gTnljTj.png) ---- 效率太差? ---- 來看看它為什麼差 ---- 輸出到 terminal 或檔案裡的常數很大 ---- ## cout 自動 flush - endl = << '\n' << flush - 使用 cin 時會自動 flush cout - 使用 cerr 時會自動 flush cout - 程式結束時強制 flush ---- - 不要用 endl - cin.tie(0) - cerr.tie(0) - 在最後讓程式自己 flush 就好了 ---- ## 歷史因素 要和 scanf/printf 同步 ---- ios_base::sync_with_stdio(false) --- # 運算子重載 ---- ```cpp ostream& operator<<(ostream& o, pair<int, int> p){ return o << p.first << " " << p.second; } istream& operator>>(istream& i, pair<int, int> p){ return i >> p.first >> p.second; } ``` --- # 輸入格式 ---- ## EOF ---- EOF = End Of Line ---- 某些題目: 一個檔案有多筆測資 而且不告訴你有幾筆 ---- ```cpp while(cin >> /*...*/){ //... } ``` ---- ## terminal 不是 file? 按 ctrl+z 或 ctrl+d 製造 EOF ---- ## 輸入一整行 ---- ```cpp string s; getline(in, s); //in 是輸入流 ``` ---- ## 跟 >> 混用 ---- ``` abc abcd abcd ``` ```cpp string a, b; cin >> a; getline(cin, b); ``` ---- 預期:a=abc,b=abcd abcd 實際:a=abc,b=空字串 ---- 多 getline 一次 ---- ## 忽略輸入 ZJ c268: 給你一堆數字,找三個數字當三角形邊長 ---- $O(n \log n)$ 可以嗎? ---- $n \leq 10^7$ 太多了吧 ---- $n \geq 50$ 保證有解 所以超過 50 項就可以不用讀了 ---- 可是一個檔案多測資,怎麼辦? ---- 把輸入忽略掉 ```cpp cin.ignore(1000000000, '\n'); ``` --- # 輸出格式 ---- ## 小數位數 ---- ```cpp cout << fixed << setprecision(10) << ...; ``` ---- ## 數字寬度 ---- ```cpp cout << setw(n) << setfill(c) << ...; ``` --- # 前置處理器 --- # 什麼是前置處理器? ---- 在編譯之前,會先做前置處理 ---- ```cpp #include ... #define ... ``` 都是前置處理器的指令 ---- ## 什麼是前置處理? 根據指令修改程式碼 ---- ## include ---- 把別的檔案的內容在該處貼上 ---- ## 引號、角括號? ```cpp #include <iostream> #include "test.cpp" ``` ---- 找檔案的地方不同 - 角括號:找函式庫 - 引號:找同個目錄 --- ## define ---- macro/ 巨集 / 宏 ---- ```cpp //#define NDEBUG #include <bits/stdc++.h> #include <bits/extc++.h> #define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define iter(a) a.begin(), a.end() #define riter(a) a.rbegin(), a.rend() #define lsort(a) sort(iter(a)) #define gsort(a) sort(riter(a)) #define mp(a, b) make_pair(a, b) #define pb(a) push_back(a) #define eb(a) emplace_back(a) #define pf(a) push_front(a) #define pob pop_back() #define pof pop_front() #define F first #define S second #define printv(a, b) {bool pvaspace=false; \ for(auto pva : a){ \ if(pvaspace) b << " "; pvaspace=true;\ b << pva;\ }\ b << "\n";} #define pii pair<int, int> #define pll pair<ll, ll> #define tiii tuple<int, int, int> #define mt make_tuple #define gt(t, i) get<i>(t) #define iceil(a, b) ((a) / (b) + !!((a) % (b))) //#define TEST typedef long long ll; typedef unsigned long long ull; using namespace std; using namespace __gnu_pbds; const ll MOD = 1000000007; const ll MAX = 2147483647; template<typename A, typename B> ostream& operator<<(ostream& o, pair<A, B> p){ return o << '(' << p.F << ',' << p.S << ')'; } int main(){ StarBurstStream return 0; } ``` ---- ```cpp #define a b ``` 把所有單字 a 換成 b ---- 也可以用 function ```cpp #define f(a) a+4 ``` ---- 換行 ```cpp #define printv(a, b) {bool pvaspace=false; \ for(auto pva : a){ \ if(pvaspace) b << " "; pvaspace=true;\ b << pva;\ }\ ``` ---- 一樣嗎? ```cpp #define f(a) a + 5 //... cout << (3 * f(1)); ``` ```cpp int f(int a){ return a + 5; } //... cout << (3 * f(1)); ``` ---- ## 條件判斷 ---- ```cpp #if ... ... #elif ... ... #else ... #endif ``` ---- 符合條件的地方才會留下來 ---- ```cpp #if defined(a) #ifdef (a) #if !defined(a) #ifndef (a) ``` 判斷 a 有沒有被 define 過 --- 去寫練習題

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully