hdu4022_Bombing(STL_map)
2017-03-30 18:35
295 查看
Bombing
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 4178 Accepted Submission(s): 1572
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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 109, 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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
3 2
1 2
1 3
2 3
0 1
1 3
0 0
[align=left]Sample Output[/align]
2
1
题意:
给你n个点,m个炸弹。输出每个炸弹能销毁多少个指定点,被销毁过的点不能重复被销毁。每两组数据中间需要空一行。n=m=0时结束。
因为炸弹是炸一列或者一行,用map就能很好的处理了,不过由于点会重复,用multiset 就行了。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef map<int ,multiset<int> > position;
position x;
position y;
int Bombingx(position &x,position &y,int p)
{
int count=x[p].size();
multiset<int>::iterator it;
for(it=x[p].begin();it!=x[p].end();it++)
y[*it].erase(p);
x[p].clear();
return count;
}
int Bombingy(position &x,position &y,int p)
{
int count=y[p].size();
multiset<int>::iterator it;
for(it=y[p].begin();it!=y[p].end();it++)
x[*it].erase(p);
y[p].clear();
return count;
}
int main()
{
x.clear();
y.clear();
int n,m;int xx,yy;int com,loc;
while(scanf("%d%d",&n,&m)!=-1,n&&m)
{
for(int i=0;i<n;i++)
{
scanf("%d%d",&xx,&yy);
x[xx].insert(yy);
y[yy].insert(xx);
}
for(int i=0;i<m;i++)
{
int sum=0;
scanf("%d%d",&com,&loc);
if(com==0)
{
sum+=Bombingx(x,y,loc);
}
else
{
sum+=Bombingy(x,y,loc);
}
printf("%d\n",sum);
}
printf("\n");
}
return 0;
}
相关文章推荐
- HDU 4022 Bombing(stl,map,multiset,iterater遍历)
- 详细解说STL hash_map系列
- 用STL中的map和multimap解决文件匹配问题
- 摘:STL 之 Map
- 【C】C++标准模板库(STL)介绍--map
- STL2—关联容器map基础总结
- STL之map实现
- [待整理]STL中map用法详解
- C++ STL中哈希表 hash_map介绍
- C++学习STL之关联容器 --- pair、map、set
- STL中map用法详解
- stl中map与set
- STL—map
- stl hashmap与map hashmap与hashtable
- 入门计划->学用stl std::map<std::string, int>
- STL中map详解
- STL_map_multimap
- map,set的模板[STL]
- std::map -------stl
- STL:map/multimap用法详解