zoukankan      html  css  js  c++  java
  • bzoj 1528 [POI2005]sam-Toy Cars 堆维护+贪心

    1528: [POI2005]sam-Toy Cars

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 716  Solved: 306
    [Submit][Status][Discuss]

    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
     
    题解:首先猜一个结论,然后贪心,每次拿的时候放回最远的需要玩的玩具,
       这个预处理+堆优化即可。
     1 #include<cstring>
     2 #include<cmath>
     3 #include<cstdio>
     4 #include<algorithm>
     5 #include<iostream>
     6 #include<queue>
     7 
     8 #define ll long long
     9 #define inf 1000000007
    10 #define N 500007
    11 #define pa pair<int,int>
    12 
    13 #define Wb putchar(' ')
    14 #define We putchar('
    ')
    15 #define rg register int
    16 using namespace std;
    17 inline int read()
    18 {
    19     int x=0,f=1;char ch=getchar();
    20     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    21     while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    22     return x*f;
    23 }
    24 inline void write(ll x)
    25 {
    26     if(x<0) putchar('-'),x=-x;
    27     if (x==0) putchar(48);
    28     int num=0;char c[20];
    29     while(x) c[++num]=(x%10)+48,x/=10;
    30     while(num) putchar(c[num--]);
    31 }
    32 
    33 int n,K,p,ans;
    34 int a[N],nxt[N],last[N];
    35 bool mark[N];
    36 priority_queue<pa,vector<pa> >q;
    37 
    38 int main()
    39 {
    40     n=read();K=read();p=read();
    41     for(int i=1;i<=n;i++)last[i]=p+1;
    42     for(int i=1;i<=p;i++)
    43         a[i]=read();
    44     for(int i=p;i;i--)
    45     {
    46         nxt[i]=last[a[i]];
    47         last[a[i]]=i;
    48     }
    49     for(int i=1;i<=p;i++)
    50     {
    51         if(mark[a[i]]){q.push(make_pair(nxt[i],a[i]));continue;}
    52         if(K)
    53         {
    54             ans++;q.push(make_pair(nxt[i],a[i]));
    55             mark[a[i]]=1;
    56             K--;
    57         }
    58         else
    59         {
    60             while(mark[q.top().second]==0)q.pop();
    61             mark[q.top().second]=0;
    62             q.pop();
    63             ans++;q.push(make_pair(nxt[i],a[i]));
    64             mark[a[i]]=1;
    65         }
    66     }
    67        write(ans);
    68 }
  • 相关阅读:
    我的Java学习推荐书目
    BTrace使用简介
    BTrace使用小结
    如何在生产环境使用Btrace进行调试
    BTrace : Java 线上问题排查神器
    淘宝Tprofiler工具实现分析
    JVM 性能调优实战之:使用阿里开源工具 TProfiler 在海量业务代码中精确定位性能代码
    性能工具TProfiler介绍文档
    分布式系统理论基础
    微信小程序
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8983588.html
Copyright © 2011-2022 走看看