1779A: Hall of Fame

Where s[0, n] is the array of lamps: If there exists some index pair i, j such that

  • i < j
  • s[i] is facing right
  • s[j] is facing left ... then the entire array will be illuminated.

Since the direction each lamp faces is a binary choice (left or right), there will exist some "boundary" where the lamp on the left is not facing the same direction as the lamp on the right, so we can just iterate over 2-sized windows of the array and print the first good/swappable pair we see.


_30
#include <iostream>
_30
#include <string>
_30
_30
using namespace std;
_30
_30
int main () {
_30
int t;
_30
cin >> t;
_30
while (t--) {
_30
int n;
_30
cin >> n;
_30
string s;
_30
cin >> s;
_30
bool good = false;
_30
for (int i = 0; i < n-1; ++i) {
_30
if (s[i]=='L' && s[i+1]=='R') {
_30
good = true;
_30
cout << i+1 << '\n';
_30
break;
_30
} else if (s[i]=='R' && s[i+1]=='L') {
_30
good = true;
_30
cout << 0 << '\n';
_30
break;
_30
}
_30
}
_30
if (!good) cout << -1 << '\n';
_30
}
_30
_30
return 0;
_30
}

If you found this solution helpful, consider leaving a star!