zoukankan      html  css  js  c++  java
  • [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 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 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: , , (, ), 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 lines contains one integer. These integers are the numbers of cars Johnny will want to play with (the cars are numbered from to ).

    输出格式:

    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

    堆加贪心:

    首先用邻接链保存后一个点的位置,保存在next里;

    然后维护一个结构体大根堆,每次加入next和a[i]就可以了。

    也就是说:每次找一个最远的删掉就可以了。(本人一开始就是贪心策略错了,哈哈)

    注意一下细节:

    邻接链要赋初值;大根堆中每一个数不一定有效,所以不能用size记录大根堆中的数。

    以下是AC代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    const int N=100001;
    const int M=500001;
    int a[M];
    int n,k,m;
    int nxt[M],last[N];
    bool in[N];
    int read()
    {
        int k=0;
        char s=getchar();
        while(s<'0'||s>'9')s=getchar();
        while(s>='0'&&s<='9')k=k*10+s-'0',s=getchar();
        return k;
    }
    struct student
    {
        int next,id;
    }heap[M];
    int size;
    void insert(int nxt,int x)
    {
        size++;
        int place=size,parent=size>>1;
        while(parent>=1&&heap[parent].next<nxt)
        {
            heap[place]=heap[parent];
            place=parent;
            parent>>=1;
        }
        heap[place].next=nxt;
        heap[place].id=x;
        return;
    }
    void delet()
    {
        student x=heap[size--];
        int place=1,child=2;
        while(child<=size)
        {
            if(child<size&&heap[child].next<heap[child+1].next)child++;
            if(heap[child].next>x.next)
            {
                heap[place]=heap[child];
                place=child;
                child<<=1;
            }
            else break;
        }
        heap[place]=x;
        return;
    }
    int main()
    {
        int i,j,ans=0;
        n=read();k=read();m=read();
        for(i=1;i<=m;i++)
            a[i]=read();
        for(i=1;i<=n;i++)last[i]=M+1;
        for(i=m;i>=1;i--)
        {
            nxt[i]=last[a[i]];
            last[a[i]]=i;
        }
        int cnt=0;
        for(i=1;i<=m;i++)
        {
            if(in[a[i]]){insert(nxt[i],a[i]);continue;}
            if(cnt<k)
            {
                ans++;
                cnt++;
                insert(nxt[i],a[i]);
                in[a[i]]=1;
            }
            else
            {
                while(in[heap[1].id]==0)delet();
                in[heap[1].id]=0;
                delet();
                ans++;
                in[a[i]]=1;
                insert(nxt[i],a[i]);
            }
        }
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    《Python数据挖掘入门与实践》高清中文版PDF+英文版+源码下载
    discuz 论坛配置 QQ/163 网易邮箱
    Discuz! X3 去掉内容图片提示下载方法(去除图片提示下载附件)
    HTTPS的建立过程(SSL建立安全会话的过程)
    前端开发之走进Vue.js
    优雅统计代码耗时的4种方法!
    Maven配置多个远程仓库的实现方法
    idea maven 一直报错“Could not transfer artifact ......(系统找不到指定的路径。)”
    IntelliJ IDEA为类和方法自动添加注释
    maven “mvn clean package”和“mvn clean install”有什么不同?
  • 原文地址:https://www.cnblogs.com/huangdalaofighting/p/6786204.html
Copyright © 2011-2022 走看看