• Tidak ada hasil yang ditemukan

演算法導入、ソート、データ構造、ハッシュ

N/A
N/A
Protected

Academic year: 2023

Membagikan "演算法導入、ソート、データ構造、ハッシュ"

Copied!
14
0
0

Teks penuh

(1)

培訓- 1 演算法導入、ソート、データ構造、ハッシュ

演算法導入、ソート、データ構造、ハッシュ

momohuang c2251393

chiangyo

September 23, 2013

1 Schedule of the Year

1.1 Major Competition

9 12

11 10

12 10 TOI

的最

3 TOI

3 TOI 100 20

4 TOI 30 12

5 TOI 12 4 算

7 IOI 2014

1.2 Minor Competition

11 10 的

11 NPSC 複

12 PK

的話 的

的USB

I

(2)

1.3 Online Contests 演算法導入、ソート、データ構造、ハッシュ

1.3 Online Contests

codeforces.com

排 排

usaco.org 11

codechef.com 比 題

的 題

topcoder.com 較 複雜 算法 的

1.4 Online Judges

SGU acm.sgu.ru 的 OJ 題

POI main.edu.pl/en 訓 的 OJ

的題

URAL acm.timus.ru OJ

POJ poj.org 題 題

TIOJ 218.210.35.237:8080/JudgeOnline tmt514 的 OJ

HOJ hoj.twbbs.org/judge/ hanhan0912 的

OJ 題 xD

1.5 Online Courses

• Coursera 最 的 MOOC Stanford 的 Andrew Ng

CS的

• edX MIT Harvard 的 MOOC CS

• Udacity 較

II

(3)

1.6 Recommended Books 演算法導入、ソート、データ構造、ハッシュ

1.6 Recommended Books

• 培 的 的 題

• 算法 算法

• Introduction to Algorithms

Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein

2 Introduction to Algorithm Competitions

2.1 What Is Big-O?

f(x) = O(g(x)) asx→ ∞

f(x)

g(x) is bounded for all sufficiently large x 的

∃x0, M ∈R+, s.t. ∀x > x0 ∈R,|f(x)|

|g(x)| < M

f(x) = O(g(x)) x 的 order of f(x) order of g(x) x3 =O(x4) x1000 =O(1.2x) 10000∗x4 =O(x4) log x=O(x0.007)

o的

f(x) =o(g(x))as x→ ∞ o

xlim→∞

f(x) g(x) = 0

O o < 0.00000012∗x2 =o(x2)

的 於 0

III

(4)

2.1 What Is Big-O? 演算法導入、ソート、データ構造、ハッシュ

於 的 Landau

的 x 於 2x, x4, log x

的 度 的 x4 becomes infinite in higher

order of x3xx43

xα becomes infinite in higher order of xβ α > β xα

order of magnitude α 的 法 的

order of magnitude 題 α > 0

ex

xαα >0 log xxα 於 0 法

的order of magnitude 法 的話

<, > O o

x→ ∞x→x0

f(x) =O(g(x))as x→x0 f(x) = o(g(x))as x→x0

∃ϵ, M ∈R+, s.t. ∀|x−x0|< ϵ,|f(x)|

|g(x)| < M

xlimx0

f(x) g(x) = 0

1cosx=o(x) asx→0

f(x+h)−f(x) =hf(x) +o(h)as h→0

x x0 h 的

f(x+h)−f(x) hf(x) 的 於 h x→ ∞ 的 O

IV

(5)

2.2 The Martial Arts of Problem Solving演算法導入、ソート、データ構造、ハッシュ

2.2 The Martial Arts of Problem Solving

mo n 題 mo n da i mo n su ta

題的 算

遭遇到一

問スター WIN LOSE

WIN

WIN

WIN LOSE

LOSE

基 算法 的 的

題 if for 的題

V

(6)

2.3 Some Problems 演算法導入、ソート、データ構造、ハッシュ

2.3 Some Problems

1. <SGU 126> 題

1 A 2 B A+B <2147483648

1 2

2 的 最

-1

2. <no judge pure thinking> 題

A = 3 (3(3...))}n B = 9 (9(9...))}m m nAB (n, m≤106)

3. <no judge pure thinking> 題

N A

B 的 PK

的 lose N, A, B 1000

4. <no judge pure thinking> 題

A+B A B

的 最

5. <POI18 Party> 題

3n 2n

的 題 n n

n n 1000 6. <Codeforces 197A> 題

A BR

A, B, R≤100

7. <Codeforces 297A> 題

01 的 A B A

A 的 100010001 1001 10010 A

A B A, B1000

VI

(7)

3.排序 演算法導入、ソート、データ構造、ハッシュ

3 排序

3.1 Introduction

排序 算法 排序 題

: N 的 (比較 的 )

題的 法 算法 的

算法 的

3.2 O(N

2

) 的算法

3 sort sort 的 外 的

排 的序

1. Selection sort

O(N) 的 排序 的序 的最 O(1) 排序

的序 的 於 N 複雜度 的 O(N2) 算 的 sort

2. Insertion sort

O(N) 排序 的序 ( ( )

比 的 wwww) 序

排 ( O(1) )

O(N2) O(N) 排序 的 序

3. Bubble sort

最 ( i i+1 )

Selection sort 算法 比 Selection sort

3.3 O(N lgN ) 的算法

的 比 O(N2) 的 排 序 法 的

(?):O(NlgN)排序法 3 sort Heap sort 外的

sort 的 O(N2) 的 Divide & Conquer

算法 的排序法 話 序

排序

VII

(8)

3.4 題外話: 基於比較的排序的最低複雜度演算法導入、ソート、データ構造、ハッシュ

1. Merge sort

序 排序 的 法

Selection sort 的 法 最 排序 的序

O(N2) O(N)

排序的最 O(1) 比 序 的最

比較 的 O(1) O(N) 複雜度 T(N) = 2× T(N2) + O(N) = O(NlgN) 算

O(N) O(lgN) 複 雜 度

=O(N)×O(lgN) = O(NlgN) 2. Quick sort

Merge sort Quick sort

(Pivot) 比較 的 的 比較 的 的

O(N) ( ) 排序

算法最 的 複雜度

Merge sort O(NlgN) 的 最 (最 )

複雜度 T(N) = T(N 1) +O(N) = O(N2)( N

O(N)) 法算 的 Quick sort

複雜度 Θ(NlgN) Quick sort 的

度的 Insertion sort 的 法

STL 的 std::sort() 的 Quick sort(Intro sort) 3. Heap sort

算法的 的

最 的 Heap O(lgN)

O(1) 最 O(lgN) 最 Heap 的培訓

3.4 題外話: 基於比較的排序的最低複雜度

的最低複雜度 排序 序 最 的最低複雜度 (

Insertion sort O(N) ) N 排序 的

N! ( ) 比較 的

N!

2! m 比較 的 (2!)N!m

N! (2!)m 1 的 於 排序的 m =O(lgN!) =O(NlgN)

最 比較的 於O(NlgN)

VIII

(9)

3.5 O(N +C) 的算法 演算法導入、ソート、データ構造、ハッシュ

3.5 O(N + C ) 的算法

的 sort 基於比較的排序 算法 基於比較的排序 算

法 ! 基於比較的 算法 複雜度 的

1. Counting sort

(index ) 的 複雜度 O(N+C)

C

2. Radix sort

排 法 的

排序 ( Counting sort) 複雜度 O((logBC)×(N +B)) B 法 C

3.6 Exercises!!

的算法 排序 算法的題

排序算法 排序 法

排序算法的

1. 序 (TIOJ 1080)

N(105) 的 a (i, j) :i < j∧ai > aj 2. K (TIOJ 1364)

NK O(N) K

3. Bubble sort 算

算 度 N(105) 的 Bubble sort排序的 4. Comiket(TIOJ 1410)

N(105) Loli i Loli siti

最 的 最 Loli

IX

(10)

4. Data Structure 演算法導入、ソート、データ構造、ハッシュ

4 Data Structure

Introduction

的 算 度 的 題 複雜 題的

的 算法的

的 法 的 題 的

4.1 Array

的 法 的

O(1) O(n) ( )

4.2 Linked List

linked list 的 的 insert erase

O(1) O(n)

linked list (data node) 的 pointer

的 pointer 的 NULL

外 的doubly linked list 的pointer

的 pointer

4.3 Stack

LIFO 的 stack push pop push

stack的 pop stack 的 於

"last in, first out" 的 的

的 DFS

4.4 Queue

FIFO 的 queue enqueue dequeue

enqueue queue 的 dequeue queue 的

於 "first in, first out" 的 的

( stack ) 排 的 排的

BFS 的

X

(11)

4.5 Deque 演算法導入、ソート、データ構造、ハッシュ

4.5 Deque

"deck" double-ended queue 的 enqueue dequeue

4.6 Tree

tree (node)

(child) (parent) (root) 的

(leaf) (binary tree) " 0 2

" 的tree 的 排序 的 O(logn) 的

4.7 Heap

heap 的 insert extract

insert heap extract heap 最 (最 )的

heap 的 比較 的 insert

extract O(logn)

4.8 Conclusion

STL 的 stack queue deque priority_ queue

的 的stack queue deque heap STL

的 題

4.9 Exercises

1. <Uva 514 Rails>

A 1,2,3, . . . n 的 序

station 1 n 的 排

station 度 的 排 序

2. <TIOJ 1176 Cows>

n 的 於 的 比 的

3. <STEP5 0098 >

XI

(12)

5. Hash 演算法導入、ソート、データ構造、ハッシュ

4. <TIOJ 1063最 >

n×m 的 01 1 的最

5. <TIOJ 1489 >

a∼z

6. <STEP5 0038 的 ♂ ♂ ♂>

n 的 序 A, B (n <100000)

1. stack push& pop的 A B?

2. 序 C A 的 法 C B 法?

C "-"

7. <STEP5 0020 >

n (4≤n 5×105) 8. <HOJ 2 >

N [Xi, Yi] [Xa, Ya],[Xb, Yb] Xa < Xb < Ya< Yb ( )

9. <TIOJ 1637 >

a, b 的 度

a, b

?

5 Hash

Introduction

n 最 的 法 序

O(n) 排序 binary search 低 O(logn)

的 " 的 " 的 n−1

x 序 的 x

的話 的複雜度 O(1) x

x

x

雜 (hash)

的 ( ) ( ) 的

XII

(13)

5.1 Hash Function 演算法導入、ソート、データ構造、ハッシュ

雜 (hash function) 的 雜 (hash table)

x x 雜 雜 的

x

雜 的雜

5.1 Hash Function

雜 的 L 的雜 h(X) X 0∼L−1的

的雜 法 的

hash 的雜

低 雜

5.1.1 Hash

h(x) =xh(x) x

LX

5.1.2 Mod Hash

雜 mod L hash h(x) = x mod L h(x) = x×k

mod Lh(x) 雜 的

5.2 Collision

的 法

5.2.1 RP

的 的雜

5.2.2 Chaining

的 linked list

5.2.3 Linear Probing

的雜 h(x, i) = (h(x) +i) mod L i 0 h(x, i)

外 Quadratic Probing h(x, i) = (h(x) +c1i2+c2i) mod L

XIII

(14)

5.3 std::map 演算法導入、ソート、データ構造、ハッシュ

5.2.4 Doubly Hashing

的雜 h(x, i) = (h1(x) +i×h2(x)) mod L

5.2.5 Perfect Hashing

雜 題 複雜度

O(n) 雜 雜 雜 h1(x)

雜 於 hash 的

的雜 h2(x) 雜 hash

h2(x)

5.3 std::map

map STL 的 (associative container)

key value mapped value

的雜

5.4 Exercises

1. <TIOJ 1160 題 >

n k 最 的 (0

n 100000)

XIV

Referensi

Dokumen terkait

4 C++ STANDARD LIBRARY建國中學2012年資訊能力競賽培訓講義 - 01 2012/09/2 4.4.2 Iterator Iterator迭代器 是用來存取 Container 裡面的元素,類似 Array 的 Pointer。對於每個 Container 有不同的型別,例如 vector::iterator。 Iterator