IT民工
加油!

这是昨天网络赛的1002题,做得很伤感。昨天处理的方法上有些错误,一直没有过。

今天学习了下set,STL真强大。将Alice的card标记为id = 0,Bob的card标记为1。将所

有的纸片按照h,w的升序排序,按照id的降序排序,也就是在相同情况下将Bob的纸片放

前面。然后将Bob的纸片的w插入set,贪心选取Alice比Bob的纸片w大一点的来覆盖,然

后删掉被覆盖的纸片。

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

const int MAXN = 100100;
typedef multiset<int> SET;
typedef multiset<int> :: iterator P;

struct Node
{
    int h, w, id;
    bool operator<(const Node &b) const{
        if(h != b.h) return h < b.h;
        if(w != b.w) return w < b.w;
        return id > b.id;
    }
};

Node a[MAXN << 1];
SET D;
int n;

void Read()
{
    int i;
    scanf("%d", &n);
    for(i = 0; i < (n << 1); i ++)
    {
        scanf("%d%d", &a[i].h, &a[i].w);
        if(i >= n) a[i].id = 1;
        else a[i].id = 0;
    }
    sort(a, a + (n << 1));
}

int cal()
{
    int ans = 0, i;
    D.clear();
    for(i = 0; i < (n << 1); i ++)
    {
        if(a[i].id == 1) D.insert(a[i].w);
        else
        {
            if(!D.empty())
            {
                if(*D.begin() <= a[i].w)
                {
                    P p = D.upper_bound(a[i].w);
                    p --;
                    ans ++;
                    D.erase(p);
                }
            }
        }
    }
    return ans;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T --)
    {
        Read();
        printf("%d\n", cal());
    }
    return 0;
}

 

 

posted on 2012-09-09 10:37  找回失去的  阅读(740)  评论(2编辑  收藏  举报