备战蓝桥杯---数据结构与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);
    }
}
相关推荐
jojo_zjx8 小时前
GESP 25年12月1级 手机电量显示
c++
52Hz1188 小时前
力扣240.搜索二维矩阵II、160.相交链表、206.反转链表
python·算法·leetcode
程序员zgh8 小时前
C语言 弱定义机制 解读
c语言·开发语言·c++
We་ct8 小时前
LeetCode 380. O(1) 时间插入、删除和获取随机元素 题解
前端·算法·leetcode·typescript
老鼠只爱大米9 小时前
LeetCode经典算法面试题 #234:回文链表(双指针法、栈辅助法等多种方法详细解析)
算法·leetcode·链表·递归·双指针·快慢指针·回文链表
独自破碎E9 小时前
【动态规划】兑换零钱(一)
算法·动态规划
Sarvartha9 小时前
顺序表笔记
算法
宵时待雨9 小时前
数据结构(初阶)笔记归纳6:双向链表的实现
c语言·开发语言·数据结构·笔记·算法·链表
狐579 小时前
2026-01-20-LeetCode刷题笔记-3314-构造最小位运算数组I
笔记·算法·leetcode
0和1的舞者9 小时前
非力扣hot100-二叉树专题-刷题笔记(一)
笔记·后端·算法·leetcode·职场和发展·知识