1101: Travel
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 22
[ Submit][ Status][ Web Board]
Description
One day, Doraemon have words with his friend Nobi and decided to take a travel. He found that there are n locations label from 1-n in a line. Each location have a machine that can get person to anywhere he like. These machines are powered by two companies. The bosses of the companies are jealous about each other and decide that you can travel to the machine that owned by himself for free but have to pay abs(st-en) if the endpoint is belong to the other’s company. Now Doraemon is in the location a and he wants you to calculate the minimum cost for him to go to location b.
Input
In every input, the first line contains a single integer n(1<=n<=10000), the second line contains a string s contains whose length is n and contains only 0 and 1. If s[i]=0, then machine i is belong to the first company, otherwise it belongs to the second company. The third line contains two integers a and b, indicates the start point and end point of Doraemon.
Output
A single line, indicates the minimum cost of Doraemon.
Sample Input
1
5
10101
1 2
Sample Output
1
HINT
Source
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int t=0;
int n=0;
int i=0;
int fee=0;
int st,en;
char a[10000];
cin>>t;
while(t--){
fee=10000;
cin>>n;
cin>>a;
cin>>st>>en;
//cout<<a<<endl;
if(a[st-1]==a[en-1])cout<<0<<endl;
else{
if(st<en){
for(i=st-1;i<en;i++)
if(a[i]!=a[st-1]){
fee=i-st+1;
//cout<<fee<<endl;
break;
}
for(i=st-1;i>=0;i--)
if(a[i]!=a[st-1]){
if(st-1-i<fee)
fee=st-1-i;
//cout<<fee<<endl;
break;
}
}
else{
st^=en;
en^=st;
st^=en;
for(i=en-1;i>=st-1;i--)
if(a[i]!=a[en-1]){
if(en-1-i<fee)
fee=en-1-i;
//cout<<fee<<endl;
break;
}
for(i=en-1;i<n;i++)
if(a[i]!=a[en-1]){
if(i-en+1<fee)
fee=i-en+1;
//cout<<fee<<endl;
break;
}
}
cout<<fee<<endl;
}
}
}