hdu 4105 貪心思想

來源:互聯網
上載者:User

標籤:貪心思想

淋漓盡致的貪心思想

波穀一定是一位元,波峰一位元不夠大的時候添加到兩位元就一定夠大了的。

當在尋找波穀碰到零了就自然當成波穀。

當在尋找波峰時碰到零時,將前面的波穀加到前一個波峰上,讓當前的零做波穀,使得波穀的值盡量小,這就是本題最關鍵的貪心思想,一直想不到。


代碼中:a表示前一個值,b表示當前考慮的值,tag為偶數時表示正在尋找波穀,奇數時在尋找波峰。

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<map>using namespace std;char data[5999];int main(){int n, m, k;while(scanf("%d", &n) != EOF){scanf("%s", data);//cout<<data<<endl;int a, b, tag = 0;a = 11;b = 0;int ans = 0;for(int i = 0; i < n; i ++){b = (data[i] - '0');if(tag % 2 == 0){if(b < a){a = b;}else{i ++;a = data[i]-'0';}}else{if(b > a){a = b;}else{if(b == 0){while(data[i] == '0'){i ++;if(i >= n) break;}//貪心思想,有0就一定讓他做波穀,把原先的波穀a給到他的前一個波峰上a = 0;//0做波穀 b = data[i]-'0';a = b;}else{i ++;a = b*10 + (data[i] - '0');}}}if(i >= n) break;ans ++; tag ++;}printf("%d\n", ans-1);}return 0;}


聯繫我們

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