HDU4398 - Template Library Management(贪心 + STL)

本文介绍了一种在编程竞赛中有效管理代码模板的策略,通过贪心算法实现最少的模板调用次数,确保顺利解决所有问题。

Problem Description

As an experienced ACMer, you must have known the importance of "code template library". With the help of pre-printed code library, you can implement the complicated algorithms correctly and efficiently. However, the size of the library is strictly limited during the contest. For example, you can only take at most 25 pages of printed codes in World Finals. So you must choose carefully which code template should be included.
Now your team is participating a programming contest whose rules are slightly different from ICPC. This contest consists of N problems, and you must solved them in order: Before you solve the (i+1)th problem, you must solve the ith problem at first. And solving the ith problem requires a specified code template Ti.
You are allowed to hold M code templates only. At the beginning of the contest, your are holding templates numbered 1, 2, ..., M. During the contest, if the problem you are trying to solve requires code template Ti, and Ti is happened at your hand (i.e, one of the M code templates you are holding is Ti), you can solve it immediately. On the other hand, if you are not holding Ti, you must call your friends who are outside the arena for help (yes, it is permitted, not cheating). They can give you the code template you need. Because you are only allowed to hold M code templates, after solving current problem, you must choose to drop the code you get from your friends just now, or to keep it and drop one of the M templates at your hand previously.
Given the problem sequence in the contest and the limitation M, you want finish all the problems with minimum number of calling your friends.

Input

The first line of each test case contains two numbers N (1 <= N <= 100000) and M (1 <= M <= 100000). The second line contains N numbers T1, T2, ..., TN (1 <= Ti<= 1e9), indicating the code templates required by each problem.

Output

Output one line for each test case, indicating the minimum number of calling friends for help.

Sample Input

4 3 
1 2 3 4 
11 3
4 1 2 1 5 3 4 4 1 2 3

Sample Output

1 4

Author

RoBa

Source 

2012 Multi-University Training Contest 10

题目大意:

一个人最开始手上有1-m个模板,是模板1, 2, 3, ,,m的意思,不是需要的n个模板的前m个,比如说你有模板1, 2, 3,但是那需要模板11, 12, 13, 14,那么手上的模板都没用了(写这题时英语太菜产生歧义,导致样例都没搞懂,没写出来)。如果手上没有需要的模板,那么就要像场外的队友求助,可以用完就还给队友,也可以留下来下一次使用,但是要扔掉手中的一个模板,因为一个人只能有m个模板,做完一题才能做下一题,求向队友求助的最少次数。

题目分析:

 只要题目读懂,这题的贪心思路还是很好想的,如果要删掉一个模板,就删掉下次使用最晚的模板,我们可以预处理next数组,来存下一个坐标,如果下次不再使用就置为一个较大的数。我们可以用map和set来简化操作,注意由于一开始我们有1-m个模板,但是next数组是处理需要的n个模板的,所以会对next数组的使用产生麻烦,所以我们用set存结构体,每次通过删除再插入来更新结构体中的next值并使set重新排序。

AC代码:

#include <iostream>
#include <cstdio>
#include <set>
#include <map>
#include <queue>
using namespace std;
const int N = 1e6 + 5;
int t[2 * N];
map<int, int> mp,vis, nex;
struct cmp{
    int num;
    int nex;
    bool operator <(const cmp &a)const{
        if (nex == a.nex)return num < a.num;
        return nex > a.nex;
    }
}tmp;


int main()
{
    int n, m;
    while(scanf("%d %d", &n, &m) == 2){
        for (int i = 1; i <= n; i ++){
            scanf("%d", &t[i]);
        }
        mp.clear();
        nex.clear();
        vis.clear();
        for (int i = n; i >= 1; i --){
            if (mp[t[i]])nex[i] = mp[t[i]];
            else nex[i] = 0x3f3f3f3f;
            mp[t[i]] = i;
        }
        set<cmp> s;
        set<cmp>:: iterator it;
        for (int i = 1; i <= m; i ++){
            if(!mp[i])mp[i] = 0x3f3f3f3f;
            tmp.num = i;
            tmp.nex = mp[i];
            s.insert(tmp);
        }
//        for (int i = 1; i <= n + m; i ++)cout << nex[i] << ' ';
//        cout << endl;
        int ans = 0;
        for (int i = 1; i <= n; i ++){
            tmp.num = t[i];
            tmp.nex = i;
            it = s.find(tmp);
            if (it == s.end()){
                    tmp.num = t[i];
                tmp.nex = nex[i];
                //cout << "**********" << t[*it] << ' ' << t[i] << endl;
                s.insert(tmp);
                s.erase(s.begin());
                ans ++;
            }else{
                s.erase(it);
                tmp.num = t[i];
                tmp.nex = nex[i];
                s.insert(tmp);
            }
//            for (it = s.begin(); it != s.end(); it ++)cout << t[*it] << ' ';
//                cout << endl;
//            for (it = s.begin(); it != s.end(); it ++)cout << nex[*it] << ' ';
//                cout << endl;
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

内容概要:本文提出了一种基于融合鱼鹰算法和柯西变异的改进麻雀优化算法(OCSSA),用于优化变分模态分解(VMD)的参数,进而结合卷积神经网络(CNN)与双向长短期记忆网络(BiLSTM)构建OCSSA-VMD-CNN-BILSTM模型,实现对轴承故障的高【轴承故障诊断】基于融合鱼鹰和柯西变异的麻雀优化算法OCSSA-VMD-CNN-BILSTM轴承诊断研究【西储大学数据】(Matlab代码实现)精度诊断。研究采用西储大学公开的轴承故障数据集进行实验验证,通过优化VMD的模态数和惩罚因子,有效提升了信号分解的准确性与稳定性,随后利用CNN提取故障特征,BiLSTM捕捉时间序列的深层依赖关系,最终实现故障类型的智能识别。该方法在提升故障诊断精度与鲁棒性方面表现出优越性能。; 适合人群:具备一定信号处理、机器学习基础,从事机械故障诊断、智能运维、工业大数据分析等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①解决传统VMD参数依赖人工经验选取的问题,实现参数自适应优化;②提升复杂工况下滚动轴承早期故障的识别准确率;③为智能制造与预测性维护提供可靠的技术支持。; 阅读建议:建议读者结合Matlab代码实现过程,深入理解OCSSA优化机制、VMD信号分解流程以及CNN-BiLSTM网络架构的设计逻辑,重点关注参数优化与故障分类的联动关系,并可通过更换数据集进一步验证模型泛化能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值