Morjani and buildings#12330

Morjani and buildings (Талдоо)

Problem Author:

Hadi Takla

Prepared by:

Hadi Takla

Edited by:

Hadi Takla

Solution at first , let be \max (h_1 , h_2 , ... , h_{k-1}).

we need the k_{th} building to be visible , so the following condition should hold.

h_i < h_k for all (1 \le i < k).

in other words.

h_k > \text{mx}

if the condition is already fulfilled we don't have to do any operation , otherwise since we can have more than one height greater than h_k , it is always optimal to increase the height of the k_{th} building until it is greater than .

so the answer is \max (0 , \text{mx} - h_k + 1)

time complexity : .


#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define F first
#define S second
using namespace std;
const int N = 2e5 + 1;
const int M = 1e9 + 7;
signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int tc;
    cin>>tc;
    while(tc--)
    {
        int n,k,mx = 0;
        cin>>n>>k;
        int h[n+10];
        for(int i=1;i<=n;i++)
        cin>>h[i];
        for(int i=1;i<k;i++)
        mx = max(mx , h[i]);
        int ans = max(0LL , mx - h[k] + 1);
        cout<<ans<<endl;
    }
}