备战蓝桥杯---数据结构与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);
    }
}
相关推荐
谙弆悕博士2 分钟前
【附Python源码】基于决策树的信用卡欺诈检测实战
python·学习·算法·决策树·机器学习·数据分析·scikit-learn
MATLAB代码顾问6 分钟前
黏菌算法(SMA)原理详解与Python实现
开发语言·python·算法
salipopl8 分钟前
C/C++ 中 volatile 关键字详解:原理、作用与实际应用
开发语言·c++
张赫轩(不重名)8 分钟前
图论3:连通性问题(复杂度均为 O(N + M) )
c++·算法·图论·拓扑学
Liangwei Lin8 分钟前
LeetCode 238. 除了自身以外数组的乘积
算法
啦啦啦_999911 分钟前
2. ID3决策树 & C4.5决策树
算法·决策树·机器学习
AIminminHu16 分钟前
(让 C++ 程序长出大脑:从“语音遥控器”到具身智能 Agent 的进化之路)------OpenGL渲染与几何内核那点事------(二-1-(15))
开发语言·c++·agent·具身智能
技术小黑30 分钟前
CNN算法实战系列02 | ResNet50V2算法实战与解析
pytorch·深度学习·算法·cnn
多加点辣也没关系39 分钟前
数据结构与算法|第十五章:排序算法(下)— 非比较类排序
算法·排序算法