hdu4268 Alice and Bob(贪心)

来源:互联网 发布:千里眼远程监控软件 编辑:程序博客网 时间:2024/05/16 19:03

本题没完全懂,set还是没有用熟练,以后再看。

不想做贪心了,真是瞎了我的狗眼5555。。。。

#include <stdio.h>#include <cstdio>#include <set>#include <string>#include <algorithm>using namespace std;const int N = 100005;struct CARD{    int hig, wed;    bool operator < (const CARD &n1) const    {        if(wed == n1.wed) return hig < n1.hig;        return wed < n1.wed;    }}A[N], B[N];int main(){  //  freopen("in.txt", "r", stdin);    int T, n, i, sum, p;    scanf("%d", &T);    while(T--)    {        scanf("%d", &n);        multiset <int> S;        S.clear();        for(i = 1; i <= n; i ++)            scanf("%d%d", &A[i].hig, &A[i].wed);        for(i = 1; i <= n; i ++)            scanf("%d%d", &B[i].hig, &B[i].wed);        sum = 0;        p = 1;        sort(A + 1, A + n + 1);        sort(B + 1, B + n + 1);        for(i = 1; i <= n; i ++)        {            while(p <= n && B[p].wed <= A[i].wed)                S.insert(B[p ++].hig);            if(S.empty()) continue;            multiset <int> :: iterator it = S.lower_bound(A[i].hig);            if(*(it) == A[i].hig)            {                sum ++;                S.erase(it);            }            else            {                if(it == S.begin()) continue;                else                {                    sum ++;                    S.erase(-- it);                }            }        }        printf("%d\n",sum);    }    return 0;}


0 0