hdu 5199 map或二分或哈希

題目描述:給出n棵樹的高度,每棵樹上都站著一只鳥,槍手Jack站在最左邊那棵樹的左邊對鳥進行射擊,當Jack在高度為H的地方向右發射一顆子彈的時候,高度為H的樹上的鳥兒就會掉落(注:其他樹上的鳥兒不會飛走或掉落,這不是腦經急轉彎!)。Jack會射擊多次,他想知道每次射擊會有多少鳥兒掉落下來。

思路:題意十分清晰,思路也很簡單,只要實現這種映射關系就行了,數據量比較大。

方法1:map+IO外掛水之

 1 #include <cstdio>
 2 #include <cctype>
 3 #include <map>
 4 using namespace std;
 5 
 6 map<int, int> mp;
 7 int n, m, tmp;
 8 char buffer[10];
 9 
10 void print_d( int x )
11 {
12     if ( x == 0 )
13     {
14         putchar('0');
15     }
16     else
17     {
18         int p = 0;
19         while ( x )
20         {
21             buffer[p++] = x % 10 + '0';            
22             x = x / 10;
23         }
24         for ( int i = p - 1; i >= 0; i-- )
25         {
26             putchar(buffer[i]);
27         }
28     }
29     putchar('\n');
30 }
31 
32 void scan_d( int & x )
33 {
34     char ch = getchar();
35     while ( !isdigit(ch) ) ch = getchar();
36     x = 0;
37     do
38     {
39         x = x * 10 + ch - '0';
40         ch = getchar();
41     } while ( isdigit(ch) );
42 }
43 
44 int main ()
45 {
46     while ( scanf("%d%d", &n, &m) != EOF )
47     {
48         mp.clear();
49         while ( n-- )
50         {
51             scan_d(tmp);
52             mp[tmp]++;
53         }
54         while ( m-- )
55         {
56             scan_d(tmp);
57             print_d(mp[tmp]);
58             mp[tmp] = 0;            
59         }
60     }
61     return 0;
62 }

這樣寫完全是對的,不過比csc的代碼要慢一點,原因如下:

當調用mp[tmp]的時候,如果tmp在map中不存在,則默認會插入一個tmp到0的映射然后返回,所以這樣寫會讓節點數增多,查詢效率變低。

證明代碼如下:

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <map>
 4 using namespace std;
 5 
 6 int main ()
 7 {
 8     map<int, int> mp;
 9     cout << "mp is empty: " << mp.size() << endl;
10     cout << mp[111] << endl;
11     cout << "The size is: " << mp.size() << endl;
12     cout << mp[222] << endl;
13     cout << "The size is: " << mp.size() << endl;
14     for ( map<int, int>::iterator it = mp.begin(); it != mp.end(); it++ )
15     {
16         cout << it->first << " maps into " << it->second << endl;
17     }
18     system("pause");
19     return 0;
20 }

運行結果如下:

可見確實是這樣,因此可以這樣寫來加快效率:

 1 #include <cstdio>
 2 #include <map>
 3 using namespace std;
 4 
 5 map<int, int> mp;
 6 int n, m, tmp;
 7 
 8 int main ()
 9 {
10     while ( scanf("%d%d", &n, &m) != EOF )
11     {
12         mp.clear();
13         while ( n-- )
14         {
15             scanf("%d", &tmp);
16             mp[tmp]++;
17         }
18         while ( m-- )
19         {
20             scanf("%d", &tmp);
21             if ( mp.count(tmp) )
22             {
23                 printf("%d\n", mp[tmp]);
24                 mp.erase(tmp);
25             }
26             else
27             {
28                 printf("0\n");
29             }
30         }
31     }
32     return 0;
33 }

不過實測差不多,關鍵可能還是hdu服務器不穩定吧,不過mp[tmp] = 0; 還是沒有 mp.erase(tmp); 好,前者刪除節點,后者只是修改映射的值,前者配合mp.count(tmp)理論上更勝一籌。

方法2:二分?略...

方法3:hash

 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 const int N = 5000007;
 6 int hash_table[N];
 7 int cnt[N];
 8 int n, m, tmp;
 9 
10 int hash_key( int key )
11 {
12     return key % N;
13 }
14 
15 int kth( int k )
16 {
17     int r = ( k + 1 ) >> 1;
18     if ( k & 1 ) return r * r;
19     return -r * r;    
20 }
21 
22 void insert( int key )
23 {
24     int t = hash_key(key);
25     for ( int i = 0; ; i++ )
26     {
27         int pos = t + kth(i);
28         if ( pos < 0 ) pos += N;
29         if ( pos >= N ) pos -= N;
30         if ( hash_table[pos] == -1 )
31         {
32             hash_table[pos] = key;
33             cnt[pos]++;
34             return ;
35         }
36         else if ( hash_table[pos] == key )
37         {
38             cnt[pos]++;
39             return ;
40         }
41     }
42 }
43 
44 int find( int key )
45 {
46     int t = hash_key(key);
47     for ( int i = 0; ; i++ )
48     {
49         int pos = t + kth(i);
50         if ( pos < 0 ) pos += N;
51         if ( pos >= N ) pos -= N;
52         if ( hash_table[pos] == key )
53         {
54             int r = cnt[pos];
55             cnt[pos] = 0;
56             return r;
57         }
58         else if ( hash_table[pos] == -1 )
59         {
60             return 0;
61         }
62     }
63 }
64 
65 int main ()
66 {
67     while ( scanf("%d%d", &n, &m) != EOF )
68     {
69         memset( hash_table, -1, sizeof(hash_table) );
70         memset( cnt, 0, sizeof(cnt) );        
71         while ( n-- )
72         {
73             scanf("%d", &tmp);
74             insert(tmp);
75         }
76         while ( m-- )
77         {
78             scanf("%d", &tmp);
79             printf("%d\n", find(tmp));
80         }
81     }
82     return 0;
83 }

?

轉載于:https://www.cnblogs.com/huoxiayu/p/4393062.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/259118.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/259118.shtml
英文地址,請注明出處:http://en.pswp.cn/news/259118.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

數字電路實驗怎么接線視頻講解_家庭影院中音箱、功放、投影機、4K播放機不知道怎么連接?手把手教你...

家庭影院中音箱、功放、投影機、4K播放機不知道怎么連接&#xff1f;手把手教你有不少用戶收到從家庭影院器材之后&#xff0c;表示完全不會連接。翻看說明書也覺得頭大&#xff0c;知識太多&#xff0c;然而卻很難找到要點。今天主要跟大家講講如何連接音箱、功放、投影機和影…

.NET開發過程中的全文索引使用技巧之Solr

前言&#xff1a;相信許多人都聽說過.net開發過程中基于Lucene.net實現的全文索引&#xff0c;而Solr是一個高性能&#xff0c;基于Lucene的全文搜索服務器。同時對其進行了擴展&#xff0c;提供了比Lucene更為豐富的查詢語言&#xff0c;同時實現了可配置、可擴展并對查詢性能…

關于字符的讀入與輸出

在筆試中&#xff0c;經常見到字符的讀入與輸出的題目。逆序打印輸入時最常見、最基本的考題&#xff0c;復雜點的就是統計單詞、逆序打印單詞之類的。難點是如何判斷輸入的結束&#xff0c;如果用getchar函數&#xff0c;其輸入結束符為EOF&#xff08;其打印值為-1&#xff0…

修正discuz發帖首次換行無效的問題

找遍了百度和google都沒有解決方案&#xff0c;連discuz官方都沒有出來解決&#xff0c;至今其官網仍有這個問題。 那就自己動手解決吧&#xff0c;順手打個補丁。雖然走了小路&#xff0c;但是能解決問題。 解決方案&#xff1a;修改static/js/bbcode.js 找到 html2bbcode()方…

auto.js停止所有線程_Java線程與并發編程實踐:深入理解volatile和final變量

同步有兩種屬性&#xff1a;互斥性和可見性。synchronized關鍵字與兩者都有關系。Java同時也提供了一種更弱的、僅僅包含可見性的同步形式&#xff0c;并且只以volatile關鍵字關聯。假設你自己設計了一個停止線程的機制(因為無法使用Thread不安全的stop()方法))。清單1中Thread…

項目實例改編:利用structs2的action 實時顯示圖片、pdf和其他內容的框架抽取。(轉)...

轉自&#xff1a;http://www.verydemo.com/demo_c167_i1382.html 針對&#xff1a;預覽文件&#xff08;圖片&#xff0c;PDF&#xff09;文件來源為action中的inputStream 重點&#xff1a; structs2的action的配置 action的寫法和結果類型 resulttype的寫法 網頁上實…

零碎的小知識點 ----------C# ToString()函數注意事項

C#中存在著大量的字符串操作&#xff0c;有專門的string類&#xff0c;各種各種的方法&#xff0c;其中使用最為頻繁的方法為ToString()&#xff0c;用起來很是順手&#xff0c;但是這里存在一個很大的問題&#xff0c;空字符是不能用ToString方法轉換的&#xff0c;不然就會報…

ios越獄系統UIGestureRecognizer事件截獲問題

越獄的機器給self.view設置一個UITapGestureRecognizer,這貨就把所有的點擊事件全截獲了,比如某個按鈕,點擊就沒效果.普通系統是沒有問題的. 因此要給UIGestureRecognizer設置delegate并且在其中對touch的view進行分別處理 比如要讓按鈕功能正常使用: 1 #pragma mark - UIGestu…

開始Go開發之旅-Golang架構師之路系列實戰

2019獨角獸企業重金招聘Python工程師標準>>> 作者: gomaster.me(馮琪超) 系列:Golang架構師之路 巧婦難做無米之炊&#xff0c;golang sdk就是gopher的大米 下載golang 點擊 官網下載golang sdk 根據不同系統&#xff0c;官網下載鏈接會選擇相應的平臺進行鏈接跳轉&…

delete與delete[]的區別

一直對C中的delete和delete[]的區別不甚了解&#xff0c;今天遇到了&#xff0c;上網查了一下&#xff0c;得出了結論。做個備份&#xff0c;以免丟失。 C告訴我們在回收用 new 分配的單個對象的內存空間的時候用 delete&#xff0c;回收用 new[] 分配的一組對象的內存空間的時…

event對應的各種坐標

IE8不支持的PageXY 相對于整個頁面鼠標的位置 包括溢出的部分 event.pageX; event.pageY; 所有瀏覽器支持的&#xff1a; 相對于當前瀏覽器窗口可視區域的坐標event.clientX;event.clientY; 相對于當前屏幕&#xff08;和瀏覽器窗口大小無關&#xff09;的坐標event.screenX;…

安卓9.0官方系統升級包_華為、榮耀公布可升級安卓10.0機型,你的手機在名單之內嗎?...

在近兩個月以前&#xff0c;美方將華為關進了小黑屋&#xff0c;隨后谷歌也將華為旗下的機型移出了安卓10.0升級名單&#xff0c;這一波操作之后&#xff0c;引起了不小的“恐慌”&#xff0c;許多華為用戶也在擔心是否還能正常使用安卓系統服務&#xff0c;不過&#xff0c;讓…

2. Mysql數據庫的入門知識

2. Mysql數據庫的入門知識 &#xff08;1&#xff09;打開Windows系統提供的服務查看相應的服務。 &#xff08;2&#xff09;在Windows任務管理器的進程中查看 &#xff08;3&#xff09;使用命令行管理windows的Mysql數據庫服務。 Net start 服務名 Net stop 服務名 mysql -h…

十月讀書心得

1.sizeof與strlen的區別。 #include <iostream> using namespace std; void main() {cout << sizeof("hello") << endl;}答案&#xff1a; 6原因&#xff1a; “hello”{‘h’,e,l,l,o,\0};共六個字節。 那么sizeof與strlen有什么區別呢&#xff…

nginx php-fpm 輸出php錯誤日志(轉)

nginx是一個web服務器&#xff0c;因此nginx的access日志只有對訪問頁面的記錄&#xff0c;不會有php 的 error log信息。 nginx把對php的請求發給php-fpm fastcgi進程來處理&#xff0c;默認的php-fpm只會輸出php-fpm的錯誤信息&#xff0c;在php-fpm的errors log里也看不到ph…

protobuf的安裝和使用

以下全部基于win7系統。 protobuf是什么&#xff0c;有什么用網上說的已經很多了。這里就是說一下怎么使用。就當給自己做個筆記吧。 .proto文件的語法什么的也請網上查看&#xff0c;挺多的。 第一步&#xff1a; 下載protoc.exe 和 protobuf-java-2.4.1.jar。這里要注意版本區…

win7優化設置_win7藍牙怎么打開?

當電腦需要連接藍牙設備的時候&#xff0c;就需要打開藍牙設置才行。鑒于一些win7的用戶還不知道藍牙功能在哪&#xff0c;win7藍牙怎么打開&#xff0c;故系統圣地分享本篇教程。1、win7藍牙怎么打開?首先要你的電腦支持藍牙功能。如果你的電腦有藍牙功能的話那么在電腦的右下…

Struts2 通配符

在配置<action …./>元素時&#xff0c;需要指定name,class和method屬性&#xff0c;這三個屬性都支持通配符。 例如&#xff1a; 1.<action name ”*Action” class “student.RegisterAction” method “{1}”> 如果用戶請求的URL為loginAction.action,則調用…

Doxygen從零學起———安裝和配置

Doxygen可以為多種語言生成說明文檔&#xff08;從程序的源代碼中提取其中按照約定格式寫的注釋中提取信息&#xff09; 例如C, Objective-C, C#, C, PHP, Python, IDL (Corba, Microsoft, and UNO/OpenOffice flavors), Fortran, VHDL, Tcl, D ,從這期開始&#xff0c;我將系…

JAVA Drp項目實戰—— Unable to compile class for JSP 一波三折

交代下背景。電腦系統是64位的&#xff0c;用的是64位的Tomcat。安裝是32位的Myeclipse10&#xff0c;java環境也是32位的。Tomcat在開始啟動時會報這樣一個錯誤&#xff0c;“Cant load IA 64-bit .dll on a AMD32-bit platform”。可是不耽誤使用&#xff0c;近期在敲Drp項目…