zoukankan      html  css  js  c++  java
  • codeforces 721B B. Passwords(贪心)

    题目链接:

    B. Passwords

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration.

    Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice.

    Entering any passwords takes one second for Vanya. But if Vanya will enter wrong password k times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that.

    Determine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds).

    Input

    The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the number of Vanya's passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds.

    The next n lines contains passwords, one per line — pairwise distinct non-empty strings consisting of latin letters and digits. Each password length does not exceed 100 characters.

    The last line of the input contains the Vanya's Codehorses password. It is guaranteed that the Vanya's Codehorses password is equal to some of his n passwords.

    Output

    Consider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he enters another 4 passwords before. He spends 2 seconds to enter first 2 passwords, then he waits 5seconds as soon as he made 2 wrong tries. Then he spends 2 more seconds to enter 2 wrong passwords, again waits 5 seconds and, finally, enters the correct password spending 1 more second. In summary in the worst case he is able to be authorized in 15 seconds.

    Examples
    input
    5 2
    cba
    abc
    bb1
    abC
    ABC
    abc
    output
    1 15
    input
    4 100
    11
    22
    1
    2
    22
    output
    3 4


    题意:
    给了一堆密码,然后按长度从小到大开始试,问最少和最多的花费时间;

    思路:

    贪心;

    AC代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <bits/stdc++.h>
    #include <stack>
    #include <map>
      
    using namespace std;
      
    #define For(i,j,n) for(int i=j;i<=n;i++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    #define lson o<<1
    #define rson o<<1|1
    typedef  long long LL;
      
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
      
    const LL mod=1e9+7;
    const double PI=acos(-1.0);
    const LL inf=1e18;
    const int N=1e6+10;
    const int maxn=1e5+10;
    const double eps=1e-12;
    
    int n,k,a[200];
    string s[200],ans;
    int main()
    {
        read(n);read(k);
        for(int i=1;i<=n;i++){cin>>s[i],a[i]=s[i].length();}
        cin>>ans;
        int len=ans.length(),mi=0,eq=0;
        for(int i=1;i<=n;i++)
        {
            if(a[i]<len)mi++;
            else if(a[i]==len)eq++;
        }
        cout<<mi+mi/k*5+1<<" "<<mi+eq+(mi+eq-1)/k*5<<endl;
    
        
    
        return 0;
    }
    

      

  • 相关阅读:
    iOS 友盟统计
    iOS 高德地图 根据经纬度解析成位置
    十六、Swift 可选值链条 Optional Chaining
    十五、自动引用计数 Automatic Reference Counting
    十四、析构器 Deinitialization
    BCB应对WIN7的放大屏幕文本技术
    十三、初始化 Initialization
    十二、继承 Inheritance
    十一、下标 Subscripts
    十、方法 Methods
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5925460.html
Copyright © 2011-2022 走看看