第K個數

In late autumn evening n robots gathered in the cheerful company of friends. Each robot has a unique identifier — an integer from 1 to 109.

At some moment, robots decided to play the game “Snowball”. Below there are the rules of this game. First, all robots stand in a row. Then the first robot says his identifier. After that the second robot says the identifier of the first robot and then says his own identifier. Then the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own. This process continues from left to right until the n-th robot says his identifier.

Your task is to determine the k-th identifier to be pronounced.

Input
The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(2·109, n·(n + 1) / 2).

The second line contains the sequence id1, id2, …, idn (1 ≤ idi ≤ 109) — identifiers of roborts. It is guaranteed that all identifiers are different.

Output
Print the k-th pronounced identifier (assume that the numeration starts from 1).

Examples
Input
2 2
1 2
Output
1
Input
4 5
10 4 18 3
Output
4
Note
In the first sample identifiers of robots will be pronounced in the following order: 1, 1, 2. As k = 2, the answer equals to 1.

In the second test case identifiers of robots will be pronounced in the following order: 10, 10, 4, 10, 4, 18, 10, 4, 18, 3. As k = 5, the answer equals to 4.
思路:剛開始想把所有的數字存放在一個二維數組中,但是會超時,然後又用K減去每行的個數
比如例二
10
10 4
10 4 18
10 4 18 3
k=5,i=1時,k>i,則k-=i,直到k<=i

#include<bits/stdc++.h>
using namespace std;
const int maxx=1e5+10;
int a[maxx];
int main()
{
    long long int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        if(k>i)
        {
            k-=i;
        }
        else
            break;
    }
    cout<<a[k]<<endl;
    return 0;
}
發佈了58 篇原創文章 · 獲贊 9 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章