-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeats.cpp
More file actions
75 lines (75 loc) · 2.12 KB
/
Copy pathSeats.cpp
File metadata and controls
75 lines (75 loc) · 2.12 KB
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
70
71
72
73
74
75
#include<iostream>
int solve(const std::string &str){
//string contains '.' and 'x'
//'.' means unoccupied Seat and 'x' means occupied Seat
//find median person
//median person is the person such that people sitting on the left of it is equal to the person right on it
int totalPersons = 0;
const int n = str.length();
int medianIndex = 0;
for(int index = 0;index<n;++index){
//count total people in the row
if(str[index]=='x'){
++totalPersons;
}
}
if(totalPersons%2==0){
//left of median will one less than right of the median
int personCount = totalPersons/2;
int index = 0;
while(1){
if(str[index]=='x'){
--personCount;
}
if(personCount==0){
break;
}
++index;
}
medianIndex = index;
}
else{
//left of median will be equal to right of median
int personCount = totalPersons/2 + 1;
int index = 0;
while(1){
if(str[index]=='x'){
--personCount;
}
if(personCount==0){
break;
}
++index;
}
medianIndex = index;
}
int totalHops,leftHops=0,rightHops=0;
//calculating hops required by person sitting on left of median
int emptyPlace=medianIndex-1;
for(int index = medianIndex-1;index>=0;--index){
if(str[index]=='x'){
//this index is filled
leftHops = leftHops + (emptyPlace - index);
--emptyPlace;
}
}
emptyPlace = medianIndex+1;
for(int index = medianIndex+1;index<n;++index){
if(str[index]=='x'){
//this index is filled
rightHops = rightHops + (index - emptyPlace);
++emptyPlace;
}
}
//std::cout<<"lefthops: "<<leftHops;
//std::cout<<"righthops: "<<rightHops;
totalHops = leftHops + rightHops;
return totalHops;
}
int main(){
std::string str;
std::cin>>str;
int output = solve(str);
std::cout<<output<<std::endl;
return 0;
}