-
Notifications
You must be signed in to change notification settings - Fork 1
/
P21_Policemen catch thieves.cpp
69 lines (63 loc) · 1.36 KB
/
P21_Policemen catch thieves.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int findTheifIndex(vector<char> temp)
{
for(int i = 0; i < temp.size(); i++)
{
if(temp[i] == 'T')
return i;
}
return -1;
}
int findPoliceIndex(vector<char> temp)
{
for(int i = 0; i < temp.size(); i++)
{
if(temp[i] == 'P')
return i;
}
return -1;
}
int main() {
int n, k;
cin>>n;
cin>>k;
vector<char> arr(n);
for(int i = 0; i < n; i++)
{
char temp;
cin>>temp;
arr[i] = temp;
}
int counter = 0;
int theifIndex = findTheifIndex(arr);
int policeIndex = findPoliceIndex(arr);
while(theifIndex != -1 && policeIndex != -1)
{
if(abs(theifIndex - policeIndex) <= k)
{
counter++;
arr[theifIndex] = 'E';
arr[policeIndex] = 'E';
theifIndex = findTheifIndex(arr);
policeIndex = findPoliceIndex(arr);
}
else
{
if(theifIndex < policeIndex)
{
arr[theifIndex] = 'E';
theifIndex = findTheifIndex(arr);
}
else
{
arr[policeIndex] = 'E';
policeIndex = findPoliceIndex(arr);
}
}
}
cout<<counter<<endl;
return 0;
}