JavaW的技术专栏 A CSU coder

【洛谷-P1540】解题报告(模拟)

2020-06-11

C++

原始题目

P1540 机器翻译

题目大意

给出内存容量和查找数列,求访问外存数量

解题思路

队列模拟(事后想想不需要,记录当前容量就行),vis数组记录状态。

解题代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;

#define rep(i, a, b) for (int i = a; i < b; ++i)
#define per(i, a, b) for (int i = b - 1; i >= a; --i)
int vis[maxn];
queue<int> q;
int n, m;
int main()
{
    ios::sync_with_stdio(false);
    while (cin >> n >> m) {
        // s.clear();
        memset(vis, 0, sizeof(vis));
        int ans = 0;
        rep(i, 0, m)
        {

            int a;
            cin >> a;
            if (vis[a])
                continue;
            else {
                if (q.size() == n) {
                    int temp = q.front();
                    q.pop();
                    vis[temp] = 0;
                    vis[a] = 1;
                    q.push(a);
                    ans++;
                } else {
                    q.push(a);
                    vis[a] = 1;
                    ans++;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

收获与反思

none


Comments

Content