演算法分析第三次作業

2022-09-13 06:18:11 字數 1656 閱讀 2706

在乙個排好序的陣列t[1..n]中查詢x,如果x在t中,輸出x在t的下標j;如果x不在t中,輸出j=0.

遍歷陣列

二分陣列

遍歷陣列的演算法,就是乙個個按順序查詢

二分演算法:判斷乙個搜尋集,每次判斷搜尋集的中間,減去一半區間

遍歷演算法:o(n)

二分演算法:o(logn)

1 #include2 #include3 #include4 #include5 #include6 #include7 #include

8 #include9 #include10 #include11

//#include

12 #include13 #include14

using

namespace

std;

15#define ll long long

16#define ull unsigned long long

17#define pii pair18

#define pii pair19

#define m_p make_pair

20#define l_b lower_bound

21#define u_b upper_bound

22const

int inf=0x3f3f3f3f;23

const ll linf=0x3f3f3f3f3f3f3f3f;24

const

int maxn=3e5+11;25

const

int maxm=2e3+11;26

const

int mod=1e9+7

; 27

const

double eps=1e-5

;28 ll rd()while (ch >= '

0'&&ch <= '

9') return x *f;}

29 inline ll qpow(ll a,ll b,ll p)b>>=1;a=a*a%p;}return

res;}

30 inline ll gcd(ll a,ll b)

31//

iterator

32//

head

33//

priority_queue

34int

a[maxn];

35int

main()

48if(a[i]==m)

52if(i==n)

56}

57//

二分檢索

58 printf("

case 2:\n");

59int l=1,r=n;

60while(l<=r)

65if(l>n||a[l]!=m) puts("0"

);66

else printf("

%d\n

",l);

67 }

view code

第三次作業

2 12有600 mb 兆位元組 的資料,需要從南京傳送到北京。一種方法是將資料寫到磁碟上,然後託人乘火車這 些磁碟捎去。另一種方法是用計算機通過長途 線路 設資訊傳送的速率為2.4kb s 傳送此資料。試比較這兩種方法的優劣。若資訊傳送速率為33.6kb s,其結果又如何?解 當傳送速率為2.4k...

第三次作業

1 有600mb 兆位元組 的資料,需要從南京傳送到北京 一種方法是將資料寫到磁碟上,然後託人乘火車將這些磁碟捎去。另一種方法是用計算機通過長途 線路 設資訊傳送的速率為2.4kb s 傳送此資料,試比較這兩種方法的優劣。若資訊傳送的速率為33.6kb s,其結果又如何?解 假定連續傳送且不出錯。若...

第三次作業

p67 2 12 有600mb的資料,需要從南京傳送到北京。一種方法是將資料寫到磁碟上,然後託人乘火車將這些磁碟捎去。另一種方法是用計算機通過長途 線路 設資訊傳送的速率是2.4kbps 傳送此資料。試比較這兩種方法的優劣。若資訊傳送速率為33.6kbps,其結果又如何?解 1 t 600 1024...