• 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 }
  • 相关阅读:
    OAuth2.0协议封装类
    FFmpeg学习笔记
    mysql学习笔记(有待增补)
    使用cnpm国内淘宝镜像命令
    mongoDB中的聚合操作示例
    react的学习日常整理
    服务器防止访问超时的一些参数设置
    MongoDB基本语句操作
    CI框架配置多个数据库
    php前后台登录交互明文传输使用base64加密
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8983588.html
Copyright © 2020-2023  润新知