Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[ACM-ICPC] Efficient Algorithm

KuoE0
February 27, 2013

[ACM-ICPC] Efficient Algorithm

KuoE0

February 27, 2013
Tweet

More Decks by KuoE0

Other Decks in Programming

Transcript

  1. ࠷େ࿈᠃࿨ ༗Ұ௕౓ҝ n తᏐྻ A1, A2, A3, ..., An-1, An

    ɼٻଖ࠷େ࿈᠃ეؒ࿨ɻ A1 A2 A3 ... An-2 An-1 An } MAX
  2. 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 ); }
  3. 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 ); } ֩৺ӡࢉɿՃ๏
  4. 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 ); }
  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 ); } T(n) = j − i +1 j=i n ∑ i=1 n ∑ = n(n +1)(n + 2) 6 Ճ๏࣍Ꮠɿ
  6. T(n) = j − i +1 j=i n ∑ i=1

    n ∑ = n(n +1)(n + 2) 6
  7. T(n) = j − i +1 j=i n ∑ i=1

    n ∑ = n(n +1)(n + 2) 6 ࡾ࣍ࣜ
  8. T(n) = j − i +1 j=i n ∑ i=1

    n ∑ = n(n +1)(n + 2) 6 ࡾ࣍ࣜ n 3 n 2 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
  9. 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
  10. ઃᏐྻ S ҝᏐྻ A తྦྷੵ࿨ɼ Ҽࠑ Si = A1 +

    A2 + ... + Ai ɼଇ Ai + Ai+1 + ... + Aj = Sj - Si-1 ɻ
  11. ઃᏐྻ S ҝᏐྻ A తྦྷੵ࿨ɼ Ҽࠑ Si = A1 +

    A2 + ... + Ai ɼଇ Ai + Ai+1 + ... + Aj = Sj - Si-1 ɻ ٻეؒ [i,j] త࿈᠃࿨ᷮधཁ O(1) త࣌ؒɻ
  12. 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
  13. 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 ] ); }
  14. 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 ] ); } O(n) O(n2) ࠷େࢦᏐ߲࣍ɿO(n2)
  15. ࠷େ࿈᠃࿨ ༗Ұ௕౓ҝ n తᏐྻ A1, A2, A3, ..., An-1, An

    ɼٻଖ࠷େ࿈᠃ეؒ࿨ɻ } MAX A1 A2 A3 ... An-2 An-1 An
  16. ੾ׂ໰୊ A1 A2 ... An/2 A1 A2 A3 ... An-2

    An-1 An An/2+1 An/2+2 ... An An/4+1 ... An/2 A1 ... An/4 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
  17. But.... A1 A2 ... An/2 A1 A2 A3 ... An-2

    An-1 An An/2+1 An/2+2 ... An
  18. How-to 5 -9 3 1 3 -10 3 3 5

    ࠷େ࿈᠃࿨ɿ5
  19. How-to 5 -9 3 1 3 -10 3 3 3

    5 3 ࠷େ࿈᠃࿨ɿ5 ࠷େ࿈᠃࿨ɿ6
  20. How-to 5 -9 3 1 3 -10 3 3 3

    5 3 ࠷େ࿈᠃࿨ɿ5 ࠷େ࿈᠃࿨ɿ6 ᙛલ࠷େ࿈᠃࿨ɿ6
  21. ᙛલ࠷େ࿈᠃࿨ɿ6 How-to 5 -9 3 1 3 -10 1 1

    ࣗӈࣕࠨፙ࠷େ࿈᠃࿨ 1 3
  22. ᙛલ࠷େ࿈᠃࿨ɿ6 How-to 5 -9 3 1 3 -10 1 1

    ࣗӈࣕࠨፙ࠷େ࿈᠃࿨ 1 3 ࿈᠃࿨ɿ4
  23. ᙛલ࠷େ࿈᠃࿨ɿ6 How-to 5 -9 3 1 3 -10 1 1

    ࣗӈࣕࠨፙ࠷େ࿈᠃࿨ ࣗࠨࣕӈፙ࠷େ࿈᠃࿨ 1 3 3 ࿈᠃࿨ɿ4
  24. ᙛલ࠷େ࿈᠃࿨ɿ6 How-to 5 -9 3 1 3 -10 1 1

    ࣗӈࣕࠨፙ࠷େ࿈᠃࿨ ࣗࠨࣕӈፙ࠷େ࿈᠃࿨ 1 3 3 ࿈᠃࿨ɿ4 ࿈᠃࿨ɿ3
  25. ᙛલ࠷େ࿈᠃࿨ɿ6 How-to 5 -9 3 1 3 -10 1 1

    ࣗӈࣕࠨፙ࠷େ࿈᠃࿨ ࣗࠨࣕӈፙ࠷େ࿈᠃࿨ 1 3 3 ࿈᠃࿨ɿ4 ࿈᠃࿨ɿ3 ࿈᠃࿨ɿ7
  26. ࠷ऴ࠷େ࿈᠃࿨ɿ7 ᙛલ࠷େ࿈᠃࿨ɿ6 How-to 5 -9 3 1 3 -10 1

    1 ࣗӈࣕࠨፙ࠷େ࿈᠃࿨ ࣗࠨࣕӈፙ࠷େ࿈᠃࿨ 1 3 3 ࿈᠃࿨ɿ4 ࿈᠃࿨ɿ3 ࿈᠃࿨ɿ7
  27. 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 ); }
  28. 㠥Ұछ㘸๏ ၊ઃػث 1 ඵՄҎࣥߦ 108 ݸࢦྩɼ... ࣌ؒෳᯑ౓ n! 2n n3

    n2 nlog2n n ࠷େن໛ 11 26 464 10 4 4.5 × 10 6 10 8