贪心用树状数组优化

Alice and Bob

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 381    Accepted Submission(s): 149


Problem Description
Alice and Bob's game never ends. Today, they introduce a new game. In this game, both of them have N different rectangular cards respectively. Alice wants to use his cards to cover Bob's. The card A can cover the card B if the height of A is not smaller than B and the width of A is not smaller than B. As the best programmer, you are asked to compute the maximal number of Bob's cards that Alice can cover.
Please pay attention that each card can be used only once and the cards cannot be rotated.
 

Input
The first line of the input is a number T (T <= 40) which means the number of test cases. 
For each case, the first line is a number N which means the number of cards that Alice and Bob have respectively. Each of the following N (N <= 100,000) lines contains two integers h (h <= 1,000,000,000) and w (w <= 1,000,000,000) which means the height and width of Alice's card, then the following N lines means that of Bob's.
 

Output
For each test case, output an answer using one line which contains just one number.
 

Sample Input
  
2 2 1 2 3 4 2 3 4 5 3 2 3 5 7 6 8 4 1 2 5 3 4
 

Sample Output
  
1 2
 

Source

分析:n是10w,俩个for肯定超时,所以用树状数组优化了一下,就是把俩组都按照升序排,再把所有宽度离散化,然后一个for从alice开始遍历bob是否符合,一个while把所有符合的bob的宽度,在树状数组里加1,原来都初始化为0,然后求树状数组里小于alice的w的和s,如果是0表示没有能覆盖的,否则就用二分求出合为s的最大的w就是符合条件的,ans++;在树状数组里-1,相当于用掉。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm>


using namespace std;
#define lowbit(x) x&(-x)
const int maxn=200010;
struct Node
{
    int h,w;
    bool operator<(Node a) const
    {
        if(h!=a.h) return h < a.h;
        else return w < a.w;
    }
}node1[100010],node2[100010];
int tab[maxn],N,d[maxn];
void insert(int p,int val)
{
    for(int i=p;i<=N;i+=lowbit(i)) d[i]+=val;
}
int query(int p)
{
    int sum=0;
    for(int i=p;i>=1;i-=lowbit(i)) sum+=d[i];
    return sum;
}
int bin1(int key)
{
    int l=1,r=N;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(tab[mid]==key) return mid;
        else if(tab[mid]>key) r=mid-1;
        else l=mid+1;
    }
}
int bin(int key)
{
    int l=1,r=N,ret=N;
    int sum=query(key);
    if(sum==0) return -1;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        int q=query(mid);
        if(q>=sum){
            ret=mid;
            r=mid-1;
        }else l=mid+1;
    }
    return ret;
}
int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        N=0;
        for(int i=0;i<n;i++) scanf("%d%d",&node1[i].h,&node1[i].w),tab[++N]=node1[i].w;
        for(int i=0;i<n;i++) scanf("%d%d",&node2[i].h,&node2[i].w),tab[++N]=node2[i].w;
        sort(node1,node1+n);
        sort(node2,node2+n);
        sort(tab+1,tab+1+N);
        int k=1;
        for(int i=2;i<=N;i++)
          if(tab[i]>tab[k]) tab[++k]=tab[i];
        N=k;
      /*  for(int i=1;i<=N;i++)
          cout<<tab[i]<<" ";
        cout<<endl;*/
        memset(d,0,sizeof(d));
        int id=0,ans=0;
        for(int i=0;i<n;i++)
        {
            while(id<n&&node2[id].h<=node1[i].h)
            {
                int p=bin1(node2[id].w);
                insert(p,1);
                id++;
            }
            
            int p1=bin1(node1[i].w);
            int p=bin(p1);
            if(p==-1) continue;
            ans++;
            insert(p,-1);
        }
        printf("%d\n",ans);
       // st.clear();*/
    }
    return 0;
}
方法二;
用set实现:
#include <set>
 #include <cstdio>
 #include <cstring>
 #include <iostream>
 #include <algorithm>
 
 using namespace std;
 
 const int maxn = 1e6+5;
 
 struct node{
     int x,y;
     friend bool operator < (node a,node b){
         return a.x<b.x||(a.x==b.x&&a.y<b.y);
     }
 }a[maxn],b[maxn];
 
 multiset<int> s;
 int n;
 
 int main(){
     freopen("sum.in","r",stdin);
     int ncase;
     cin>>ncase;
     while(ncase--){
         cin>>n;
         for(int i=0;i<n;i++)
             scanf("%d%d",&a[i].x,&a[i].y);
         for(int i=0;i<n;i++)
             scanf("%d%d",&b[i].x,&b[i].y);
         sort(a,a+n);
         sort(b,b+n);
         set<int>::iterator it;
         s.clear();
         int j = 0;
         int ans = 0;
         for(int i=0;i<n;i++){
             while(j<n&&b[j].x<=a[i].x){
                 s.insert(b[j].y);
                 j++;
             }
             if(s.size()==0)
                 continue;
             it = s.upper_bound(a[i].y);
             if(it!=s.begin()){
                 ans++;
                 it--;
                 s.erase(it);
             }
         }
         cout<<ans<<endl;
     }
     return 0;
 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值