CodeForces721B R - Passwords

xiaoxiao2021-02-27  289

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 Print two integers — time (in seconds), Vanya needs to be authorized to Codehorses in the best case for him and in the worst case respectively.

Example Input 5 2 cba abc bb1 abC ABC abc Output 1 15 Input 4 100 11 22 1 2 22 Output 3 4 Note 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 5 seconds 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.

Consider the second sample case. There is no way of entering passwords and get the access to the site blocked. As soon as the required password has length of 2, Vanya enters all passwords of length 1 anyway, spending 2 seconds for that. Then, in the best case, he immediately enters the correct password and the answer for the best case is 3, but in the worst case he enters wrong password of length 2 and only then the right one, spending 4 seconds at all.

题意:这个人有很多的密码,但他忘了这个网站的密码,所以输入N个密码,再输入正确的密码,求他找到正确的密码所需要花费的最少和最多的时间。其中他肯定是从密码长度由小到大找的,如果密码长度相同,则随便顺序输入。网站在输错K个密码的时候会冻结五分钟,才能继续输入。

思路:保存每个长度的密码个数,先算出没有冻结情况的时候需要花费的时间,最少的时间是小于正确密码长度的个数+1,最长的时间就是小于等于正确密码长度的个数。再看冻结五分钟的情况,直接个数/k*5就可以了,但要记得排除最后一个输入刚好是正确答案,刚好满K个错误的情况。

#include<iostream> #include<algorithm> #include<map> using namespace std; map<int,int> mp; int main() { int n,k; while(cin >> n >> k) { string pas[200]; for(int i =0 ;i<n;i++) cin >> pas[i]; sort(pas,pas+n); string ans; cin >> ans; int num2=0; int num1 = 1; for(int i=0;i<n;i++) mp[pas[i].length()]++; map<int,int>::iterator iter; for(iter = mp.begin();iter!=mp.end();iter++) { if(iter->first == ans.length())break; num2 = num2+iter->second; num1 = num1+iter->second; } num2 = num2+mp[ans.length()]; num1 = num1+(num1%k==0? (num1/k-1)*5:(num1/k*5)); num2 = num2+(num2%k==0? (num2/k-1)*5:(num2/k*5)); cout<<num1 << " " << num2<<endl; } return 0; }
转载请注明原文地址: https://www.6miu.com/read-4358.html

最新回复(0)