Home BOJ. K th Number (1520)
Post
Cancel

BOJ. K th Number (1520)

[Link] https://www.acmicpc.net/problem/1520


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <bits/stdc++.h>
#define for0(i,n) for(int i=0;i<n;i++)
using namespace std;
int n,m,a,b,c,x,r;
pair<int,int>l[1<<17];
int main(){
  cin>>n>>m;for0(i,n){cin>>x;l[i]={x,i};};sort(l,l+n);
  for0(i,m){
    cin>>a>>b>>c;a--,b--;r=0;
    for0(j,n)if(a<=l[j].second&&l[j].second<=b)if(++r==c){x=l[j].first;break;}
    cout<<x<<'\n';
  }
  return 0;
}

Why? The code above gets AC but the code below gets TLE

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
#define for0(i,n) for(int i=0;i<n;i++)
using namespace std;
pair<int,int>l[1<<17];
int main(){
  int n,m,a,b,c,x,r;cin>>n>>m;for0(i,n){cin>>x;l[i]={x,i};};sort(l,l+n);
  for0(i,m){
    cin>>a>>b>>c;a--,b--;r=0;
    for0(j,n)if(a<=l[j].second&&l[j].second<=b)if(++r==c){x=l[j].first;break;}
    cout<<x<<'\n';
  }
  return 0;
}
This post is licensed under CC BY 4.0 by the author.