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++解法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 买股评99元一年《雅戈尔600177 今
- 下一篇: Linux无法查看ip地址问题解决