Helvetic Coding Contest 2017 online mirror (teams allowed, unrated)

G. Fake News (easy)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

As it's the first of April, Heidi is suspecting that the news she reads today are fake, and she does not want to look silly in front of all the contestants. She knows that a newspiece is fake if it contains heidi as a subsequence. Help Heidi assess whether the given piece is true, but please be discreet about it...

Input

The first and only line of input contains a single nonempty string s of length at most 1000 composed of lowercase letters (a-z).

Output

Output YES if the string s contains heidi as a subsequence and NO otherwise.

Examples
input
abcheaibcdi
output
YES
input
hiedi
output
NO
Note

A string s contains another string p as a subsequence if it is possible to delete some characters from s and obtain p.

看下给你的字符串是否存在“heidi”

#include<stdio.h>
char s[]="heidi",st[100005];
int n,num=0;
int main()
{
    scanf("%s",st+1);
    for(int i=1;st[i];++i)
    {
        if(st[i]==s[num])
        ++num;
        if(num==5) return 0*puts("YES");
    }
    puts("NO");
    return 0;
}
M. April Fools' Problem (easy)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The marmots have prepared a very easy problem for this year's HC2 – this one. It involves numbers nk and a sequence of npositive integers a1, a2, ..., an. They also came up with a beautiful and riveting story for the problem statement. It explains what the input means, what the program should output, and it also reads like a good criminal.

However I, Heidi, will have none of that. As my joke for today, I am removing the story from the statement and replacing it with these two unhelpful paragraphs. Now solve the problem, fools!

Input

The first line of the input contains two space-separated integers n and k (1 ≤ k ≤ n ≤ 2200). The second line contains nspace-separated integers a1, ..., an (1 ≤ ai ≤ 104).

Output

Output one number.

Examples
input
8 5
1 1 1 1 1 1 1 1
output
5
input
10 3
16 8 2 4 512 256 32 128 64 1
output
7
input
5 1
20 10 50 30 46
output
10
input
6 6
6 6 6 6 6 6
output
36
input
1 1
100
output
100
我在做这些水题真的好么?只当练英语了,n个数选k个最小的和,不开心的时候就A水题?
#include <bits/stdc++.h>
int a[2205];
using namespace std;
int main() {
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++)
    scanf("%d",&a[i]);
    sort(a,a+n);
    int s=0;
    for(int i=0;i<k;i++)
        s+=a[i];
    printf("%d",s);
    return 0;
}

 

J. Send the Fool Further! (easy)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on.

Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank?

Heidi's n friends are labeled 0 through n - 1, and their network of connections forms a tree. In other words, every two of her friends ab know each other, possibly indirectly (there is a sequence of friends starting from a and ending on b and such that each two consecutive friends in the sequence know each other directly), and there are exactly n - 1 pairs of friends who know each other directly.

Jenny is given the number 0.

Input

The first line of the input contains the number of friends n (3 ≤ n ≤ 100). The next n - 1 lines each contain three space-separated integers uv and c (0 ≤ u, v ≤ n - 1, 1 ≤ c ≤ 104), meaning that u and v are friends (know each other directly) and the cost for travelling between u and v is c.

It is guaranteed that the social network of the input forms a tree.

Output

Output a single integer – the maximum sum of costs.

Examples
input
4
0 1 4
0 2 2
2 3 3
output
5
input
6
1 2 3
0 2 100
1 4 2
0 3 7
3 5 10
output
105
input
11
1 0 1664
2 0 881
3 2 4670
4 2 1555
5 1 1870
6 2 1265
7 2 288
8 7 2266
9 2 1536
10 6 3378
output
5551
Note

In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2).

 

 就是简单的DFS一遍,找到从根到叶的最长路,这个自己当然是想不出来的,别人的代码真的很巧妙

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
typedef long long ll ;
#define rep(i, a, b) for (int i = a ; i <= b; ++ i)
const int N = 5005 ;
using namespace std ;
int n, ter[N], e, nxt[N], lnk[N], w[N] ;
void add(int x, int y, int z) {
    ter[++ e] = y, nxt[e] = lnk[x], lnk[x] = e, w[e] = z ;
}
int dfs(int p, int las) {
    int ret = 0 ;
    for (int i = lnk[p]; i; i = nxt[i]) if (ter[i] != las)
        ret = max(ret, dfs(ter[i], p) + w[i]) ;
    return ret ;
}

int main() {
    scanf("%d", &n) ;
    int x, y, z ;
    rep(i, 1, n - 1) {
        scanf("%d%d%d", &x, &y, &z) ;
        ++ x, ++ y ;
        add(x, y, z) ;
        add(y, x, z) ;
    }
    printf("%d\n", dfs(1, 0)) ;
    return 0 ;
}

 

A. Heidi and Library (easy)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 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.

Q巨和我们讲这个是页面置换算法,其中最完美的是OPT,但是好像硬件并没有这样实现

最佳置换算法(OPT)

什么是OPT

最佳置换算法,其所选择的被淘汰的页面将是以后永不使用的,或是在最长(未来)时间内不再被访问的页面。采用最佳置换算法通常可保证最低的缺页率。但是人们目前还无法与之,一个进程在内存的若干个页面中,哪一个页面是未来最长时间内不再被访问的,因而该算法是无法实现的,但是可以利用该算法取评价其他的算法。

算法思想

举例如下:


 

我们将页面队列存在一个Vector动态数组中。我们可以从图中得知:当发生页面置换时,就要寻找在未来最长时间内不再被访问的页面,将其置换出去,比如当内存中存在的页面为 7、0、1,且要访问页面2时,此时我们要寻找页面队列中将要访问到的页面2以后的页面队列(0、3、0、4、2、3、0、3、2、1、2、0、1、7、0、1)中,页面7、0、1哪个最久未被访问到,即寻找页面7、0、1在以后的队列中第一次出现的这三个页面的下标值最大的那一个。因为页面7在后面的页面队列中再次被访问到是数组中下标为17的地方,页面0再次被访问到是数组下标为4的地方,页面1再次被访问的是数组中下标为13,所以页面7是未来最久才被访问的页面,所以将页面7置换出去,将页面2调入内存中。

它通常有两个参数,即需要刷新的页面和时间,然后通过贪心算法进行求解

上面的这道题可以理解为你只能有k本书,而且n天内每个人到来看他所要的书都要有书读,所以就成了保留哪本书的贪心问题,所以每次去掉元素,应该要做的是去掉下一次出现位置最晚的元素!这个元素占用图书馆空间的时间最长!存在a[i]就是这本书买了还没有删除,如果当前图书数等于k,你又要读现在的这本书,只能先进行删除操作,再进行插入。显而易见,每次插入就是又去买了一本书。

#include <bits/stdc++.h>
using namespace std;
int a[88];
int n, k;
int main()
{
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
    set <int> s;
    int t = 0;
    for (int i = 0; i < n; i++)
    {
        if (!s.count(a[i]))
        {
            if (s.size() == k)
            {
                int M = -1, p = -1;
                for (auto it = s.begin(); it != s.end(); it++)
                {
                    int j;
                    for (j = i; j < n; j++)
                    {
                        if (a[j] == *it)
                        {
                            break;
                        }
                    }
                    if (j > M)
                    {
                        M = j;
                        p = *it;
                    }
                }
                s.erase(p);
            }
            s.insert(a[i]);
            t++;
        }
    }
    printf("%d\n", t);
}

 

 

posted @ 2017-05-30 20:10  暴力都不会的蒟蒻  阅读(739)  评论(0编辑  收藏  举报