Problem:
Extraordinary Girl (I)
She is an extraordinary girl. She works for a library. Since she is young
and cute, she is forced to do a lot of laborious jobs. The most annoying job
for her is to put returned books into shelves, carrying them by a cart. The
cart is large enough to carry many books, but too heavy for her. Since she
is delicate, she wants to walk as short as possible when doing this job.
    The library has 4N shelves (1 <= N <= 10000), three main passages, and
N + 1 sub passages. Each shelf is numbered between 1 to 4N as shown in
Figure 1. Horizontal dashed lines correspond to main passages, while vertical
dashed lines correspond to sub passages. She starts to return books from the
position with white circle in the figure and ends at the position with black
circle. For simplicity, assume she can only stop at either the middle of shelves
or the intersection of passages. At the middle of shelves, she put books with
the same ID as the shelves. For example, when she stops at the middle of
shelf 2 and 3, she can put books with ID 2 and 3.
Since she is so lazy that she doesn’t want to memorize a complicated
route, she only walks main passages in forward direction (see an arrow in
Figure 1). The walk from an intersection to its adjacent intersections takes 1
cost. It means the walk from the middle of shelves to its adjacent intersection,
and vice versa, takes 0.5 cost. You, as only a programmer amoung her friends,
are to help her compute the minimum possible cost she takes to put all books
in the shelves.
Input
The first line of input contains the number of test cases, T . Then T test
cases follow. Each test case consists of two lines. The first line contains the
number N , and the second line contains 4N characters, either Y or N . Y in
n-th position means there are some books with ID n, and N means no book
with ID n.
Output
The output should consists of T lines, each of which contains one integer,
the minimum possible cost, for each test case.
Sample Input
2
2
YNNNNYYY
4
NYNNYYNNNYNYYNNN
Output for the Sample Input
6
9

Submitted solutions:

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
int main(){
	int q;
	cin>>q;
	while(q--){
		int n,N;
		cin>>n;
		N=n*2;
		string s;cin>>s;
		rep(i,s.size())s[i]=(s[i]=='Y'?1:0);
		string t=s.substr(N);
		s=s.substr(0,N);
		vvi dp(n+1,vi(2,inf));
		if(t[0])dp[0][1]=2;
		else if(s[0]){
			dp[0][1]=1;
			dp[0][0]=1;
		}else dp[0][0]=0;
		rep(i,n){
			int a,b;
			if(i!=n-1){
				a=s[i*2+2]||s[i*2+1];
				b=t[i*2+2]||t[i*2+1];
			}else{
				a=s[i*2+1];
				b=t[i*2+1];
			}
			if(dp[i][0]!=inf){
				if(b)dp[i+1][1]=min(dp[i+1][1],dp[i][0]+2);
				else if(a){
					dp[i+1][1]=min(dp[i+1][1],dp[i][0]+1);
					dp[i+1][0]=min(dp[i+1][0],dp[i][0]+1);
				}else dp[i+1][0]=min(dp[i+1][0],dp[i][0]);
			}
			if(dp[i][1]!=inf){
				if(a+b==2){
					dp[i+1][1]=min(dp[i+1][1],dp[i][1]+2);
					dp[i+1][0]=min(dp[i+1][0],dp[i][1]+2);
				}else if(a+b==0)dp[i+1][1]=min(dp[i+1][1],dp[i][1]);
				else if(a){
					dp[i+1][1]=min(dp[i+1][1],dp[i][1]+1);
					dp[i+1][0]=min(dp[i+1][0],dp[i][1]+1);
				}else if(b)dp[i+1][1]=min(dp[i+1][1],dp[i][1]+1);
			}
		}
		cout<<min(dp[n][0],dp[n][1]+1)+n<<endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1552
final_rank: 17

code_snippet: #include <iostream>
using namespace std;
int mp[2][20010],n;

int solve(){
  int x=0,y=0,cost=0,f=1;
  while(x!=n+1) {
    if(f&&mp[0][x])f=0;
    if(f&&mp[1][x])f=0,cost++;

    if(mp[1][x])y=1,mp[1][x]=0;
    else if(mp[0][x])y=mp[0][x]=0;
    else x++;
    cost++;
  }
  if(y==1)cost++;
  return cost-1;
}

int main(){
  int q;
  cin>> q;
  while(q--){
    cin>>n;
    char a;
    for(int i=0;i<2;i++)
      for(int j=0;j<2*n;j++)cin>> a,mp[i][(j+1)/2] |= a/'Y';
    cout<<solve()<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1240
final_rank: 2

code_snippet: #include <iostream>
#include <cstring>
using namespace std;
int mp[2][20010];

int solve(int goal){
  int x=0,y=0,cost=0,f=1;
  while(x!=goal+1) {
    if(f&&mp[0][x])f=0;
    if(f&&mp[1][x])f=0,cost++;

    if(mp[1][x])y=1,mp[1][x]=0;
    else if(mp[0][x])y=mp[0][x]=0;
    else x++;
    cost++;
  }
  if(y==1)cost++;
  return cost-1;
}

int main(){
  int q;
  cin>> q;
  while(q--){
    int n;
    cin>>n;
    memset(mp,0,sizeof(mp));

    char a;
    for(int i=0;i<2;i++)
      for(int j=0;j<2*n;j++)cin>> a,mp[i][(j+1)/2] |= a/'Y';
    cout<<solve(n)<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1324
final_rank: 6

code_snippet: #include <iostream>
#include <cstring>
using namespace std;
int mp[2][20010];

int solve(int goal){
  int x=0,y=0,cost=0,f=1;
  while(x!=goal+1) {
    if(f&&mp[0][x])f=0;
    if(f&&mp[1][x])f=0,cost++;

    if(mp[1][x])y=1,mp[1][x]=0;
    else if(mp[0][x])y=mp[0][x]=0;
    else x++;
    cost++;
  }
  if(y==1)cost++;
  return cost-1;
}

int main(){
  int q;
  cin>> q;
  while(q--){
    int n,pos;
    cin>>n;
    memset(mp,0,sizeof(mp));

    for(int i=0;i<2;i++){
      pos=0;
      for(int j=0,cnt=1;j<2*n;j++){
	char a;
	cin>> a;
	mp[i][pos] |= a/'Y';
	cnt++;
	if(cnt%2==0)pos++;
      }
    }
    cout<<solve(pos)<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1324
final_rank: 6

code_snippet: #include <iostream>
#include <cstring>
using namespace std;
int mp[2][20010];

int solve(int goal){
  int x=0,f=1,cost=0,y=0;
  while(x!=goal+1) {
    if(f&&mp[0][x])f=0,cost++,y=mp[0][x]=0;
    else if(f&&mp[1][x])cost++,f=y=0;
    else if(mp[1][x]) cost++,y=1,mp[1][x]=0;
    else if(mp[0][x]) cost++,y=mp[0][x]=0;
    else cost++,x++;
  }
  if(y==1)cost++;
  return cost-1;
}

int main(){
  int q;
  cin>> q;
  while(q--){
    int n,pos,cnt;
    cin>>n;
    memset(mp,0,sizeof(mp));

    for(int i=0;i<2;i++){
      pos=0,cnt=1;
      char a;
      for(int j=0;j<2*n;j++){
	cin>> a;
	if(a=='Y') a=1;
	else a=0;
	mp[i][pos] |= a;
	cnt++;
	if(cnt==2)pos++,cnt=0;
      }
    }
    cout<<solve(pos)<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1324
final_rank: 6

code_snippet: #include <iostream>
#include <cstring>
using namespace std;
int mp[2][20010];
int n;

int solve(int goal){
  int x=0,f=1,cost=0,y=0;
  while(x!=goal+1) {
    if(f&&mp[0][x])f=0,cost++,y=mp[0][x]=0;
    else if(f&&mp[1][x])cost++,f=y=0;
    else if(mp[1][x]) cost++,y=1,mp[1][x]=0;
    else if(mp[0][x]) cost++,y=mp[0][x]=0;
    else cost++,x++;
  }
  if(y==1)cost++;
  return cost-1;
}

int main(){
  int q;
  cin>> q;
  while(q--){
    cin>>n;
    memset(mp,0,sizeof(mp));
    int pos,cnt;
    for(int i=0;i<2;i++){
      pos=0,cnt=1;
      char a;
      for(int j=0;j<2*n;j++){
	cin>> a;
	if(a=='Y') a=1;
	else a=0;
	mp[i][pos] |= a;
	cnt++;
	if(cnt==2)pos++,cnt=0;
      }
    }
    //     for(int i=0;i<2;i++){for(int j=0;j<=pos;j++) cout << mp[i][j]<<" ";cout <<endl;}
    cout<<solve(pos)<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1324
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
 
int main() {
  int T;
  cin >> T;
  while(T--) {
    int n;
    string s;
    cin >> n >> s;
    bool a[2][n+1];
    memset(a,0,sizeof(a));
    for(int i=0; i<4*n; i++) {
      if(i<2*n) a[0][(i+1)/2]|=s[i]=='Y';
      else a[1][(i+1-2*n)/2]|=s[i]=='Y';
    }
    int x=1<<29,y=0;
    for(int i=0; i<2; i++) {
      for(int j=0; j<n+1; j++) {
        if(a[i][j]) {
          x=min(x,j);
          y=max(y,j);
        }
      }
    }
    int ans=n;
    for(int j=x+1; j<y; j++) {
      for(int i=0; i<2; i++) {
        if(a[i][j]) ans++;
      }
    }
    if(a[1][x]) ans+=2;
    else if(a[0][x]) ans++;
    if(a[1][y]) ans+=2;
    else if(a[0][y]) ans++;
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1332
final_rank: 12

code_snippet: #include<iostream>
using namespace std;
int main(){
  int t;
  cin>>t;
  while(t--){
    int n,e=-1,E=-1,be=1e9,BE=1e9;
    int f=0;
    cin>>n;
    int an=n;
    for(int i=0;i<2;i++){
      char a,b='N';
      for(int j=0;j<n;j++){
	cin>>a;
	if(a=='Y'||b=='Y'){
	  an++;
	  if(i==1)f=2,E=j,BE=min(BE,j);
	  else e=j,be=min(be,j);
	}
	cin>>b;
      }
	if(b=='Y'){
	  an++;
	  if(i==1)f=2,E=n,BE=min(BE,n);
	  else e=n,be=min(be,n);
	}
      b='N';
    }
    if(e>=E&&f>0)f--;
    if(be<=BE&&f>0)f--;
    cout<<an+f<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1160
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
  int T;
  cin >> T;
  while(T--) {
    int n;
    string s;
    cin >> n >> s;
    bool a[2][n+1];
    memset(a,0,sizeof(a));
    for(int i=0; i<4*n; i++) {
      if(i<2*n) a[0][(i+1)/2]|=s[i]=='Y';
      else a[1][(i+1-2*n)/2]|=s[i]=='Y';
    }
    int x=1<<29,y=0;
    for(int i=0; i<2; i++) {
      for(int j=0; j<n+1; j++) {
        if(a[i][j]) {
          x=min(x,j);
          y=max(y,j);
        }
      }
    }
    int ans=n;
    for(int j=x+1; j<y; j++) {
      for(int i=0; i<2; i++) {
        if(a[i][j]) ans++;
      }
    }
    if(a[1][x]) ans+=2;
    else if(a[0][x]) ans++;
    if(a[1][y]) ans+=2;
    else if(a[0][y]) ans++;
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1332
final_rank: 12

code_snippet: #include<iostream>
#include <string>
#include <string.h>
using namespace std;

int main(){

	int T = 0;
	int N = 0;
	string str;
	int result = 0;

	bool flag = false;
	int pivot = 0;

	cin >> T;

	for (int i = 0; i<T; i++){
		//?????????
		N = 0, str = "",flag=false,pivot=0,result=0;

		//????????????
		cin >> N;
		cin >> str;

		for (pivot = 0; pivot < (2 * N); pivot++){
			//???????????????
			if (pivot == 0){
				if (str[pivot + 2 * N] == 'Y'){
					result = result + 2;
					flag = true;
				}
				else if (str[pivot] == 'Y'){
					result++;
					flag = true;
				}
			}//?????????????????????
			else if (pivot == (2 * N) - 1){
				if (flag == false){
					if (str[pivot + 2 * N] == 'Y'){
						result = result + 2;
						flag = true;
					}
					else if (str[pivot] == 'Y'){
						result++;
						flag = true;
					}
					else{
						result--;
					}
				}
				else{
					if (str[pivot + 2 * N] == 'Y'){
						result++;
					}
					else if (str[pivot] != 'Y'){
						result--;
					}
				}
			}//?????????
			else{
				if (flag == false){
					if (str[pivot + 2 * N] == 'Y' || str[pivot + 2 * N + 1] == 'Y'){
						result = result + 2;
						flag = true;
					}
					else if (str[pivot] == 'Y' || str[pivot + 1] == 'Y'){
						result++;
						flag = true;
					}
				}
				else{
					if (str[pivot] == 'Y' || str[pivot + 1] == 'Y'){
						result++;
					}
					if (str[pivot + 2 * N] == 'Y' || str[pivot + 2 * N + 1] == 'Y'){
						result++;
					}
				}
			}
			result++;
			if(pivot!=0)pivot++;

		}

		cout << result << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1308
final_rank: 4

code_snippet: #include <iostream>
#include <string>
#include <vector>
using namespace std;


int main()
{
   int T;
   cin>>T;
   while(T--)
   {
       int N;
       cin>>N;
       string s;
       cin>>s;
       vector<bool> done(s.size(),false);
       vector<vector<int> > dp(3,vector<int>(N+2,1000000));
       dp[0][0]=0;
       for(int c=0;c<=N;c++)
       {
           bool b1 = (c != 0 && s[c*2-1]=='Y') || (c != N && s[c*2] == 'Y');
           bool b2 = (c != 0 && s[c*2-1+2*N]=='Y') || (c != N && s[c*2+2*N] == 'Y');
           if(b1 && b2)
           {
               dp[0][c+1] = min(dp[0][c]+4,min(dp[1][c]+3,dp[2][c]+3));
               dp[1][c+1] = min(dp[0][c]+3,min(dp[2][c]+3,dp[1][c]+4));
               dp[2][c+1] = min(dp[0][c]+3,min(dp[2][c]+4,dp[1][c]+3));

           }
           else if(!b1 && b2)
           {
                dp[0][c+1] = min(dp[0][c]+4,min(dp[1][c]+3,dp[2][c]+3));
                dp[1][c+1] = min(dp[0][c]+3,min(dp[1][c]+2,dp[2][c]+2));
                dp[2][c+1] = min(dp[0][c]+3,min(dp[1][c]+2,dp[2][c]+2));
           }
           else if(b1 && !b2)
           {
                dp[0][c+1] = min(dp[0][c]+2,min(dp[1][c]+2,dp[2][c]+3));
                dp[1][c+1] = min(dp[0][c]+2,min(dp[1][c]+2,dp[2][c]+3));
                dp[2][c+1] = min(dp[0][c]+3,min(dp[1][c]+3,dp[2][c]+4));
           }
           else if(!b1 && !b2)
           {
                dp[0][c+1] = min(dp[0][c]+1,min(dp[1][c]+2,dp[2][c]+3));
                dp[1][c+1] = min(dp[0][c]+2,min(dp[1][c]+1,dp[2][c]+2));
                dp[2][c+1] = min(dp[0][c]+3,min(dp[1][c]+2,dp[2][c]+1));
           }
       }
       cout<<dp[0][N+1]-1<<endl;

   }
}
accuracy: 1
time_ms: 10
memory_kb: 1452
final_rank: 16

code_snippet: #include <iostream>
#include <cstring>
using namespace std;

bool put[10001][2];
int dp[10001][3];

int min(int a, int b, int c) {
    return min(a, min(b, c));
}

int main() {
    int T; cin >> T;
    while (T--) {
        int N; cin >> N;
        memset(put, 0, sizeof put);
        char c;
        for (int k=1; k<=4*N; ++k) {
            cin >> c;
            if (c == 'Y') {
                if (k <= 2*N) {
                    put[k/2][0] = true;
                } else {
                    put[(k-2*N)/2][1] = true;
                }
            }
        }
        memset(dp, 0, sizeof dp);
        if (put[0][0] && put[0][1]) {
            dp[0][0] = 3;
            dp[0][1] = 2;
            dp[0][2] = 2;
        } else if (put[0][0] && ! put[0][1]) {
            dp[0][0] = 1;
            dp[0][1] = 1;
            dp[0][2] = 2;
        } else if ( ! put[0][0] && put[0][1]) {
            dp[0][0] = 3;
            dp[0][1] = 2;
            dp[0][2] = 2;
        } else if ( ! put[0][0] && ! put[0][1]) {
            dp[0][0] = 0;
            dp[0][1] = 1;
            dp[0][2] = 2;
        }
        for (int k=1; k<=N; ++k) {
            if (put[k][0] && put[k][1]) {
                dp[k][0] = min(dp[k-1][0] + 4, dp[k-1][1] + 3, dp[k-1][2] + 3);
                dp[k][1] = min(dp[k-1][0] + 3, dp[k-1][1] + 3, dp[k-1][2] + 3);
                dp[k][2] = min(dp[k-1][0] + 3, dp[k-1][1] + 3, dp[k-1][2] + 4);
            } else if (put[k][0] && ! put[k][1]) {
                dp[k][0] = min(dp[k-1][0] + 2, dp[k-1][1] + 2, dp[k-1][2] + 3);
                dp[k][1] = min(dp[k-1][0] + 2, dp[k-1][1] + 2, dp[k-1][2] + 3);
                dp[k][2] = min(dp[k-1][0] + 3, dp[k-1][1] + 3, dp[k-1][2] + 4);
            } else if ( ! put[k][0] && put[k][1]) {
                dp[k][0] = min(dp[k-1][0] + 4, dp[k-1][1] + 3, dp[k-1][2] + 3);
                dp[k][1] = min(dp[k-1][0] + 3, dp[k-1][1] + 2, dp[k-1][2] + 2);
                dp[k][2] = min(dp[k-1][0] + 3, dp[k-1][1] + 2, dp[k-1][2] + 2);
            } else if ( ! put[k][0] && ! put[k][1]) {
                dp[k][0] = min(dp[k-1][0] + 1, dp[k-1][1] + 2, dp[k-1][2] + 3);
                dp[k][1] = min(dp[k-1][0] + 2, dp[k-1][1] + 1, dp[k-1][2] + 2);
                dp[k][2] = min(dp[k-1][0] + 3, dp[k-1][1] + 2, dp[k-1][2] + 1);
            }
        }
        cout << dp[N][0] << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1304
final_rank: 3

code_snippet: #define _USE_MATH_DEFINES
#define INF 100000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cstdio>

using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
 
static const double EPS = 1e-8;
 
const int tx[] = {+0,+0,+1};
const int ty[] = {-1,+1,+0};

int nodes[5][20001];
int dp[1<<5][20001][5]; //dp[book_state][x][y];

int compute_book_state(int x){
  int res = 0;
  for(int y = 1,i=0; y < 5; y+=2,i++){
    if(nodes[y][x] == 1){
      res |= (1<<y);
    }
  }
  return res;
}

int shelves_per_line;

struct State{
  int x;
  int y;
  int cost;
  int book_state;
  State(int x,int y,int cost,int book_state) :
    x(x),y(y),cost(cost),book_state(book_state) {}
  bool operator<(const State& s) const {
    return cost < s.cost;
  }
  bool operator>(const State& s) const {
    return cost > s.cost;
  }
};

void bfs(){
  
  priority_queue<State,vector<State>,greater<State> > que;
  que.push(State(0,0,0,compute_book_state(0)));
           
  while(!que.empty()){
    State s = que.top();
    que.pop();
    int sx = s.x;
    int sy = s.y;
    int cost = s.cost;
    for(int i=0;i<3;i++){
      int dx = sx + tx[i];
      int dy = sy + ty[i];
      if(dy < 0 || dy >= 5 || dx > shelves_per_line) continue;
      
      if(i < 2){
        if(s.book_state & (1<<dy)){
          int next_book_state = (s.book_state & ~(1<<dy));
          if(dp[next_book_state][dx][dy] > cost + abs(sy - dy)){
            dp[next_book_state][dx][dy] = cost + abs(sy - dy);
            que.push(State(dx,dy,cost + abs(sy - dy),next_book_state));
          }
        }
        else{
          if(dp[s.book_state][dx][dy] > cost + abs(sy - dy)){
            dp[s.book_state][dx][dy] = cost + abs(sy - dy);
            que.push(State(dx,dy,cost + abs(sy - dy),s.book_state));
          }
        }
      }
      
      else{
        if(sy % 2 == 1) continue;
        if(s.book_state != 0) continue;
        
        int next_book_state = compute_book_state(dx);
        if(dp[next_book_state][dx][dy] > cost + 2){
          dp[next_book_state][dx][dy] = cost + 2;
          que.push(State(dx,dy,cost + 2,next_book_state));
        }
      }
    }
  }
}

int main(){
  int num_of_test_cases;
  while(~scanf("%d",&num_of_test_cases)){
    for(int case_i = 0; case_i < num_of_test_cases; case_i++){
      scanf("%d",&shelves_per_line);
      string shelves;
      cin >> shelves;

      memset(nodes,0,sizeof(nodes));
      for(int y = 1,i=0; y <= 4; y+=2,i++){
        for(int x = 0; x <= shelves_per_line; x++){
          if(x == 0){
            if(shelves[(i * shelves_per_line * 2)] == 'Y'){
              nodes[y][x] = 1;
            }
          }
          else if(x == shelves_per_line){
            if(shelves[(i * shelves_per_line * 2) + (shelves_per_line * 2 - 1)] == 'Y'){
              nodes[y][x] = 1;
            }
          }
          else{
            if(shelves[(i * shelves_per_line * 2) + 2 * x - 1] == 'Y' 
               || shelves[(i * shelves_per_line * 2) + 2 * x] == 'Y'){
              nodes[y][x] = 1;
            }
          }
        }
      }

      memset(dp,0x3f,sizeof(dp));
      int book_state = compute_book_state(0);
      dp[book_state][0][0] = 0;
      bfs();

      printf("%d\n",dp[0][shelves_per_line][0]/2);
    }
  }
}
accuracy: 1
time_ms: 30
memory_kb: 14232
final_rank: 20

code_snippet: #define _USE_MATH_DEFINES
#define INF 100000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cstdio>

using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
 
static const double EPS = 1e-8;
 
const int tx[] = {+0,+0,+1};
const int ty[] = {-1,+1,+0};

int nodes[5][20001];
int dp[1<<5][20001][5]; //dp[book_state][x][y];

int compute_book_state(int x){
  int res = 0;
  for(int y = 1,i=0; y < 5; y+=2,i++){
    if(nodes[y][x] == 1){
      res |= (1<<y);
    }
  }
  return res;
}

int shelves_per_line;

struct State{
  int x;
  int y;
  int cost;
  int book_state;
  State(int x,int y,int cost,int book_state) :
    x(x),y(y),cost(cost),book_state(book_state) {}
  bool operator<(const State& s) const {
    return cost < s.cost;
  }
  bool operator>(const State& s) const {
    return cost > s.cost;
  }
};

int bfs(){
  
  priority_queue<State,vector<State>,greater<State> > que;
  que.push(State(0,0,0,compute_book_state(0)));
           
  while(!que.empty()){
    State s = que.top();
    que.pop();
    int sx = s.x;
    int sy = s.y;
    int cost = s.cost;
    for(int i=0;i<3;i++){
      int dx = sx + tx[i];
      int dy = sy + ty[i];
      if(dy < 0 || dy >= 5 || dx > shelves_per_line) continue;
      
      if(i < 2){
        if(s.book_state & (1<<dy)){
          int next_book_state = (s.book_state & ~(1<<dy));
          if(dp[next_book_state][dx][dy] > cost + abs(sy - dy)){
            dp[next_book_state][dx][dy] = cost + abs(sy - dy);
            que.push(State(dx,dy,cost + abs(sy - dy),next_book_state));
          }
        }
        else{
          if(dp[s.book_state][dx][dy] > cost + abs(sy - dy)){
            dp[s.book_state][dx][dy] = cost + abs(sy - dy);
            que.push(State(dx,dy,cost + abs(sy - dy),s.book_state));
          }
        }
      }
      
      else{
        if(sy % 2 == 1) continue;
        if(s.book_state != 0) continue;
        
        int next_book_state = compute_book_state(dx);
        if(dp[next_book_state][dx][dy] > cost + 2){
          dp[next_book_state][dx][dy] = cost + 2;
          que.push(State(dx,dy,cost + 2,next_book_state));
        }
      }
    }
  }
}

int main(){
  int num_of_test_cases;
  while(~scanf("%d",&num_of_test_cases)){
    for(int case_i = 0; case_i < num_of_test_cases; case_i++){
      scanf("%d",&shelves_per_line);
      string shelves;
      cin >> shelves;

      memset(nodes,0,sizeof(nodes));
      for(int y = 1,i=0; y <= 4; y+=2,i++){
        for(int x = 0; x <= shelves_per_line; x++){
          if(x == 0){
            if(shelves[(i * shelves_per_line * 2)] == 'Y'){
              nodes[y][x] = 1;
            }
          }
          else if(x == shelves_per_line){
            if(shelves[(i * shelves_per_line * 2) + (shelves_per_line * 2 - 1)] == 'Y'){
              nodes[y][x] = 1;
            }
          }
          else{
            if(shelves[(i * shelves_per_line * 2) + 2 * x - 1] == 'Y' 
               || shelves[(i * shelves_per_line * 2) + 2 * x] == 'Y'){
              nodes[y][x] = 1;
            }
          }
        }
      }

      memset(dp,0x3f,sizeof(dp));
      int book_state = compute_book_state(0);
      dp[book_state][0][0] = 0;
      bfs();

      printf("%d\n",dp[0][shelves_per_line][0]/2);
    }
  }
}
accuracy: 1
time_ms: 20
memory_kb: 14232
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

class range {
private:
  struct Iterator {
    int val;
    int operator*() {return val;}
    bool operator!=(Iterator& itr) {return val < itr.val;}
    void operator++() {++val;}
  };
  Iterator i, n;
public:
  range(int n) : i({0}), n({n}) {}
  range(int i, int n) : i({i}), n({n}) {}
  Iterator& begin() {return i;}
  Iterator& end() {return n;}
};

template<class T> inline T at(const vector<T> &v, int i) {return v[(i % (int)v.size() + v.size()) % v.size()];}

template<class T> inline bool is_max(T &a, const T &b) {return a < b ? a = b, true : false;}
template<class T> inline bool is_min(T &a, const T &b) {return a > b ? a = b, true : false;}

int main() {
  int t;
  cin >> t;
  for (int _ : range(t)) {
    int n;
    string s, s1, s2;
    cin >> n >> s;
    s1 = "N" + s.substr(0, n * 2) + "N";
    s2 = "N" + s.substr(n * 2) + "N";
    n = n + 1;
    int res = n + 1;
    for (int i : range(n)) {
      if (s1[2 * i] == 'Y' || s1[2 * i + 1] == 'Y') ++res;
      if (s2[2 * i] == 'Y' || s2[2 * i + 1] == 'Y') ++res;
    }
    for (int i : range(n)) {
      if (s1[2 * i] == 'Y' || s1[2 * i + 1] == 'Y') {
        --res;
        break;
      }
      if (s2[2 * i] == 'Y' || s2[2 * i + 1] == 'Y') break;
    }
    reverse(s1.begin(), s1.end());
    reverse(s2.begin(), s2.end());
    for (int i : range(n)) {
      if (s1[2 * i] == 'Y' || s1[2 * i + 1] == 'Y') {
        --res;
        break;
      }
      if (s2[2 * i] == 'Y' || s2[2 * i + 1] == 'Y') break;
    }
    cout << res << endl;
  }
}
accuracy: 1
time_ms: 10
memory_kb: 1328
final_rank: 11

code_snippet: #include <iostream>
using namespace std;

const int MAX_N = 10000;

int main(){
    int m, n, i, j;
    cin >> m;
    while(m--){
        cin >> n;
        string t;
        cin >> t;
        int shelf[2][MAX_N+1];
        for(i = 0; i < 2*n+2; i++){
            shelf[i/(n+1)][i%(n+1)] = 0;
        }
        int minc[2]={MAX_N+3,MAX_N+3}, maxc[2]={-1,-1}, count=0;
        for(i = 0; i < 4*n; i++){
            int r = i/2/n, c = (i%(2*n)+1)/2;
            if(t[i]=='Y'){
                shelf[r][c] = 1;
                if(minc[r] > MAX_N) minc[r] = c;
                maxc[r] = c;
            }
        }
        for(i = 0; i < 2; i++){
            for(j = 0; j < n+1; j++){
                count += shelf[i][j];
            }
        }
        count += n;
        if(minc[0] > minc[1]) count++;
        if(maxc[0] < maxc[1]) count++;
        cout << count << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1380
final_rank: 14

code_snippet: #include<iostream>
#include<string>
#include<cstring>
using namespace std;
#define INF (1<<29)


int g[10001];
int dp[10001][2];


int main(){
	int t,n;
	string s;
	cin>>t;
	while(t--){
		cin>>n>>s;
		memset(g,0,sizeof(g));
		if(s[0]=='Y')g[0]|=1;
		for(int i=1;i<2*n-1;i+=2){
			if(s[i]=='Y'||s[i+1]=='Y')g[1+i/2]|=1;
		}
		if(s[2*n-1]=='Y')g[n]|=1;
		
		if(s[2*n]=='Y')g[0]|=2;
		for(int i=2*n+1;i<4*n-1;i+=2){
			if(s[i]=='Y'||s[i+1]=='Y')g[1+(i-2*n)/2]|=2;
		}
		if(s[4*n-1]=='Y')g[n]|=2;

		if(g[0]&2){
			dp[0][0]=3;
			dp[0][1]=2;
		}else if(g[0]&1){
			dp[0][0]=1;
			dp[0][1]=1;
		}else{
			dp[0][0]=0;
			dp[0][1]=1;
		}
		for(int i=0;i<n;i++){
			if(g[i+1]==3){
				dp[i+1][0]=min(dp[i][0]+4,dp[i][1]+3);
				dp[i+1][1]=min(dp[i][0]+3,dp[i][1]+3);
			}else if(g[i+1]==1){
				dp[i+1][0]=min(dp[i][0]+2,dp[i][1]+2);
				dp[i+1][1]=min(dp[i][0]+2,dp[i][1]+2);
			}else if(g[i+1]==2){
				dp[i+1][0]=min(dp[i][0]+4,dp[i][1]+3);
				dp[i+1][1]=min(dp[i][0]+3,dp[i][1]+2);
			}else{
				dp[i+1][0]=min(dp[i][0]+1,dp[i][1]+2);
				dp[i+1][1]=min(dp[i][0]+2,dp[i][1]+1);
			}
		}
		cout<<dp[n][0]<<endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1432
final_rank: 15

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
using namespace std;
int main(){
  int q;
  cin >> q;
  for(int loop = 0;loop < q;loop++){
	int n;
	string str;
	cin >> n;
	cin >> str;
	vector<vector<bool> > field(2,vector<bool>(n+1,false));
	for(int i = 0;i < 2*n;i++){
	  if(str[i] == 'Y'){
		field[0][(i+1)/2] = true;
	  }
	  if(str[i+2*n] == 'Y'){
		field[1][(i+1)/2] = true;
	  }
	}

	int ans = n;
	bool first = true;
	int b_s = 0;
	for(int i = 0;i < n+1;i++){
	  if(first){
		if(field[1][i]){
		  first = false;
		  ans += 2;
		  b_s = 1;
		}else if(field[0][i]){
		  first = false;
		  ans++;
		}
	  }else{
		if(field[0][i] && field[1][i]){
		  ans+=2;
		  b_s = 0;
		}else if(field[0][i]){
		  ans++;
		  b_s = 0;
		}else if(field[1][i]){
		  ans++;
		  b_s = 1;
		}
	  }
	}
	
	if(b_s == 1)ans++;
	cout << ans << endl;
	
	
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1324
final_rank: 6

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int libgirl();
int girl(int x, int y, int point);

int main(){
	int times;
	int i;
	cin >> times;
	for(i = 0; i < times; i++){
		cout << libgirl() << endl;
	}
}

int libgirl(){
	string shelve_input;
	int N, i;
	vector<vector<int>> shelve;
	int upper = 0;
	int downer = 0;
	int start = 1;
	int end = 1;
	int walk = 0;
	
	cin >> N;
	cin >> shelve_input;
	for(i = 0; i < 2*N; i++){
		shelve.push_back({0,0});
	}
	for(i = 0; i < 2*N; i++){
		if(shelve_input[i] == 'Y'){
			shelve[i][0] = 1;
			upper++;
		}else{
			shelve[i][0] = 0;
		}
	}
	for(i = 2*N; i < 4*N; i++){
		if(shelve_input[i] == 'Y'){
			shelve[i-2*N][1] = 1;
			downer++;
		}else{
			shelve[i-2*N][1] = 0;
		}
	}
	for(i = 0; i < 2*N; i++){
		if(shelve[i][0] == 1){
			start = 0;
			break;
		}else if(shelve[i][1] == 1){
			break;
		}
	}
	for(i = 2*N-1; i >= 0; i--){
		if(shelve[i][0] == 1){
			end = 0;
			break;
		}else if(shelve[i][1] == 1){
			break;
		}
	}
	for(i = 2; i < 2*N; i = i + 2){
		if(shelve[i][0] == 1 && shelve[i-1][0] == 1){
			upper--;
		}
		if(shelve[i][1] == 1 && shelve[i-1][1] == 1){
			downer--;
		}
	}
	walk = upper + downer + N + start + end;
	return walk;
}
accuracy: 1
time_ms: 10
memory_kb: 2572
final_rank: 18

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define REV(v) rbegin(v), rend(v)
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second
#define ok(x) (0 <= x && x < s.size() && s[x] == 'Y')

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

struct info{
	int left, right, cnt;
};

int N;
info check(string s){
	info ret;
	ret.left = 2 * N;
	ret.right = 0;
	ret.cnt = 0;
	rep(i, N + 1){
		if (ok(i * 2 - 1) || ok(i * 2)){
			ret.left = min(ret.left, i);
			ret.right = max(ret.right, i);
			++ret.cnt;
		}
	}
	return ret;
}

int main(){
	int T;
	cin >> T;
	while (T--){
		cin >> N;
		string s;
		cin >> s;
		info up, down;
		up = check(s.substr(0, 2 * N));
		down = check(s.substr(2 * N));

		int total = up.cnt + down.cnt;

		int ans = N;
		if (total){
			ans = N + 2;
			ans += total;
			if (up.left <= down.left) --ans;
			if (up.right >= down.right) --ans;
		}
		cout << ans << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1316
final_rank: 5