UVa 11491 Erasing and Winning 題解

來源:互聯網
上載者:User

標籤:post   body   max   get   for   個數   貪心   col   pre   

難度:α

用時:30 min

題目:??

 

這是個裸貪心題。

 

題目要求在某數字字串中刪去給定個數的字元,使剩下來的數字最大。

 

那麼不難想到用隊列。線性複雜度。0 ms。

 

每讀入一個數,就把之前比較小的拿掉。

注意不能拿太多,否則長度不夠。

隊滿了也不能繼續放。(除非有更大的元素可以把隊尾的懟下去)

1 for (int i = 0; i < d; i++) {2     char c = getchar();3     while (it > 0 && ans[it] < c && it + d - i > d - e) it--;4     if (it < d - e) ans[++it] = c;5 }6 ans[++it] = ‘\0‘; puts(ans+1)

這個速度非常快的,相比我自己的一個想法來說。

 

經過分析我發現,從原字串裡取出幾個數字,每個數位位置是有限的。首先不能太靠後,其次要在前一個選走的數字之後。

 

那麼演算法就是這樣的。

1 last = -1;2 int remain = d - e;3 for (int i = 1; i <= remain; i++)4     ans[i] = max_elem(last+1, d - remain + i); // 求最大元素(用 max_element 函數不知道怎麼有問題),並記錄位置 last。具體實現略

 

跑了個 700 ms。

果斷抄了一個 0 ms 的代碼,卻跑了個 10 ms。

不知道怎麼搞的。

 

2018-02-06

UVa 11491 Erasing and Winning 題解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.