zoukankan      html  css  js  c++  java
  • Helvetic Coding Contest 2017 online mirror (teams allowed, unrated) A

    Description

    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.

    Examples
    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 2before 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.

    题意:学弟给我是这么解释的:有n本书,书架只能放k本,多出来只能从原来的书架丢出来

    比如4 80这组,都放得下,前面1,2都买过了,那么以后不需要买了,所以是2

    4 1这组,买1放入书架,放不下了,丢掉1,买2放入书架,一共是2

    4 2这组,买1,2放入书架,买3丢2(因为1以后要用),再丢1放最后一本1,一共是3

    解法:刚开始就想到是操作系统里面的页面置换算法,到底是哪一种呢,当然是最不可能实现的那种,最佳置换算法啦,就是当前使用,以后很久以后才又有用,或者以后也用不到了,因为现实是不知道以后到底要不要用,题目已经告诉你全部数据了,当然很好写,第一题数据量不大就写一次暴力喽

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int a[123];
     6 int g[123];
     7 set<int> s;
     8 int n;
     9 
    10 int secr(int num,int k)
    11 {
    12     for(int i = k + 1 ; i<n;i++)
    13     {
    14         if(a[i] == num)
    15         {
    16             return (i-k);
    17         }
    18     }
    19     return 1000000;
    20 }
    21 
    22 int modify(int k)
    23 {
    24     int maxs = 0;
    25     int pos;
    26     for(auto x:s)
    27     {
    28         int q = secr(x,k);
    29         if(q > maxs)
    30         {
    31             maxs = q;
    32             pos = x;
    33         }
    34     }
    35     return pos;
    36 }
    37 
    38 int main()
    39 {
    40     int k;
    41     scanf("%d %d",&n,&k);
    42     for(int i=0;i<n;i++)
    43     {
    44         scanf("%d",a+i);
    45         g[a[i]]++;
    46     }
    47     int sum = 0;
    48     for(int i=0;i<n;i++)
    49     {
    50         if(s.size() == 0)
    51         {
    52             s.insert(a[i]);
    53             g[a[i]]--;
    54             sum ++ ;
    55         }
    56         else
    57         {
    58             if(s.find(a[i]) == s.end())
    59             {
    60                 sum++;
    61                 if(s.size() >= k)
    62                 {
    63                     s.erase(s.find(modify(i)));
    64                     s.insert(a[i]);
    65                     g[a[i]]--;
    66                 }
    67                 else
    68                 {
    69                     s.insert(a[i]);
    70                     g[a[i]]--;
    71                 }
    72             }
    73             else
    74             {
    75                 g[a[i]]--;
    76             }
    77         }
    78     }
    79     printf("%d
    ",sum);
    80     return 0;
    81 }
  • 相关阅读:
    Allegro绘制PCB流程
    KSImageNamed-Xcode
    UIApplicationsharedApplication的常用使用方法
    javascript中间AJAX
    hdu1845 Jimmy’s Assignment --- 完整匹配
    嵌入式控制系统和计算机系统
    Bean行为破坏之前,
    jsonkit 分解nsarray 时刻 一个错误
    IO 字符流学习
    2013级别C++文章9周(春天的)工程——运算符重载(两)
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/6965418.html
Copyright © 2011-2022 走看看