Codeforces712 B. Passwords (贪心水题)
2016-10-01 11:02
1026 查看
题目连接:http://codeforces.com/contest/721/problem/B
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
output
input
output
Note
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串密码,按长度非递减输入(不是按给出的顺序输入),第n+1行密码是真密码,没输入k次密码后要等5s后才能再次输入,求最短时间与最长时间。
解题思路:直接贪心模拟。
/* ***********************************************
┆ ┏┓ ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃ ┃ ┆
┆┃ ━ ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃ ┃ ┆
┆┃ ┻ ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆ ┃ 勒 ┃ ┆
┆ ┃ 戈 ┗━━━┓ ┆
┆ ┃ 壁 ┣┓┆
┆ ┃ 的草泥马 ┏┛┆
┆ ┗┓┓┏━┳┓┏┛ ┆
┆ ┃┫┫ ┃┫┫ ┆
┆ ┗┻┛ ┗┻┛ ┆
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <bitset>
using namespace std;
#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define all(a) a.begin(), a.end()
#define mem(x,v) memset(x,v,sizeof(x))
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,k;
n = read(), k = read();
string ans;
string s[105];
for(int i=1;i<=n;i++)
cin>>s[i];
cin>>ans;
int cnt1 = 0,cnt2 = 0;
for(int i=1;i<=n;i++)
{
if(s[i].size()<ans.size())cnt1++;
else if(s[i].size()==ans.size())cnt2++;
}
int res1 = cnt1 / k;
int mi = cnt1+1+res1*5;
int res2 = (cnt1+cnt2-1) / k;
int mx = cnt1+cnt2+res2*5;
cout<<mi<<" "<<mx<<endl;
return 0;
}
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
Note
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串密码,按长度非递减输入(不是按给出的顺序输入),第n+1行密码是真密码,没输入k次密码后要等5s后才能再次输入,求最短时间与最长时间。
解题思路:直接贪心模拟。
/* ***********************************************
┆ ┏┓ ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃ ┃ ┆
┆┃ ━ ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃ ┃ ┆
┆┃ ┻ ┃ ┆
┆┗━┓ 马 ┏━┛ ┆
┆ ┃ 勒 ┃ ┆
┆ ┃ 戈 ┗━━━┓ ┆
┆ ┃ 壁 ┣┓┆
┆ ┃ 的草泥马 ┏┛┆
┆ ┗┓┓┏━┳┓┏┛ ┆
┆ ┃┫┫ ┃┫┫ ┆
┆ ┗┻┛ ┗┻┛ ┆
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <bitset>
using namespace std;
#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)
#define pb push_back
#define mp make_pair
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod 1000000007
#define LL long long
#define ULL unsigned long long
#define MS0(X) memset((X), 0, sizeof((X)))
#define SelfType int
SelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}
SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}
#define Sd(X) int (X); scanf("%d", &X)
#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define all(a) a.begin(), a.end()
#define mem(x,v) memset(x,v,sizeof(x))
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,k;
n = read(), k = read();
string ans;
string s[105];
for(int i=1;i<=n;i++)
cin>>s[i];
cin>>ans;
int cnt1 = 0,cnt2 = 0;
for(int i=1;i<=n;i++)
{
if(s[i].size()<ans.size())cnt1++;
else if(s[i].size()==ans.size())cnt2++;
}
int res1 = cnt1 / k;
int mi = cnt1+1+res1*5;
int res2 = (cnt1+cnt2-1) / k;
int mx = cnt1+cnt2+res2*5;
cout<<mi<<" "<<mx<<endl;
return 0;
}
相关文章推荐
- poj2586 贪心水题一道(一步贪心)
- VJ水题——今年暑假不AC(简单贪心)
- PAT甲题题解-1125. Chain the Ropes (25)-贪心水题
- poj+数模组要做的水题+Stripies 贪心
- 【2015ZUFE新生赛网络同步赛C】【水题 贪心】boom x乘(y+z)尽可能大
- UVa(11292),贪心水题
- HDU 1009 贪心水题
- “师创杯”山东理工大学第九届ACM程序设计竞赛 正式赛 G.打字【Dp+贪心】水题
- 贪心水题(区间覆盖)
- 【Codeforces Round 263 (Div 2)B】【水题 贪心】Appleman and Card Game 选字符,每个字符个数的平方
- NYOJ1036 非洲小孩(又一道贪心水题,区间选点问题)
- HDU 2019 Fighting for HDU(贪心水题)
- hrbust2223 水题 (贪心+二叉堆)
- HDU-5783 Divide the Sequence(贪心水题)
- 【Codeforces Round 336 (Div 2) A】【贪心 水题】Saitama Destroys Hotel 电梯只下不上 接n人到达0层最早时刻
- HDU 1789 贪心水题
- 水题贪心今年暑假不AC HDU - 2037
- HDU1009 贪心水题
- Hduoj1051 【贪心】【水题】
- 玲珑杯Round #13 A -- 你没看见A(贪心,水题)