页面置换算法

26 篇文章 0 订阅
14 篇文章 0 订阅

CF-GYM-101498-F

While cooking your dinner, you will need n ingredients. Initially, all ingredients are in the refrigerator.

You are not allowed to keep more than k ingredients outside the refrigerator at the same time. If there are k ingredients outside the refrigerator, and you need to use another ingredient from it, then you must do the following:

1 Open the refrigerator.
2 Return an ingredient that is currently outside.
3 Take the required ingredient.

Whenever you need an ingredient, you will open the refrigerator only if it’s not outside. Each time you open the refrigerator you can take only one item. Your task is to minimize the number of times you will need to open the refrigerator.

Input

The first line contains an integer T (1  ≤  T  ≤  100), where T is the number of test cases.

Each case contains two lines. The first line contains two integers n and k (1  ≤  n, k  ≤  105), the number of ingredients needed, and the maximum allowed number of ingredients that can be kept outside the refrigerator at the same time.

The second line contains n integers a1, a2, …, an (1  ≤  ai  ≤  109), where ai is the ID of the ith ingredient you will need. Ingredients must be used in the order given in the input.

Output

For each test case, print a single integer that represents the minimum number of times you will open the refrigerator.

Example

Input

2
5 3
2 4 5 2 1
7 3
1 2 3 4 3 2 1

Output

4
5

Note

In the first test case, you can keep up to 3 items outside the refrigerator. You must open the refrigerator 3 times to use the first three ingredients (2, 4, and 5), and then keep them outside the refrigerator. The fourth ingredient (2) is already outside the refrigerator, so you can use it directly. You cannot use the fifth item because there are 3 items outside the refrigerator, so you must open the refrigerator, return an item, and take the fifth ingredient from the refrigerator and use it.

影响次数的关键在于每次放回去的时候放哪个。每次需要把在外面的元素,下一个第一个出现的位置最远的那个元素,放回去。没有下一次出现的,就更优先放回去。

模拟即可,过程实现比较复杂

#include<iostream>
#include<stdio.h>
#include<string>
#include<string.h>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
#include<vector>
#include<functional>
using namespace std;
typedef long long ll;
int  a[100000 + 10];
int n, k;
map<int, int, greater<int> >s;
map<int, queue<int> >index;
map<int, int>outside;
int ans;
void take_out(int i, int x)
{
    outside[x] = 1;
    index[x].pop();
    if (index[x].size())
    {
        s[index[x].front()] = x;
    }
    else
    {
        s[n + x] = x;
    }
    ans++;
}
void push_in(int i, int x)
{
    outside[x] = 0;
    s.erase(i);
}
int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        index.clear();
        s.clear();
        outside.clear();
        ans = 0;
        scanf("%d%d", &n, &k);
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
            index[a[i]].push(i);
        }
        for (int i = 0; i < n; i++)
        {
            if (outside[a[i]])
            {
                if (index[a[i]].size())
                    index[a[i]].pop();
                s.erase(i);
                if (index[a[i]].size())
                { 
                    s[index[a[i]].front()] = a[i];
                }
                else
                {
                    s[a[i] + n] = a[i];
                }
            }
            else
            {
                if (s.size() < k)
                {
                    take_out(i, a[i]);
                }
                else
                {
                    push_in(s.begin()->first, s.begin()->second);
                    take_out(i, a[i]);
                }
            }
        }
        cout << ans << endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值