2018年上海金马五校程序设计竞赛 Problem C : Summer Camp

Problem C : Summer Camp


Description

DengDalao is a third-year student. He hopes to participate in summer camps to gain opportunities for pursuing a master's degree. Now, he wants to know the cheapest way to come to the summer camp he dreams of.

There are n cities, numbered 1, 2, 3, ..., n. DengDalao lives in the city a, and the summer camp will be held in the city b. DengDalao's home and the summer camp may be in the same city.

DengDalao can and only can take a flight to travel between cities, and transfer flights in any cities for any times during the whole journey. But he must start his journey in the city a and end in the city b. The airport of each city belongs to one of the two companies. If the airports of cities i and j belong to different companies, the cost of flights between them is |i - j|, otherwise the cost is zero.

Please help DengDalao calculate the minimum fee he needs to pay for the flights.

Input

There are several test cases (no more than 100). Each test case contains two lines.

The first line contains three integers na and b (1 ≤ n ≤ 100,000, 1 ≤ ab ≤ n).

The second line contains a string with length n, which consists of only characters '0' and '1'. If the i-th character is '0', then the airport of the city i belongs to the first company, otherwise it belongs to the the second one.

Output

For each test case, print one line with the minimum fee DengDalao needs to pay for the flights.

Sample Input

5 1 5
10010
4 4 2
1010

 

Sample Output

 

1
0



由题意可知起点到终点所需支付不是1就是0

如果起点和终点的航空公司一样就直接输出0,不然就找离终点最近的那个和起点是相同公司的城市

代码如下:
#include<iostream> 
#include<cstdio>
#include<cmath>  
using namespace std ;  
int main(){
 int n,a,b;
 while(~scanf("%d%d%d",&n,&a,&b)){
  string q;
  cin>>q;
  if(q[a-1]==q[b-1]){
   cout<<0<<endl;
  }
  else{
   int num=fabs(a-b);
   for(int i=0;i<n;i++){
    if(fabs(i-b+1)<num&&q[i]==q[b-1]) num=fabs(i-b);
    
   }
   cout<<num<<endl;
  }
 }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值