UVa 11572 Unique Snowflakes 演算法分析

來源:互聯網
上載者:User

標籤:chapter   amp   mes   ast   style   names   bsp   clu   ash   

難度:β-

用時:0 min

題目:??

代碼:?? ??

 

這是一道連續區間水題。

就是判重而已。

 

價值在於它的 map 做法。

用數組 prev 表示前面同值的位置,這樣就不用 set。

 

 

為什麼要用 map?因為是值對位,值可以很大,數組存不下。

 

在構造 prev 時,要用 map。

下面轉紫書代碼。

1 for (int i = 0; i < n; i++) {2     cin >> A[i];3     if (!cur.count(A[i])) last[i] = -1;4     else last[i] = cur[A[i]];5     cur[A[i]] = i;6 }

 

有 prev 數組以後,在延伸區間的過程中,右端點可以快速更新。

1 while (R < n) {2     while (R < n && last[R] < L) R++;3     ans = max(ans, R - L);4     L++;5 }

如果用 set,這裡就是 log 時間。

 

另外還有 hash_map 的使用。

1 #include <ext/hash_map>2 using namespace __gnu_cxx;3 hash_map<int, int> cur;

用時比普通 map 少一點。

 

2018-02-05

UVa 11572 Unique Snowflakes 演算法分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.