备战蓝桥杯---数据结构与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);
    }
}
相关推荐
笑鸿的学习笔记3 分钟前
qt-C++语法笔记之Stretch与Spacer的关系分析
c++·笔记·qt
hardStudy_h17 分钟前
C++——内联函数与Lambda表达式
开发语言·jvm·c++
vortex528 分钟前
算法设计与分析 知识总结
算法
艾莉丝努力练剑44 分钟前
【C语言】学习过程教训与经验杂谈:思想准备、知识回顾(三)
c语言·开发语言·数据结构·学习·算法
ZZZS05161 小时前
stack栈练习
c++·笔记·学习·算法·动态规划
位东风1 小时前
【c++学习记录】状态模式,实现一个登陆功能
c++·学习·状态模式
hans汉斯1 小时前
【人工智能与机器人研究】基于力传感器坐标系预标定的重力补偿算法
人工智能·算法·机器人·信号处理·深度神经网络
vortex53 小时前
算法设计与分析:分治、动态规划与贪心算法的异同与选择
算法·贪心算法·动态规划
前端拿破轮3 小时前
🤡🤡🤡面试官:就你这还每天刷leetcode?连四数相加和四数之和都分不清!
算法·leetcode·面试
雷羿 LexChien3 小时前
C++内存泄漏排查
开发语言·c++