洛谷 P3419 [POI2005]SAM-Toy Cars

题目描述

Johnny is a little boy - he is only three years old and enjoys playing with toy cars very much. Johnny has nn different cars. They are kept on a shelf so high, that Johnny cannot reach it by himself. As there is little space in his room, at no moment may there be more than kk toy cars on the floor.

Johnny plays with one of the cars on the floor. Johnny's mother remains in the room with her son all the time. When Johnny wants to play with another car that is on the floor, he reaches it by himself. But when the toy is on the shelf, his mummy has to hand it to him. When she gives Johnny one car, she can at the same time pick any car from the floor and put it back on the shelf (so that there remains sufficient space on the floor).

The mother knows her child very well and therefore can predict perfectly which cars Johnny will want to play with. Having this knowledge, she wants to minimize the number of times she has to hand Johnny a toy from the shelf. Keeping that in mind, she has to put the toys off on the shelf extremely thoughtfully.

TaskWrite a programme that:

reads from the standard input the sequence of toy cars in order in which Johnny will want to play with them,calculates the minimal number of times the mother has to pick cars from the shelf,writes the result to the standard output.

Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio 拿不到它们. 为了让他的房间有足够的空间,在任何时刻地板上都不会有超过k 个玩具. Jasio 在地板上玩玩具. Jasio'的妈妈则在房间里陪他的儿子. 当Jasio 想玩地板上的其他玩具时,他会自己去拿,如果他想玩的玩具在架子上,他的妈妈则会帮他去拿,当她拿玩具的时候,顺便也会将一个地板上的玩具放上架子使得地板上有足够的空间. 他的妈妈很清楚自己的孩子所以他能够预料到Jasio 想玩些什么玩具. 所以她想尽量的使自己去架子上拿玩具的次数尽量的少,应该怎么安排放玩具的顺序呢?

输入输出格式

输入格式:

 

In the first line of the standard input there are three integers: nn,kk,pp (1\le k\le n\le 100\ 0001kn100 000, 1\le p\le 500\ 0001p500 000), separated by single spaces. These denote respectively: the total number of cars, the number of cars that can remain on the floor at once and the length of the sequence of cars which Johnny will want to play with. Each of the following pp lines contains one integer. These integers are the numbers of cars Johnny will want to play with (the cars are numbered from 11 to nn).

 

输出格式:

 

In the first and only line of the standard output one integer should be written - the minimal number of times his mother has to pick a car from the shelf.

 

输入输出样例

输入样例#1:
3 2 7
1
2
3
1
3
1
2
输出样例#1:
4
思路:经典的贪心
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 500001
using namespace std;
struct nond{
    int net,id;
}heap[MAXN];
int ans;
int vis[MAXN];
int n,k,m,size;
int a[MAXN],net[MAXN],last[MAXN];
void add(int net,int x){
    size++;
    int place=size,parent=size>>1;
    while(parent>=1&&heap[parent].net<net){
        heap[place]=heap[parent];
        place=parent;
        parent>>=1;
    }
    heap[place].net=net;
    heap[place].id=x;
}
void deletee(){
    nond now=heap[size--];
    int place=1,child=2;
    while(child<size){
        if(child<size&&heap[child].net<heap[child+1].net)
            child++;
        if(heap[child].net>now.net){
            heap[place]=heap[child];
            place=child;
            child<<=1;
        }
        else break;
    }
    heap[place]=now;
    return ;
}
int main(){
    scanf("%d%d%d",&n,&k,&m);
    for(int i=1;i<=m;i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)    last[i]=MAXN+1;
    for(int i=m;i>=1;i--){
        net[i]=last[a[i]];
        last[a[i]]=i;
    }    
    int cnt=0;
    for(int i=1;i<=m;i++){
        if(vis[a[i]]){
            add(net[i],a[i]);
            continue;
        }
        if(cnt<k){
            ans++;
            cnt++;
            add(net[i],a[i]);
            vis[a[i]]=1;
        }
        else{
            while(vis[heap[1].id]==0)    deletee();
            vis[heap[1].id]=0;
            deletee();
            ans++;
            vis[a[i]]=1;
            add(net[i],a[i]);
        }
    }
    cout<<ans;
}

 

 

转载于:https://www.cnblogs.com/cangT-Tlan/p/7654754.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大学生就业服务平台管理系统按照操作主体分为管理员和用户。管理员的功能包括学生档案管理、字典管理、试卷管理、试卷选题管理、试题表管理、考试记录表管理、答题详情表管理、错题表管理、法律法规管理、法律法规收藏管理、法律法规留言管理、就业分析管理、论坛管理、企业管理、简历管理、老师管理、简历投递管理、新闻资讯管理、新闻资讯收藏管理、新闻资讯留言管理、学生信息管理、宣传管理、学生管理、职位招聘管理、职位收藏管理、招聘咨询管理、管理员管理。用户的功能等。该系统采用了Mysql数据库,Java语言,Spring Boot框架等技术进行编程实现。 大学生就业服务平台管理系统可以提高大学生就业服务平台信息管理问题的解决效率,优化大学生就业服务平台信息处理流程,保证大学生就业服务平台信息数据的安全,它是一个非常可靠,非常安全的应用程序。 管理员权限操作的功能包括管理新闻信息,管理大学生就业服务平台信息,包括考试管理,培训管理,投递管理,薪资管理等,可以管理新闻信息。 考试管理界面,管理员在考试管理界面中可以对界面中显示,可以对考试信息的考试状态进行查看,可以添加新的考试信息等。投递管理界面,管理员在投递管理界面中查看投递种类信息,投递描述信息,新增投递信息等。新闻信息管理界面,管理员在新闻信息管理界面中新增新闻信息,可以删除新闻信息。新闻信息类型管理界面,管理员在新闻信息类型管理界面查看新闻信息的工作状态,可以对新闻信息的数据进行导出,可以添加新新闻信息的信息,可以编辑新闻信息信息,删除新闻信息信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值