日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

HDU 4022 Bombing c++解法

發布時間:2024/1/18 c/c++ 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 4022 Bombing c++解法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

It’s a cruel war which killed millions of people and ruined series of cities. In order to stop it, let’s bomb the opponent’s base.
It seems not to be a hard work in circumstances of street battles, however, you’ll be encountered a much more difficult instance: recounting exploits of the military. In the bombing action, the commander will dispatch a group of bombers with weapons having the huge destructive power to destroy all the targets in a line. Thanks to the outstanding work of our spy, the positions of all opponents’ bases had been detected and marked on the map, consequently, the bombing plan will be sent to you.
Specifically, the map is expressed as a 2D-plane with some positions of enemy’s bases marked on. The bombers are dispatched orderly and each of them will bomb a vertical or horizontal line on the map. Then your commanded wants you to report that how many bases will be destroyed by each bomber. Notice that a ruined base will not be taken into account when calculating the exploits of later bombers.

Input
Multiple test cases and each test cases starts with two non-negative integer N (N<=100,000) and M (M<=100,000) denoting the number of target bases and the number of scheduled bombers respectively. In the following N line, there is a pair of integers x and y separated by single space indicating the coordinate of position of each opponent’s base. The following M lines describe the bombers, each of them contains two integers c and d where c is 0 or 1 and d is an integer with absolute value no more than 10 9, if c = 0, then this bomber will bomb the line x = d, otherwise y = d. The input will end when N = M = 0 and the number of test cases is no more than 50.

Output
For each test case, output M lines, the ith line contains a single integer denoting the number of bases that were destroyed by the corresponding bomber in the input. Output a blank line after each test case.

寫一下本人的解法,一開始想到用結構體去寫,寫完一個O(N)的,oj上超時。后面用map寫O(logn)的,也是超時。最后把cin改成scanf才能ac。說明這道題的測試樣例點很多。而scanf讀入速度約為cin的10倍,這題時間要求挺嚴格的。

下面粘貼代碼:

#include <map> #include <iostream> #include <set> #include <cstdio> using namespace std;int main() {int n, m;while (cin >> n >> m) {map<long long int, multiset<long long int > > mapx;map<long long int, multiset<long long int > > mapy;if (n == 0 && m == 0)break;long long int x, y;for (int i = 0; i < n; i++) {scanf("%lld %lld", &x, &y);mapx[y].insert(x);mapy[x].insert(y);}multiset<long long int >::iterator it;long long int choose, line;for (int i = 0; i < m; i++) {cin >> choose >> line;if (choose == 1) {cout << mapx[line].size() << endl;//用y找有幾個x,輸出對應數量for (it = mapx[line].begin(); it != mapx[line].end(); it++) {//輸出完之后,把另一個map中的對應點刪掉if (mapy[*it].empty())//不判斷empty可能會報錯,我的編譯器會報錯。continue;elsemapy[*it].erase(mapy[*it].find(line));}mapx[line].clear();//刪除y=line時所有的x}if (choose == 0) {//同理用x找有幾個y時cout << mapy[line].size() << endl;for (it = mapy[line].begin(); it != mapy[line].end(); it++) {if (mapx[*it].empty())continue;elsemapx[*it].erase(mapx[*it].find(line));}mapy[line].clear();}}cout << endl;}return 0; }

總結

以上是生活随笔為你收集整理的HDU 4022 Bombing c++解法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。