hdu 4022map和list等数据结构的应用

这题还是挺好的,想了好久才想出做法。做法的大体思想其实就是暴力删点,用了一个布尔数组(deleted)来记录在执行一个请求前某点是否已被删除(所以在点结构体中加了一个变量id以唯一标识一个点)。我是用了两个链表存了两份点(lx, ly),一份先按x再按y排序,另一份先按y再按x排序。然后用一个mapx存下lx中不同x值开始的位置。(同理得mapy)。对于一个删除x=d的请求,可以直接从mapx中读出x=d在链表中的起始位置,然后一个个地删,一边删一边记录在deleted数组中就可以了。同理可以处理y=d的情况。

这题最让我记忆深刻的地方是我在打代码的时候有个地方犯了一个小错误死循环了没输出结果,然后调试,发现map里存的iterator都不对,各种稀奇古怪的问题,最后才发现那是eclipse的bug,无语了。

/*
 * hdu 4022/win.cpp
 * Created on: 2012-11-9
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
const int MAXN = 100100;
typedef struct MP{
    int x, y;
    int id;
}MP;
MP mp[MAXN];
bool deleted[MAXN];
inline bool cmpx(const MP &m1, const MP &m2) {
    if(m1.x == m2.x) {
        return m1.y < m2.y;
    }
    return m1.x < m2.x;
}
inline bool cmpy(const MP &m1, const MP &m2) {
    if(m1.y == m2.y) {
        return m1.x < m2.x;
    }
    return m1.y < m2.y;
}
map<int, list<MP>::iterator> mapx, mapy;
int main() {
#ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
#endif
    int N, M, o, d, temp;
    while(scanf("%d%d", &N, &M) == 2) {
        if(N == 0 || M == 0) {
            break;
        }
        for(int i = 0; i < N; i++) {
            scanf("%d%d", &mp[i].x, &mp[i].y);
            mp[i].id = i;
        }
        list<MP> lx, ly;
        sort(mp, mp + N, cmpx);
        copy(mp, mp + N, back_inserter(lx));
        sort(mp, mp + N, cmpy);
        copy(mp, mp + N, back_inserter(ly));
        list<MP>::iterator it = lx.begin();
        it = lx.begin();
        mapx.clear();
        while(it != lx.end()) {
            temp = (*it).x;
            if(mapx.count(temp) == 0) {
                mapx[temp] = it;
            }
            it++;
        }
        it = ly.begin();
        mapy.clear();
        while(it != ly.end()) {
            temp = (*it).y;
            if(mapy.count(temp) == 0) {
                mapy[temp] = it;
            }
            it++;
        }
        memset(deleted, false, sizeof(deleted));
        for(int i = 0; i < M; i++) {
            scanf("%d%d", &o, &d);
            int ans = 0;
            if(o == 0) {
                if(mapx.count(d) > 0) {
                    list<MP>::iterator it = mapx[d];
                    while(it != lx.end() && (*it).x == d) {
                        if(!deleted[(*it).id]) {
                            deleted[(*it).id] = true;
                            ans++;
                        }
                        it++;
                    }
                }
            }else {
                if(mapy.count(d) > 0) {
                    list<MP>::iterator it = mapy[d];
                    while(it != ly.end() && (*it).y == d) {
                        if(!deleted[(*it).id]) {
                            deleted[(*it).id] = true;
                            ans++;
                        }
                        it++;
                    }
                }
            }
            printf("%d\n", ans);
        }
        putchar('\n');
    }
    return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2012-11-12 16:27  moonbay  阅读(172)  评论(0编辑  收藏  举报