备战蓝桥杯---数据结构与STL应用(进阶2)

本文将主要围绕有关map的今典应用展开:

下面我用图进行分析:

下面为AC代码:

#include<bits/stdc++.h>
using namespace std;
struct Point
{
    int x,y;
    bool operator < (const Point & r) const {
        return x < r.x || ( x == r.x && y < r.y );//升序排列
    }
};
multiset<Point> S;
int main()
{
    int T, kas = 0; scanf("%d",&T);
    while(T--){
        if(kas) puts("");
        int n;
        scanf("%d",&n);
        printf("Case #%d:\n",++kas);
        S.clear();
        while(n--){
            Point P;
            scanf("%d%d",&P.x,&P.y);
            auto it = S.lower_bound(P);
            if(it == S.begin() || (--it)->y > P.y){
                it = S.insert(P);
                while(it != S.end() && (it->x==P.x&&it->y==P.y)) it++;
                while(it != S.end() && it->y >= P.y) S.erase(it++);
            }
            printf("%d\n",S.size());
        }
    }
    return 0;
}

接题:

其实与上一章的task题类似,我们按敌方防御力从大到小,选择攻击力合适的,在其中,如果他们防御力均小于,我们选一个防御力min的去同归于尽,反之选一个最接近敌方攻击力的。

下面是AC代码:

#include<bits/stdc++.h>
using namespace std;
struct node{
    int att,de;
}t[100010],e[100010];
int T,n,m,k;
bool cmp(node a,node b){
    return a.att>b.att;
}
bool cmp1(node a,node b){
    return a.de>b.de;
}
int main(){
    cin>>T;
    while(T--){
        if(k!=0) puts(" ");
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) scanf("%d%d",&t[i].att,&t[i].de);
        for(int i=1;i<=m;i++) scanf("%d%d",&e[i].att,&e[i].de);
        sort(t+1,t+n+1,cmp);
        sort(e+1,e+m+1,cmp1);
        int j=1,cnt=0,f=0;
        map<int,int> mp;
        for(int i=1;i<=m;i++){
            while(t[j].att>=e[i].de){
                if(mp.count(t[j].de)==0) mp[t[j].de]=1;
                else mp[t[j].de]++;
                j++;
            }
            if(mp.empty()){
                f=1;
                break;
            } 
           map<int,int>::iterator it=mp.lower_bound(e[i].att);
            if(it==mp.end()||(--mp.end())->first<=e[i].att){
                cnt++;
                if(--mp[mp.begin()->first]==0) mp.erase(mp.begin()->first);
            }
            else{
                if(it->first==e[i].att){
                    if(--mp[(++it)->first]==0) mp.erase(it->first);
                }
                else{
                    if(--mp[(it)->first]==0) mp.erase(it->first);
                }
            }
        }
       if(f==0) printf("Case #%d: %d",++k,n-cnt);
        else printf("Case #%d: -1",++k);
    }
}

  • 13
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值