zoukankan      html  css  js  c++  java
  • Heidi and Library (easy)(cf5.18团队赛模拟水题)

    Heidi and Library (easy)

    Your search for Heidi is over – you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.

    We will look at the library’s operation during n consecutive days. Heidi knows in advance that on the i-th day (1 ≤ i ≤ n) precisely one person will come to the library, request to borrow the book ai, read it in a few hours, and return the book later on the same day.

    Heidi desperately wants to please all her guests, so she will make sure to always have the book ai available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.

    There is a problem, though. The capacity of the library is k – this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.

    You are given k and the sequence of requests for books a1, a2, …, an. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?

    Input
    The first line of input will contain two integers n and k (1 ≤ n, k ≤ 80). The second line will contain n integers a1, a2, …, an (1 ≤ ai ≤ n) – the sequence of book requests.

    Output
    On a single line print the minimum cost of buying books at the store so as to satisfy all requests.

    Example

    input
    4 80
    1 2 2 1
    output
    2

    input
    4 1
    1 2 2 1
    output
    3

    input
    4 2
    1 2 3 1
    output
    3

    Note
    In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.

    In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.

    In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.

    这种签到题,我们当天比赛的时候竟然没切掉。。。(感觉自己活到今天也是奇迹)
    总体思路:暴力,每次图书馆满了我们就选择一本出现最靠后的书扔掉(或者是之后再也不会出现的书),要特别注意的是,不要把自己刚买的书扔掉!!!(本人傻傻分不清,wa了3次。。。)

    这里写代码片
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    
    using namespace std;
    
    const int N=101;
    int n,k;
    int a[N];
    int zl[N];
    bool have[N];
    int tot=0,ans=0;
    int p[N],f;
    
    void cl(int bh)
    {
        int i,j;
        memset(p,0,sizeof(p));
        for (i=bh+1;i<=n;i++)
        {
            if (p[a[i]]==0&&have[a[i]]&&a[i]!=a[bh])  //有这本书而且之后几天内会出现
            {  //显然我们不能把刚买的书又扔了 
                p[0]++;
                p[a[i]]=1;
                j=a[i];
            } 
        }
        for (i=1;i<=n;i++) 
        if (p[i]==0&&have[i]==1&&i!=a[bh]) 
        {  //我们有这本书,这本书之后不会出现且不是我们刚买进来的书 
            j=i;  //显然我们选择这本书扔掉更优 
            break;
        }
        have[j]=0;
        return;
    }
    
    int main()
    {
        memset(have,0,sizeof(have));
        scanf("%d%d",&n,&k);
        for (int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            p[a[i]]++;
            if (p[a[i]]==1) zl[0]++,zl[zl[0]]=a[i];  //不同的书的种类 
        }
        for (int i=1;i<=n;i++)  //从1到n扫一遍 
        {
            if (have[a[i]]==0&&tot<k)  //没有这本书,而且图书馆没满 
            {
                ans++; //直接买 
                tot++;
                have[a[i]]=1;
            }
            else if (have[a[i]])
            {
                continue;
            }
            else  //图书馆满了 
            {
                have[a[i]]=1;  //先把当前需要的书买进来 
                ans++;
                //扔掉哪本书呢???就是在最近用不到的书
                cl(i);
            }
        }
        printf("%d",ans);
        return 0;
    }

    很显然,这个写法的时间复杂度不忍直视,那这就激发我们探求更优的解法:
    B.Heidi and Library

    <<待续<<

  • 相关阅读:
    沙盒解决方案part1:沙盒解决方案的用途和好处
    session定义使用和丢失问题小结(20120101有更新&&20121026又更)
    普通pc检测维修的一个特例:检测\换板\电源\IDE信道\声卡驱动
    用户sa登录失败,该用户与可信sql server连接无关联
    trycatchfinally(C# 简单学习)
    按钮只能一次提交:ajax页面中调用ascx控件,如何设置ascx中按钮为false
    开始学习:Ajax的全称是:AsynchronousJavaScript+XML
    ajax学习的第一次亲密接触.(虽然还有一点模糊)
    .net2.0 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的
    创建可重复使用的控件ascx-一页只能有一个服务器端 Form 标记?
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673591.html
Copyright © 2011-2022 走看看