SlideShare uma empresa Scribd logo
1 de 59
Baixar para ler offline
Efficient Algorithm
      郭至軒(KuoE0)
     KuoE0.tw@gmail.com
          KuoE0.ch
Attribution-ShareAlike 3.0 Unported
           (CC BY-SA 3.0)

  http://creativecommons.org/licenses/by-sa/3.0/

              Latest update: Feb 27, 2013
Time Complexity
最大連續和

     有一長度為 n 的數列 A1, A2, A3, ...,
          An-1, An,求其最大連續區間和。

A1   A2   A3      ...         An-2   An-1   An


                        }
                        MAX
int MAX = A[ 1 ];
for ( int i = 1; i <= n; ++i )
 for ( int j = i; j <= n; ++j ) {
   int sum = 0;
   for ( int k = i; k <= j; ++k )
    sum += A[ k ];
   MAX = max( MAX, sum );
 }
int MAX = A[ 1 ];
for ( int i = 1; i <= n; ++i )
 for ( int j = i; j <= n; ++j ) {
   int sum = 0;
   for ( int k = i; k <= j; ++k )
    sum += A[ k ];
   MAX = max( MAX, sum );
 }




        核心運算:加法
int MAX = A[ 1 ];
for ( int i = 1; i <= n; ++i )
 for ( int j = i; j <= n; ++j ) {
   int sum = 0;
   for ( int k = i; k <= j; ++k )
    sum += A[ k ];
   MAX = max( MAX, sum );
 }
int MAX = A[ 1 ];
   for ( int i = 1; i <= n; ++i )
    for ( int j = i; j <= n; ++j ) {
      int sum = 0;
      for ( int k = i; k <= j; ++k )
       sum += A[ k ];
      MAX = max( MAX, sum );
    }


                    n   n
                              n(n + 1)(n + 2)
加法次數: T (n) = ∑ ∑ j − i + 1 =
              i=1 j=i                6
n   n
                        n(n + 1)(n + 2)
T (n) = ∑ ∑ j − i + 1 =
        i=1 j=i                6
n   n
                        n(n + 1)(n + 2)
T (n) = ∑ ∑ j − i + 1 =                   三次式
        i=1 j=i                6
n   n
                        n(n + 1)(n + 2)
T (n) = ∑ ∑ j − i + 1 =                          三次式
        i=1 j=i                6


                    n3         n2          n     1

       n=10        10 3       10 2        10 1   1

      n=10 2       10 6       10 4        10 2   1

      n=10 3       10 9       10 6        10 3   1
n^3   n^2        n   1

7000




5250




3500




1750




  0
       3   7   11   13          17           19
最大指數次項影響最大
 Time Complexity:   O(n3)
不需精確分析

進行精確分析較耗時間!
不需精確分析

進行精確分析較耗時間!


  略估迴圈次數
int MAX = A[ 1 ];
for ( int i = 1; i <= n; ++i )
 for ( int j = i; j <= n; ++j ) {
   int sum = 0;
   for ( int k = i; k <= j; ++k )
    sum += A[ k ];
   MAX = max( MAX, sum );
 }


                      最多次數
  第 1 層迴圈                n
  第 2 層迴圈                n
  第 3 層迴圈                n
n×n×n=           n 3

Time Complexity:   O(n3)
前處理優化
最大連續和

     有一長度為 n 的數列 A1, A2, A3, ...,
          An-1, An,求其最大連續區間和。

A1   A2   A3      ...         An-2   An-1   An


                        }
                        MAX
設數列 S 為數列 A 的累積和,
因此 Si = A1 + A2 + ... + Ai,則
Ai + Ai+1 + ... + Aj = Sj - Si-1。
設數列 S 為數列 A 的累積和,
   因此 Si = A1 + A2 + ... + Ai,則
    Ai + Ai+1 + ... + Aj = Sj - Si-1。



求區間 [i,j] 的連續和        需要 O(1) 的時間。
A1   A2   A3   A4   A5   A6    A7   A8
S1   3    7    -1   -3   9    -10   1    2    3
S2   3    7    -1   -3   9    -10   1    2    10
S3   3    7    -1   -3   9    -10   1    2    9
S4   3    7    -1   -3   9    -10   1    2    6
S5   3    7    -1   -3   9    -10   1    2    15
S6   3    7    -1   -3   9    -10   1    2    5
S7   3    7    -1   -3   9    -10   1    2    6
S8   3    7    -1   -3   9    -10   1    2    8
int MAX = A[ 1 ];
S[ 0 ] = 0;
for ( int i = 1; i <= n; ++i )
 S[ i ] = S[ i - 1 ] + A[ i ];
for ( int i = 1; i <= n; ++i )
 for ( int j = i; j <= n; ++j ) {
   MAX = max( MAX, S[ j ] - S[ i - 1 ] );
 }
int MAX = A[ 1 ];
S[ 0 ] = 0;
for ( int i = 1; i <= n; ++i )
 S[ i ] = S[ i - 1 ] + A[ i ];
                                O(n)
for ( int i = 1; i <= n; ++i )      O(n2)
 for ( int j = i; j <= n; ++j ) {
   MAX = max( MAX, S[ j ] - S[ i - 1 ] );
 }




     最大指數次項:O(n2)
Divide & Conquer
     分而治之
劃分為相同的子問題
劃分為相同的子問題




 遞迴求解子問題
劃分為相同的子問題




 遞迴求解子問題




將子問題的解合併
最大連續和

     有一長度為 n 的數列 A1, A2, A3, ...,
          An-1, An,求其最大連續區間和。

A1   A2   A3      ...         An-2   An-1   An


                        }
                        MAX
切割問題
A1   A2   A3    ...   An-2   An-1   An
切割問題
     A1    A2        A3                          ...                       An-2      An-1   An




     A1         A2         ...     An/2                         An/2+1 An/2+2      ...      An




A1   ...   An/4           An/4+1   ...    An/2         An/2+1    ...   A3n/4      A3n/4+1   ...   An




A1         An/4           An/4+1          An/2         An/2+1          A3n/4      A3n/4+1         An
求解問題

遞迴直到數列   剩一元素,直接回傳該數值。


  有一元素時,該數值為最大連續和。
合併子問題解

選擇子問題中最優解!
       ...   ...



  10               11
合併子問題解

選擇子問題中最優解!
       ...   ...



  10               11
But....

A1   A2        A3                  ...                An-2     An-1   An




A1        A2        ...   An/2             An/2+1 An/2+2     ...      An
But....
                    答案可能橫跨兩個區間!


A1   A2        A3                  ...                An-2     An-1   An




A1        A2        ...   An/2             An/2+1 An/2+2     ...      An
How-to

5   -9   3    1   3   -10   3   3
How-to

5    -9   3    1   3   -10   3   3

    最大連續和:5
How-to

5    -9   3    1    3   -10   3   3


    最大連續和:5        最大連續和:6
How-to

5    -9   3    1    3   -10   3   3


    最大連續和:5        最大連續和:6

          當前最大連續和:6
How-to

5   -9   3    1   3   -10   1   1




         當前最大連續和:6
How-to

   5   -9   3    1   3   -10   1   1


自右而左找最大連續和
            當前最大連續和:6
How-to

   5   -9   3    1   3   -10   1   1


     連續和:4
自右而左找最大連續和
            當前最大連續和:6
How-to

   5   -9   3    1   3   -10   1   1


     連續和:4
自右而左找最大連續和           自左而右找最大連續和
            當前最大連續和:6
How-to

   5   -9   3    1   3   -10   1   1


     連續和:4
自右而左找最大連續和           連續和:3
                     自左而右找最大連續和
            當前最大連續和:6
How-to

    5   -9    3   1   3   -10   1   1


     連續和:4
自右而左找最大連續和  連續和:3
            自左而右找最大連續和
         連續和:7
             當前最大連續和:6
How-to

    5   -9    3   1   3   -10   1   1


     連續和:4
自右而左找最大連續和  連續和:3
            自左而右找最大連續和
         連續和:7
             當前最大連續和:6
             最終最大連續和:7
int maxsum( int L, int R ) {
  if ( R - L == 1 )
   return A[ L ];
  int mid = ( L + R ) / 2;
  int MAX = max( maxsum( L, mid ), maxsum( mid,
  R ) );
  int tempL = 0, tempR = 0;
  for ( int i = mid - 1, sum = 0; i >= L; --i )
   tempL = max( tempL, sum += A[ i ] );
  for ( int i = mid, sum = 0; i < R; ++i )
   tempR = max( tempR, sum += A[ i ] );
  return max( MAX, tempL + tempR );
}
Time Complexity
              total time
Time Complexity
              total time

                   n

              2 * (n/2) = n

                   ...

              n * (n/n) = n
The height




             }
             log2n
Time Complexity: O(n×log2n)
Time Complexity of Divide & Conquer


   子問題扣除遞迴呼叫的時間複雜度 C(n)

                 遞迴呼叫層數 H(n)



   Time Complexity: C(n) × H(n)
Time Comparison
        n3      n2     nlog2n


100     0.15    0.02    0.01


1000   123.86   1.36    0.14
                        單位:秒
從測試資料判斷演算法
   假設機器 1 秒可以執行 106 個指令,...


時間複雜度   n!   2n   n3     n2     nlog2n        n


最大規模    9    20   10 2   10 3   7.5 × 10 4   10 6
另一種說法
   假設機器 1 秒可以執行 108 個指令,...


時間複雜度   n!    2n   n3    n2     nlog2n        n


最大規模    11    26   464   10 4   4.5 × 10 6   10 8
Practice Now
10245 - The Closest Pair Problem
Reference
• 提升程式設計的邏輯思考力 (link)
Thank You for Your
    Listening.

Mais conteúdo relacionado

Mais procurados

实验九 用Mathematica软件求函数偏导数与多元函数的极值
实验九  用Mathematica软件求函数偏导数与多元函数的极值实验九  用Mathematica软件求函数偏导数与多元函数的极值
实验九 用Mathematica软件求函数偏导数与多元函数的极值guestfe33f0e
 
Equalization in digital communication
Equalization in digital communicationEqualization in digital communication
Equalization in digital communicationPei-Che Chang
 
03.第三章用Matlab求极值
03.第三章用Matlab求极值03.第三章用Matlab求极值
03.第三章用Matlab求极值Xin Zheng
 
P3 teoria de exponentes I solucion
P3   teoria de exponentes I solucionP3   teoria de exponentes I solucion
P3 teoria de exponentes I solucionlutv223
 
Limit and continuity
Limit and continuityLimit and continuity
Limit and continuityJB Online
 
241525第三章初等代数运算命令与例题
241525第三章初等代数运算命令与例题241525第三章初等代数运算命令与例题
241525第三章初等代数运算命令与例题gwadhysys
 

Mais procurados (9)

实验九 用Mathematica软件求函数偏导数与多元函数的极值
实验九  用Mathematica软件求函数偏导数与多元函数的极值实验九  用Mathematica软件求函数偏导数与多元函数的极值
实验九 用Mathematica软件求函数偏导数与多元函数的极值
 
Equalization in digital communication
Equalization in digital communicationEqualization in digital communication
Equalization in digital communication
 
Sum
SumSum
Sum
 
03.第三章用Matlab求极值
03.第三章用Matlab求极值03.第三章用Matlab求极值
03.第三章用Matlab求极值
 
Graduate report
Graduate reportGraduate report
Graduate report
 
Example
ExampleExample
Example
 
P3 teoria de exponentes I solucion
P3   teoria de exponentes I solucionP3   teoria de exponentes I solucion
P3 teoria de exponentes I solucion
 
Limit and continuity
Limit and continuityLimit and continuity
Limit and continuity
 
241525第三章初等代数运算命令与例题
241525第三章初等代数运算命令与例题241525第三章初等代数运算命令与例题
241525第三章初等代数运算命令与例题
 

Semelhante a [ACM-ICPC] Efficient Algorithm

指考乙公式
指考乙公式指考乙公式
指考乙公式zoayzoay
 
Scilab introduction(Scilab 介紹)
Scilab introduction(Scilab 介紹)Scilab introduction(Scilab 介紹)
Scilab introduction(Scilab 介紹)JIANG MING-LI
 
ejercicios de integrales indefinidas (Analisis matematico )
ejercicios de integrales indefinidas (Analisis matematico )ejercicios de integrales indefinidas (Analisis matematico )
ejercicios de integrales indefinidas (Analisis matematico )charx_rex
 
指考甲公式
指考甲公式指考甲公式
指考甲公式zoayzoay
 
學測公式
學測公式學測公式
學測公式zoayzoay
 
福建省福州大学2019年暑假物理竞赛夏令营-物理奥赛进阶之路:0-1+微积分初步+40张ppt.pptx
福建省福州大学2019年暑假物理竞赛夏令营-物理奥赛进阶之路:0-1+微积分初步+40张ppt.pptx福建省福州大学2019年暑假物理竞赛夏令营-物理奥赛进阶之路:0-1+微积分初步+40张ppt.pptx
福建省福州大学2019年暑假物理竞赛夏令营-物理奥赛进阶之路:0-1+微积分初步+40张ppt.pptxyantingguo2008
 
实验二 用Mathmatica软件求极限
实验二  用Mathmatica软件求极限实验二  用Mathmatica软件求极限
实验二 用Mathmatica软件求极限guestfe33f0e
 
实验二 用Mathmatica软件求极限
实验二  用Mathmatica软件求极限实验二  用Mathmatica软件求极限
实验二 用Mathmatica软件求极限Xin Zheng
 
C1 discrete time signals and systems in the time-domain
C1 discrete time signals and systems in the time-domainC1 discrete time signals and systems in the time-domain
C1 discrete time signals and systems in the time-domainPei-Che Chang
 
C语言学习100例实例程序
C语言学习100例实例程序C语言学习100例实例程序
C语言学习100例实例程序yiditushe
 
Algorithms.exercises.solution
Algorithms.exercises.solutionAlgorithms.exercises.solution
Algorithms.exercises.solutionRegina Long
 
龍騰[掌握]數學A複習講義
龍騰[掌握]數學A複習講義龍騰[掌握]數學A複習講義
龍騰[掌握]數學A複習講義lungtengtech
 
龍騰[突破]數學B複習講義
龍騰[突破]數學B複習講義龍騰[突破]數學B複習講義
龍騰[突破]數學B複習講義lungtengtech
 

Semelhante a [ACM-ICPC] Efficient Algorithm (20)

指考乙公式
指考乙公式指考乙公式
指考乙公式
 
Scilab introduction(Scilab 介紹)
Scilab introduction(Scilab 介紹)Scilab introduction(Scilab 介紹)
Scilab introduction(Scilab 介紹)
 
ejercicios de integrales indefinidas (Analisis matematico )
ejercicios de integrales indefinidas (Analisis matematico )ejercicios de integrales indefinidas (Analisis matematico )
ejercicios de integrales indefinidas (Analisis matematico )
 
指考甲公式
指考甲公式指考甲公式
指考甲公式
 
學測公式
學測公式學測公式
學測公式
 
福建省福州大学2019年暑假物理竞赛夏令营-物理奥赛进阶之路:0-1+微积分初步+40张ppt.pptx
福建省福州大学2019年暑假物理竞赛夏令营-物理奥赛进阶之路:0-1+微积分初步+40张ppt.pptx福建省福州大学2019年暑假物理竞赛夏令营-物理奥赛进阶之路:0-1+微积分初步+40张ppt.pptx
福建省福州大学2019年暑假物理竞赛夏令营-物理奥赛进阶之路:0-1+微积分初步+40张ppt.pptx
 
实验二 用Mathmatica软件求极限
实验二  用Mathmatica软件求极限实验二  用Mathmatica软件求极限
实验二 用Mathmatica软件求极限
 
实验二 用Mathmatica软件求极限
实验二  用Mathmatica软件求极限实验二  用Mathmatica软件求极限
实验二 用Mathmatica软件求极限
 
C1 discrete time signals and systems in the time-domain
C1 discrete time signals and systems in the time-domainC1 discrete time signals and systems in the time-domain
C1 discrete time signals and systems in the time-domain
 
1 1指數
1 1指數1 1指數
1 1指數
 
Dp1007
Dp1007Dp1007
Dp1007
 
Ppt 1-50
Ppt 1-50Ppt 1-50
Ppt 1-50
 
Ch5 範例
Ch5 範例Ch5 範例
Ch5 範例
 
3 1等差與等比
3 1等差與等比3 1等差與等比
3 1等差與等比
 
Ch2
Ch2Ch2
Ch2
 
C语言学习100例实例程序
C语言学习100例实例程序C语言学习100例实例程序
C语言学习100例实例程序
 
Algorithms.exercises.solution
Algorithms.exercises.solutionAlgorithms.exercises.solution
Algorithms.exercises.solution
 
龍騰[掌握]數學A複習講義
龍騰[掌握]數學A複習講義龍騰[掌握]數學A複習講義
龍騰[掌握]數學A複習講義
 
龍騰[突破]數學B複習講義
龍騰[突破]數學B複習講義龍騰[突破]數學B複習講義
龍騰[突破]數學B複習講義
 
Ppt 26-50
Ppt 26-50Ppt 26-50
Ppt 26-50
 

Mais de Chih-Hsuan Kuo

[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-selectChih-Hsuan Kuo
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in RustChih-Hsuan Kuo
 
在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。Chih-Hsuan Kuo
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Chih-Hsuan Kuo
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoChih-Hsuan Kuo
 
Pocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSPocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSChih-Hsuan Kuo
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in GeckoChih-Hsuan Kuo
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!Chih-Hsuan Kuo
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...Chih-Hsuan Kuo
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree IsomorphismChih-Hsuan Kuo
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's AlgorithmChih-Hsuan Kuo
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint SetChih-Hsuan Kuo
 

Mais de Chih-Hsuan Kuo (20)

Rust
RustRust
Rust
 
[Mozilla] content-select
[Mozilla] content-select[Mozilla] content-select
[Mozilla] content-select
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in Rust
 
在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。在開始工作以前,我以為我會寫扣。
在開始工作以前,我以為我會寫扣。
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36
 
Use C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in GeckoUse C++ to Manipulate mozSettings in Gecko
Use C++ to Manipulate mozSettings in Gecko
 
Pocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OSPocket Authentication with OAuth on Firefox OS
Pocket Authentication with OAuth on Firefox OS
 
Necko walkthrough
Necko walkthroughNecko walkthrough
Necko walkthrough
 
Protocol handler in Gecko
Protocol handler in GeckoProtocol handler in Gecko
Protocol handler in Gecko
 
面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!面試面試面試,因為很重要所以要說三次!
面試面試面試,因為很重要所以要說三次!
 
應徵軟體工程師
應徵軟體工程師應徵軟體工程師
應徵軟體工程師
 
面試心得分享
面試心得分享面試心得分享
面試心得分享
 
Windows 真的不好用...
Windows 真的不好用...Windows 真的不好用...
Windows 真的不好用...
 
Python @Wheel Lab
Python @Wheel LabPython @Wheel Lab
Python @Wheel Lab
 
Introduction to VP8
Introduction to VP8Introduction to VP8
Introduction to VP8
 
Python @NCKU CSIE
Python @NCKU CSIEPython @NCKU CSIE
Python @NCKU CSIE
 
[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism[ACM-ICPC] Tree Isomorphism
[ACM-ICPC] Tree Isomorphism
 
[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm[ACM-ICPC] Dinic's Algorithm
[ACM-ICPC] Dinic's Algorithm
 
[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set[ACM-ICPC] Disjoint Set
[ACM-ICPC] Disjoint Set
 
[ACM-ICPC] Traversal
[ACM-ICPC] Traversal[ACM-ICPC] Traversal
[ACM-ICPC] Traversal
 

Último

泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书jakepaige317
 
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制jakepaige317
 
educ6506presentationtc3302771-240427173057-06a46de5.pptx
educ6506presentationtc3302771-240427173057-06a46de5.pptxeduc6506presentationtc3302771-240427173057-06a46de5.pptx
educ6506presentationtc3302771-240427173057-06a46de5.pptxmekosin001123
 
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...黑客 接单【TG/微信qoqoqdqd】
 
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptxEDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptxmekosin001123
 
EDUC6506_ClassPresentation_TC330277 (1).pptx
EDUC6506_ClassPresentation_TC330277 (1).pptxEDUC6506_ClassPresentation_TC330277 (1).pptx
EDUC6506_ClassPresentation_TC330277 (1).pptxmekosin001123
 

Último (6)

泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
泽兰应用科学大学毕业证制作/定制国外大学录取通知书/购买一个假的建国科技大学硕士学位证书
 
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
哪里可以购买日本筑波学院大学学位记/做个假的文凭可认证吗/仿制日本大学毕业证/意大利语CELI证书定制
 
educ6506presentationtc3302771-240427173057-06a46de5.pptx
educ6506presentationtc3302771-240427173057-06a46de5.pptxeduc6506presentationtc3302771-240427173057-06a46de5.pptx
educ6506presentationtc3302771-240427173057-06a46de5.pptx
 
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
1.🎉“入侵大学入学考试中心修改成绩”来袭!ALEVEL替考大揭秘,轻松搞定考试成绩! 💥你还在为无法进入大学招生系统而烦恼吗?想知道如何通过技术手段更改...
 
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptxEDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
EDUC6506(001)_ClassPresentation_2_TC330277 (1).pptx
 
EDUC6506_ClassPresentation_TC330277 (1).pptx
EDUC6506_ClassPresentation_TC330277 (1).pptxEDUC6506_ClassPresentation_TC330277 (1).pptx
EDUC6506_ClassPresentation_TC330277 (1).pptx
 

[ACM-ICPC] Efficient Algorithm

  • 1. Efficient Algorithm 郭至軒(KuoE0) KuoE0.tw@gmail.com KuoE0.ch
  • 2. Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/ Latest update: Feb 27, 2013
  • 4. 最大連續和 有一長度為 n 的數列 A1, A2, A3, ..., An-1, An,求其最大連續區間和。 A1 A2 A3 ... An-2 An-1 An } MAX
  • 5. int MAX = A[ 1 ]; for ( int i = 1; i <= n; ++i ) for ( int j = i; j <= n; ++j ) { int sum = 0; for ( int k = i; k <= j; ++k ) sum += A[ k ]; MAX = max( MAX, sum ); }
  • 6. int MAX = A[ 1 ]; for ( int i = 1; i <= n; ++i ) for ( int j = i; j <= n; ++j ) { int sum = 0; for ( int k = i; k <= j; ++k ) sum += A[ k ]; MAX = max( MAX, sum ); } 核心運算:加法
  • 7. int MAX = A[ 1 ]; for ( int i = 1; i <= n; ++i ) for ( int j = i; j <= n; ++j ) { int sum = 0; for ( int k = i; k <= j; ++k ) sum += A[ k ]; MAX = max( MAX, sum ); }
  • 8. int MAX = A[ 1 ]; for ( int i = 1; i <= n; ++i ) for ( int j = i; j <= n; ++j ) { int sum = 0; for ( int k = i; k <= j; ++k ) sum += A[ k ]; MAX = max( MAX, sum ); } n n n(n + 1)(n + 2) 加法次數: T (n) = ∑ ∑ j − i + 1 = i=1 j=i 6
  • 9. n n n(n + 1)(n + 2) T (n) = ∑ ∑ j − i + 1 = i=1 j=i 6
  • 10. n n n(n + 1)(n + 2) T (n) = ∑ ∑ j − i + 1 = 三次式 i=1 j=i 6
  • 11. n n n(n + 1)(n + 2) T (n) = ∑ ∑ j − i + 1 = 三次式 i=1 j=i 6 n3 n2 n 1 n=10 10 3 10 2 10 1 1 n=10 2 10 6 10 4 10 2 1 n=10 3 10 9 10 6 10 3 1
  • 12. n^3 n^2 n 1 7000 5250 3500 1750 0 3 7 11 13 17 19
  • 16. int MAX = A[ 1 ]; for ( int i = 1; i <= n; ++i ) for ( int j = i; j <= n; ++j ) { int sum = 0; for ( int k = i; k <= j; ++k ) sum += A[ k ]; MAX = max( MAX, sum ); } 最多次數 第 1 層迴圈 n 第 2 層迴圈 n 第 3 層迴圈 n
  • 17. n×n×n= n 3 Time Complexity: O(n3)
  • 19. 最大連續和 有一長度為 n 的數列 A1, A2, A3, ..., An-1, An,求其最大連續區間和。 A1 A2 A3 ... An-2 An-1 An } MAX
  • 20. 設數列 S 為數列 A 的累積和, 因此 Si = A1 + A2 + ... + Ai,則 Ai + Ai+1 + ... + Aj = Sj - Si-1。
  • 21. 設數列 S 為數列 A 的累積和, 因此 Si = A1 + A2 + ... + Ai,則 Ai + Ai+1 + ... + Aj = Sj - Si-1。 求區間 [i,j] 的連續和 需要 O(1) 的時間。
  • 22. A1 A2 A3 A4 A5 A6 A7 A8 S1 3 7 -1 -3 9 -10 1 2 3 S2 3 7 -1 -3 9 -10 1 2 10 S3 3 7 -1 -3 9 -10 1 2 9 S4 3 7 -1 -3 9 -10 1 2 6 S5 3 7 -1 -3 9 -10 1 2 15 S6 3 7 -1 -3 9 -10 1 2 5 S7 3 7 -1 -3 9 -10 1 2 6 S8 3 7 -1 -3 9 -10 1 2 8
  • 23. int MAX = A[ 1 ]; S[ 0 ] = 0; for ( int i = 1; i <= n; ++i ) S[ i ] = S[ i - 1 ] + A[ i ]; for ( int i = 1; i <= n; ++i ) for ( int j = i; j <= n; ++j ) { MAX = max( MAX, S[ j ] - S[ i - 1 ] ); }
  • 24. int MAX = A[ 1 ]; S[ 0 ] = 0; for ( int i = 1; i <= n; ++i ) S[ i ] = S[ i - 1 ] + A[ i ]; O(n) for ( int i = 1; i <= n; ++i ) O(n2) for ( int j = i; j <= n; ++j ) { MAX = max( MAX, S[ j ] - S[ i - 1 ] ); } 最大指數次項:O(n2)
  • 25. Divide & Conquer 分而治之
  • 29. 最大連續和 有一長度為 n 的數列 A1, A2, A3, ..., An-1, An,求其最大連續區間和。 A1 A2 A3 ... An-2 An-1 An } MAX
  • 30. 切割問題 A1 A2 A3 ... An-2 An-1 An
  • 31. 切割問題 A1 A2 A3 ... An-2 An-1 An A1 A2 ... An/2 An/2+1 An/2+2 ... An A1 ... An/4 An/4+1 ... An/2 An/2+1 ... A3n/4 A3n/4+1 ... An A1 An/4 An/4+1 An/2 An/2+1 A3n/4 A3n/4+1 An
  • 32. 求解問題 遞迴直到數列 剩一元素,直接回傳該數值。 有一元素時,該數值為最大連續和。
  • 35. But.... A1 A2 A3 ... An-2 An-1 An A1 A2 ... An/2 An/2+1 An/2+2 ... An
  • 36. But.... 答案可能橫跨兩個區間! A1 A2 A3 ... An-2 An-1 An A1 A2 ... An/2 An/2+1 An/2+2 ... An
  • 37. How-to 5 -9 3 1 3 -10 3 3
  • 38. How-to 5 -9 3 1 3 -10 3 3 最大連續和:5
  • 39. How-to 5 -9 3 1 3 -10 3 3 最大連續和:5 最大連續和:6
  • 40. How-to 5 -9 3 1 3 -10 3 3 最大連續和:5 最大連續和:6 當前最大連續和:6
  • 41. How-to 5 -9 3 1 3 -10 1 1 當前最大連續和:6
  • 42. How-to 5 -9 3 1 3 -10 1 1 自右而左找最大連續和 當前最大連續和:6
  • 43. How-to 5 -9 3 1 3 -10 1 1 連續和:4 自右而左找最大連續和 當前最大連續和:6
  • 44. How-to 5 -9 3 1 3 -10 1 1 連續和:4 自右而左找最大連續和 自左而右找最大連續和 當前最大連續和:6
  • 45. How-to 5 -9 3 1 3 -10 1 1 連續和:4 自右而左找最大連續和 連續和:3 自左而右找最大連續和 當前最大連續和:6
  • 46. How-to 5 -9 3 1 3 -10 1 1 連續和:4 自右而左找最大連續和 連續和:3 自左而右找最大連續和 連續和:7 當前最大連續和:6
  • 47. How-to 5 -9 3 1 3 -10 1 1 連續和:4 自右而左找最大連續和 連續和:3 自左而右找最大連續和 連續和:7 當前最大連續和:6 最終最大連續和:7
  • 48. int maxsum( int L, int R ) { if ( R - L == 1 ) return A[ L ]; int mid = ( L + R ) / 2; int MAX = max( maxsum( L, mid ), maxsum( mid, R ) ); int tempL = 0, tempR = 0; for ( int i = mid - 1, sum = 0; i >= L; --i ) tempL = max( tempL, sum += A[ i ] ); for ( int i = mid, sum = 0; i < R; ++i ) tempR = max( tempR, sum += A[ i ] ); return max( MAX, tempL + tempR ); }
  • 49. Time Complexity total time
  • 50. Time Complexity total time n 2 * (n/2) = n ... n * (n/n) = n
  • 51. The height } log2n
  • 53. Time Complexity of Divide & Conquer 子問題扣除遞迴呼叫的時間複雜度 C(n) 遞迴呼叫層數 H(n) Time Complexity: C(n) × H(n)
  • 54. Time Comparison n3 n2 nlog2n 100 0.15 0.02 0.01 1000 123.86 1.36 0.14 單位:秒
  • 55. 從測試資料判斷演算法 假設機器 1 秒可以執行 106 個指令,... 時間複雜度 n! 2n n3 n2 nlog2n n 最大規模 9 20 10 2 10 3 7.5 × 10 4 10 6
  • 56. 另一種說法 假設機器 1 秒可以執行 108 個指令,... 時間複雜度 n! 2n n3 n2 nlog2n n 最大規模 11 26 464 10 4 4.5 × 10 6 10 8
  • 57. Practice Now 10245 - The Closest Pair Problem
  • 59. Thank You for Your Listening.