zoukankan      html  css  js  c++  java
  • 杂题训练

    杂练题之一

    1528: [POI2005]sam-Toy Cars

    Description

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

    Input

    第一行三个整数: n, k, p (1 <= k <= n <= 100.000, 1 <= p <= 500.000), 分别表示玩具的总数,地板上玩具的最多个数以及Jasio 他想玩玩具的序列的个数,接下来p行每行描述一个玩具编号表示Jasio 想玩的玩具.

    Output

    一个数表示Jasio 的妈妈最少要拿多少次玩具.

    Sample Input

    3 2 7
    1
    2
    3
    1
    3
    1
    2

    Sample Output

    4

    看到区间前后照应题,一般都是要记录一个last数组或next数组

    正如此题,明显不会是什么高级算法,而是乱搞贪心呗

    对于每个ai

    记录一个nxt[ai],表示下一次出现的位置

    特别的,如果它是那个种类的最后一个,nxt=n+1;

    然后用堆优化,每次找出堆中nxt最大的弹出(应该很好理解吧)

    code:

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<vector>
    using namespace std;
    #define Maxn 500010
    int a[Maxn],first[Maxn],nt[Maxn];
    bool mark[Maxn];
    struct node
    {
        int x,y;
        friend bool operator < (node x,node y)
        {
            return x.x<y.x;
        }
    };
    priority_queue<node > q;
    int main()
    {
        int n,k,p;
        scanf("%d%d%d",&n,&k,&p);
        for(int i=1;i<=p;i++) scanf("%d",&a[i]);
        memset(first,0,sizeof(first));
        for(int i=p;i>=1;i--)
        {
            nt[i]=first[a[i]];
            first[a[i]]=i;
        }
        for(int i=1;i<=p;i++) if(nt[i]==0) nt[i]=p+1;
        memset(mark,0,sizeof(mark));
        int cnt=0,ans=0;
        for(int i=1;i<=p;i++)
        {
            if(!mark[a[i]])
            {
                ans++;
                if(cnt<k) cnt++;
                else
                {
                    node nw=q.top();q.pop();
                    mark[nw.y]=0;
                }
                mark[a[i]]=1;
            }
            node now;
            now.x=nt[i];now.y=a[i];
            q.push(now);
        }
        printf("%d
    ",ans);
        return 0;
    }
    
  • 相关阅读:
    POJ1239
    HDU 2829 四边形不等式优化
    返回数字二进制的最高位位数o(n)
    矩阵快速幂 模板
    HDU4718 The LCIS on the Tree(LCT)
    HDU4010 Query on The Trees(LCT)
    HDU3487 Play With Chains(Splay)
    CF444C DZY Loves Colors
    HDU4836 The Query on the Tree(树状数组&&LCA)
    HDU4831&&4832&&4834
  • 原文地址:https://www.cnblogs.com/wzxbeliever/p/11622880.html
Copyright © 2011-2022 走看看