【codeforces 721B】B. Passwords【输入密码按长度非递减顺序输入,每输一次耗时1秒,输错k次等待5秒,最后一行为正确密码,问消耗的最少和最多时间】

传送门: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

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.

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


题意:

输入密码按长度非递减顺序输入,每输一次密码耗时1秒,输错k次等待5秒,最后一行为正确密码,问消耗的最少和最多时间。

思路:

对密码长度进行桶排序,算出比正确密码长度短的密码个数,最少时间为下次输入即正确,最多为该长度最后输入才正确

代码一:

#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << "  " ;
#define pl(x) cout << #x << "= " << x << endl;
#define ll __int64
using  namespace  std;

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 cnt[105];//长度为i有多少个
string s;
int n,k;


int  main(){
  read(n);read(k);
  for(int i=1; i<=n; i++){
    cin>>s;
    cnt[s.size()]++;
  }
  cin>>s;
  int len=s.size();
  int mn=0,mx=0;
  for(int i=1; i<=len; i++){
    if(i==len){
      mn+=1;
      mx+=cnt[i];
    }else{
      mn+=cnt[i];
      mx+=cnt[i];
    }
  }
  mn+=(mn-1)/k*5;mx+=(mx-1)/k*5;
  cout<<mn<<' '<<mx<<endl;
  return 0;
}


代码二:

(自己的丑代码,要有敢于批判自己的精神_(:зゝ∠)_)

#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << "  " ;
#define pl(x) cout << #x << "= " << x << endl;
#define ll __int64
using  namespace  std;

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 cnt[105];//长度为i有多少个
string s;
int n,k;


int  main(){
/*  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  #ifndef ONLINE_JUDGE
  freopen("in.txt","r",stdin);
  #endif*/

  read(n);read(k);
  for(int i=1; i<=n; i++){
    cin>>s;
    cnt[s.size()]++;
  }
  cin>>s;
  int len=s.size();
  int tmp,sum=0;
  for(int i=1; i<len; i++){//先处理长度小的
    if(cnt[i]==0)continue;
    sum+=cnt[i];
  }
  int ans=0;
  tmp=sum%k;//多下来的不能达到k的
  ans+=((sum-tmp)+5*(sum/k));
  int mn=0,mx=0;
  mn+=(ans+tmp+1);
  sum=(tmp+cnt[len]);
  mx+=ans+sum+(5*((sum-1)/k));
  cout<<mn<<' '<<mx<<endl;
  return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值