zoukankan      html  css  js  c++  java
  • BZOJ1528 [POI2005]sam-Toy Cars

    贪心的思想,每次如果要放回去的话,一定是那个下次时间最后的玩具。

    于是直接用堆模拟玩一遍就好了。

    蒟蒻表示只是懒了不想写struct用了pair结果各种报错各种跪你至于吗g++编译器!!!

     1 /**************************************************************
     2     Problem: 1528
     3     User: rausen
     4     Language: C++
     5     Result: Accepted
     6     Time:716 ms
     7     Memory:13304 kb
     8 ****************************************************************/
     9  
    10 #include <cstdio>
    11 #include <queue>
    12 #include <utility>
    13  
    14 using namespace std;
    15 const int N = 500005;
    16  
    17 int n, k, p, ans;
    18 int a[N], next[N], last[N];
    19 bool v[N];
    20 priority_queue <pair <int, int> > h;
    21  
    22 int main() {
    23     int i;
    24     scanf("%d%d%d", &n, &k, &p);
    25     for (i = 1; i <= n; ++i)
    26         last[i] = p + 1;
    27     for (i = 1; i <= p; ++i)
    28         scanf("%d", a + i);
    29     for (i = p; i; --i) {
    30         next[i] = last[a[i]];
    31         last[a[i]] = i;
    32     }
    33     for (i = 1; i <= p; ++i) {
    34         if (v[a[i]])
    35             h.push(make_pair(next[i], a[i])); else
    36         if (k) {
    37             h.push(make_pair(next[i], a[i]));
    38             v[a[i]] = 1, --k, ++ans;
    39         } else {
    40             while (!v[h.top().second]) h.pop();
    41             v[h.top().second] = 0, h.pop();
    42             h.push(make_pair(next[i], a[i]));
    43             v[a[i]] = 1, ++ans;
    44         }
    45     }
    46     printf("%d
    ", ans);
    47     return 0;
    48 }
    View Code

    (p.s.  忘了初始化last数组结果贡献1WA)

    By Xs酱~ 转载请说明 博客地址:http://www.cnblogs.com/rausen
  • 相关阅读:
    NFS与通配符
    yum管理RPM包与linux网络设置
    git常用命令总结——覆盖日常开发全操作
    inner join on会过滤掉两边空值的条件
    入园第一天
    P3750 [六省联考2017]分手是祝愿 题解
    CSP2021 爆零记
    CSP 前模板记录(黄题篇)
    对拍
    2021.10.20CSP模拟模拟赛 赛后总结
  • 原文地址:https://www.cnblogs.com/rausen/p/4162917.html
Copyright © 2011-2022 走看看