codeforces 120B Quiz League

来源:互联网 发布:剑豪生死斗 知乎 编辑:程序博客网 时间:2024/05/17 01:10

题目链接:传送门。

A team quiz game called "What? Where? When?" is very popular in Berland. The game is centered on two teams competing. They are the team of six Experts versus the team of the Audience. A person from the audience asks a question and the experts are allowed a minute on brainstorming and finding the right answer to the question. All it takes to answer a typical question is general knowledge and common logic. The question sent be the audience are in envelops lain out in a circle on a round table. Each envelop is marked by the name of the asker's town. Each question is positioned in a separate sector. In the centre of the table is a spinning arrow. Thus, the table rather resembles a roulette table with no ball but with a spinning arrow instead. The host sets off the spinning arrow to choose a question for the experts: when the arrow stops spinning, the question it is pointing at is chosen. If the arrow points at the question that has already been asked, the host chooses the next unanswered question in the clockwise direction. Your task is to determine which will be the number of the next asked question if the arrow points at sector number k.

Input

The first line contains two positive integers n and k (1 ≤ n ≤ 1000 and 1 ≤ k ≤ n) — the numbers of sectors on the table and the number of the sector where the arrow is pointing. The second line contains n numbers: ai = 0 if the question from sector i has already been asked and ai = 1 if the question from sector i hasn't been asked yet (1 ≤ i ≤ n). The sectors are given in the clockwise order, the first sector follows after the n-th one.

Output

Print the single number — the number of the sector containing the question the experts will be asked. It is guaranteed that the answer exists, that is that not all the questions have already been asked.

Examples
Input

5 50 1 0 1 0

Output

2

Input

2 11 1

Output

1

【题意】

n个人要来回答问题,0表示已经回答过这个问题,1表示没有回答过问题,现在有一个问题要交给一个人回答,如果这个人没有回答过问题就是这个人回答,否则就让他后面第一个没回答问题的人来回答,其中她们是环绕着坐的,所以第n个人后面是第一个人。问你这个问题最后是谁来回答。

【分析】

简单的模拟就行,当然有一个更快的办法就是直接找出所有没有回答问题的人,然后二分一下就行了。

【代码】

#include<bits/stdc++.h>using namespace std;int num[1234];int main(){    freopen("input.txt","r",stdin);    freopen("output.txt","w",stdout);    int len,que,a,flag = 0;    scanf("%d%d",&len,&que);    for(int i = 1;i<=len;i++)    {        scanf("%d",&a);        if(a) num[flag++] = i;    }    num[flag++] = len+num[0];    int ans = lower_bound(num,num+flag,que)-num;    printf("%d\n",num[ans]>len?num[ans]-len:num[ans]);    return 0;}