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

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

下面我用图进行分析:

下面为AC代码:

cpp 复制代码
#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代码:

cpp 复制代码
#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);
    }
}
相关推荐
小张会进步几秒前
数组:二维数组
java·javascript·算法
佑白雪乐19 分钟前
LCR 175. 计算二叉树的深度
算法·深度优先
阿Y加油吧26 分钟前
力扣打卡day07——最大子数组和、合并区间
算法
_饭团29 分钟前
字符串函数全解析:12 种核心函数的使用与底层模拟实现
c语言·开发语言·学习·考研·面试·蓝桥杯
Larry_Yanan29 分钟前
Qt网络开发之基于 QWebEngine 实现简易内嵌浏览器
linux·开发语言·网络·c++·笔记·qt·学习
想吃火锅100530 分钟前
【leetcode】105. 从前序与中序遍历序列构造二叉树
算法·leetcode·职场和发展
2401_8318249636 分钟前
嵌入式C++驱动开发
开发语言·c++·算法
靠沿39 分钟前
【优选算法】专题十八——BFS解决拓扑排序问题
算法·宽度优先
cui_ruicheng42 分钟前
C++数据结构进阶:哈希表实现
数据结构·c++·算法·哈希算法·散列表
光电笑映44 分钟前
高阶数据结构之红黑树详解
数据结构