HDU 4022Bombing 2011網路賽(二分 或 STL)

果7發表於2013-10-05

Bombing

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


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

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
 

題目大意:題目意思說給你n個基地的座標位置,然後有m個炸彈,炸彈有兩種,0號炸彈可以炸燬x相同的基地,1號炸彈可以炸燬y相同的基地。問你每顆炸彈能炸燬的基地數目,如果這個基地已經被先前的炸彈炸燬了,就不能再被炸了。

 解題思路:開始上來就直接二分,沒有考慮到座標的範圍,那樣的話是需要將座標位置離散化的。但是考慮到另一個問題,離散化將x座標離散化,但是把x炸掉之後,那所有x的基地都將被炸掉,所以最後在每一個點先設定一個索引index,然後index是多少,就表示哪個點,開始設定visi都為0,具體實現見程式碼:

         題目地址:Bombing

二分AC程式碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#define maxx 100005
using namespace std;
int n,m;

struct node
{
    int x;
    int y;
    int index;  //每個點有自己的標記
}mq;
node a[maxx],b[maxx];
int visi[maxx];
//a按照x排序 b按照y排序
//visi記錄每個基地是否被炸

bool cmp1(node m1,node m2) //按照x排序
{
    return m1.x<=m2.x;
}
bool cmp2(node m1,node m2) //按照x排序
{
    return m1.y<=m2.y;
}

int cal1(int xx)  //計算x相等的數目
{
    int l,r,mid,ans;
    l=0,r=n-1,mid=(l+r)>>1;
    ans=0;
    while(l<=r)
    {
        if(a[mid].x<xx) l=mid+1;
        else r=mid-1;
        mid=(l+r)>>1;
    }

    for(mid=l;mid<n;mid++)
    {
        if(a[mid].x!=xx)  break;
        if(visi[a[mid].index]) continue;
        visi[a[mid].index]=1;  //標記這個點的x座標被炸
        ans++;
    }
    return ans;
}

int cal2(int yy)  //計算y相等的數目
{
    int l,r,mid,ans;
    l=0,r=n-1,mid=(l+r)>>1;
    ans=0;
    while(l<=r)
    {
        if(b[mid].y<yy) l=mid+1;
        else r=mid-1;
        mid=(l+r)>>1;
    }

    //cout<<l<<" "<<r<<" "<<mid<<endl;
    for(mid=l;mid<n;mid++)
    {
        if(b[mid].y!=yy)  break;
        if(visi[b[mid].index]) continue;
        visi[b[mid].index]=1;  //標記這個點的x座標被炸
        ans++;
    }
    return ans;
}

int main()
{
    int i,tmp,p;
    while(scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)
            break;

        for(i=0;i<n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            a[i].index=i;
            b[i]=a[i];
        }
        memset(visi,0,sizeof(visi));
        sort(a,a+n,cmp1);
        sort(b,b+n,cmp2);
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&tmp,&p);
            if(tmp==0) printf("%d\n",cal1(p));
            else printf("%d\n",cal2(p));
        }
        puts("");
    }
    return 0;
}

//484MS


在網上搜了一下,很多都是用STL過的。
首先建立兩個map<int,multiset<int> > mx,注意後面兩個尖括號中間要有個空格。否則編譯錯誤,第一個map是x,第一維是x,第二維便是這個x對應的y。然後每次如果看能炸燬多少個基地很容易知道,直接mx[p].size(),即可求出,主要是找到個數之後要把基地炸燬,把x對應的y刪掉,把y對應的x也要刪掉,具體實現見程式碼:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define maxx 100005
using namespace std;
typedef map<int,multiset<int> > node;
node mx;
node my;

int cal(node &x,node &y,int p)
{
    int res=x[p].size();
    multiset<int>::iterator it;
    for(it=x[p].begin();it!=x[p].end();it++)
    {
        //x[p].erase(*it);   //不能這樣刪除,這樣刪之後it無效
        y[*it].erase(p);
    }
    x[p].clear();  //直接一次清空
    return res;
}

int main()
{
    int n,m,x,y,q,p;
    while(scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        mx.clear(); my.clear();
        while(n--)
        {
            scanf("%d%d",&x,&y);
            mx[x].insert(y);
            my[y].insert(x);
        }
        while(m--)
        {
            scanf("%d%d",&q,&p);
            if(q==0) printf("%d\n",cal(mx,my,p));
            else printf("%d\n",cal(my,mx,p));
        }
        puts("");
    }
    return 0;
}

//1062MS



相關文章