HDU 4022 Bombing (STL应用)

Bombing

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1377    Accepted Submission(s): 533

Problem Description 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.  

 

Sample Input 3 2 1 2 1 3 2 3 0 1 1 3 0 0  

 

Sample Output 2 1  

 

Source
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest  

 

Recommend lcy      
2011年上海网络赛的B题,用的是STL。好像方法很多,STL太强大了,确实应该好好学习下。
我现在用的是STL中的map 和 multiset 来做的,代码写起来比较简洁,也比较好容易理解。

/*
HDU 4022
G++ 1296ms


*/


#include<stdio.h>
#include<iostream>
#include<set>
#include<map>
#include<algorithm>
using namespace std;

// 建立一个 map,从 int 到 一个 multiset 容器的映射
typedef map<int,multiset<int> > line;// 两个>间一定要加个空格
map<int,multiset<int> >mx;//定义x坐标对应的map
map<int,multiset<int> >my;//定义y坐标对应的map

int bomb(line &x,line &y,int pos)
{
    int ret=x[pos].size();
    multiset<int>::iterator it;//这个学习下
    for(it=x[pos].begin();it!=x[pos].end();it++)
       y[*it].erase(pos);//multiset 去除一个元素
    x[pos].clear();//清空multiset
    return ret;
}
int main()
{
    int n,m;
    int c,d;
    int tx,ty;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0)break;
        mx.clear();
        my.clear();
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&tx,&ty);
            mx[tx].insert(ty);
            my[ty].insert(tx);
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&c,&d);
            int ans;
            if(c==0) ans=bomb(mx,my,d);
            else ans=bomb(my,mx,d);
            printf("%d\n",ans);
        }
        printf("\n");
    }
    return 0;
}

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/08/23/2652628.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞