2019.08.25校内模拟赛Page

page.png
这个题目其实我一眼就看出来是原题了,原题是\(SPOJ688\)也就是\(POI2005\)的题.
原题\(link\)在这里:原题
正如许多人想的一样,这题正解就是个贪心.
如果说出现缺页(需要拿新玩具),而我们还有空间可以放,那么就直接拿出来,\(++ans\).
如果没有空间了,我们就把空间里出现次数最早的一次最晚的那个页面(玩具)拿出来,把新的放进去.
贪心思路十分简单,主要的难度在于实现.实现上,目前已知的除了暴力有两种做法.
第一种是权值线段树的做法想出此种做法的大佬,第二种是\(STL\)\(set\)做法想出此种做法的大佬.
我自己是写了一个\(O(n^2)\)的贪心,但是由于这题的数据有梯度,所以我过了\(75pts\).为什么不写正解呢?因为我是懒癌重度患者...(反正只差\(25pts\))
我写的做法是每次加入一个新数需要更新内存的时候就暴力去找空间里出现次数最早的一次最晚的那一个,然后开桶记录内存里有啥.
暴力做法如下:
\(Code:\)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <map>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = a ; i <= b ; ++ i)
#define per(i,a,b) for (int i = a ; i >= b ; -- i)
#define pii pair < int , int >
#define X first
#define Y second
#define rint read<int>
#define int long long
#define pb push_back

using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;

template < class T >
    inline T read () {
        T x = 0 , f = 1 ; char ch = getchar () ;
        while ( ch < '0' || ch > '9' ) {
            if ( ch == '-' ) f = - 1 ;
            ch = getchar () ;
        }
        while ( ch >= '0' && ch <= '9' ) {
            x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
            ch = getchar () ;
       }
   return f * x ;
}

const int N = 2e5 + 100 ;

int n , k , v[N] , tot , ans ;
bool mk[N] , cnt[N] ;

signed main() {
    freopen ("page.in" , "r" , stdin) ;
    freopen ("page.out" , "w" , stdout) ;
    n = rint () ; k = rint () ;
    rep ( i , 1 , n ) v[i] = rint () ;
    rep ( i , 1 , n ) {
        if ( cnt[v[i]] ) continue ;
        else if ( tot < k ) ++ ans , ++ tot , cnt[v[i]] = true ;
        else {
            MEM ( mk , false ) ; int pos ;
            rep ( j , i + 1 , n ) {
                if ( mk[v[j]] || ! cnt[v[j]] ) continue ;
                pos = j ; mk[v[j]] = true ;
            }
            rep ( k , 1 , i ) if ( cnt[v[k]] && ! mk[v[k]] ) pos = k ;
            ++ ans ; cnt[v[pos]] = false ; cnt[v[i]] = true ;
        }
    }
    printf ("%lld\n" , ans ) ; return 0 ;
}

而权值线段树的做法由于太过繁琐,我决定不去码它.而是去码\(set\)的做法:
\(Code:\)

咕咕咕咕咕~

转载于:https://www.cnblogs.com/Equinox-Flower/p/11408760.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值