【類比題,未知演算法】hdu hdoj 1761 Spring-outing Decision(2)

來源:互聯網
上載者:User

這題做了我一整天呀!!!!!!!!!!

也TLE了一整天!!!!!!!!!

 

最後竟然卡時間過了。。。。

936ms.....

實在是eggache.....

 

最開始題目沒有看清楚,,,不懂m是幹什麼用的,導致一直wa....

後來由於map過多使用類似t_map["abc"]這樣的進行查詢和插入,導致時間過長。

 

因為t_map["abc"]=1在"abc"存在的時候要先進行查詢,然後修改值。

但是在"abc"不存在的時候,執行bb = t_map["abc"]會進行查詢,也會進行插入一個新的"abc".

要知道,紅/黑樹狀結構插入的時間是很長的。。。

 

所以在以後使用map的過程當中,盡量使用 insert, find 來進行插入和查詢。這樣才能儘可能減少時間。。。。

#include <stdlib.h><br />#include<stdio.h><br />#include<string.h><br />#include<string><br />#include<iostream><br />#include<map><br />#include<vector><br />using namespace std;<br />typedef map<string, int> STRING2MAP;</p><p>int main()<br />{<br /> int n, m;<br /> int i, j;<br /> int k;<br /> char name[25], like[25], hate[25], go[25];<br /> map<string, STRING2MAP> t_map;<br /> map<string, STRING2MAP>::iterator it_t;<br /> map<string, bool> t_now;<br /> map<string, bool>::iterator it_n;<br /> STRING2MAP::iterator it_f;<br /> STRING2MAP S2M;<br /> string t_add[101], t_remove[101];<br /> int p_add, p_remove;<br /> int h, l;<br /> int geted;<br /> while (scanf("%d%d", &n, &m) != EOF)<br /> {<br /> t_map.clear();<br /> t_now.clear();<br /> for (i=0; i < n; i++)<br /> {<br /> scanf("%s", name);<br /> scanf("%d", &k);<br /> t_map[name] = S2M;<br /> it_t = t_map.find(name);<br /> for (j=0; j < k; j++)<br /> {<br /> scanf("%s", like);<br /> it_t->second.insert(STRING2MAP::value_type(like, 1));<br /> //it_t->second[like] = 1;<br /> }<br /> scanf("%d", &k);<br /> for (j=0; j < k; j++)<br /> {<br /> scanf("%s", hate);<br /> it_t->second.insert(STRING2MAP::value_type(hate, 2));<br /> //it_t->second[hate] = 1<<1;<br /> }<br /> }<br /> scanf("%d", &k);<br /> for (j=0; j < k; j++)<br /> {<br /> scanf("%s", go);<br /> t_now[go] = true;<br /> }<br /> for (i=0; i<m; i++)<br /> {<br /> p_add = 0;<br /> p_remove = 0;<br /> it_t=t_map.begin();<br /> for (; it_t != t_map.end(); it_t++)<br /> {<br /> it_n=t_now.begin();<br /> l=h=0;<br /> for (; it_n != t_now.end(); it_n++)<br /> {<br /> it_f = it_t->second.find(it_n->first);<br /> if (it_f != it_t->second.end())<br /> {<br /> geted = it_f->second;<br /> if (geted & 1) l++;<br /> if (geted & 1<<1) h++;<br /> if (l>0 && h>0) break;<br /> }<br /> }<br /> if (h == 0 && l > 0)<br /> t_add[p_add++] = it_t->first;<br /> if (h > 0 && l == 0)<br /> t_remove[p_remove++] = it_t->first;<br /> }<br /> if (p_remove == 0 && p_add == 0)<br /> break;<br /> for (j=0; j < p_remove; j++)<br /> t_now.erase(t_remove[j]);<br /> for (j=0; j <p_add; j++)<br /> t_now.insert(make_pair(t_add[j], true));<br /> }<br /> if (t_now.empty())<br /> {<br /> printf("None/n");<br /> continue;<br /> }<br /> it_n=t_now.begin();<br /> cout<< it_n->first;<br /> it_n++;<br /> for (; it_n != t_now.end(); it_n++)<br /> cout << " " << it_n->first;<br /> cout << endl;<br /> }<br /> return 0;<br />}

聯繫我們

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