資源限制 時間限制:1.0s 記憶體限制:256.0MB 問題描述 給定一個序列,每次詢問序列中第l個數到第r個數中第K大的數是哪個, 輸入格式 第一行包含一個數n,表示序列長度, 第二行包含n個正整數,表示給定的序列, 第三個包含一個正整數m,表示詢問個數, 接下來m行,每行三個數l,r,K,表示詢問序列從左往右第l個數到第r個數中,從大往小第K大的數是哪個,序列元素從1開始標號, 輸出格式 總共輸出m行,每行一個數,表示詢問的答案, 樣例輸入
5 1 2 3 4 5 2 1 5 2 2 3 2
樣例輸出
4 2
資料規模與約定
對于30%的資料,n,m<=100;
對于100%的資料,n,m<=1000;
保證k<=(r-l+1),序列中的數<=10^6,
本題可以借助STL的algorithm和vector將資料進行排序,然后查找并輸出,
代碼如下:
1 #include <iostream> 2 #include <algorithm> 3 #include <vector> 4 using namespace std; 5 bool cmp(int a, int b) 6 { 7 return a > b; 8 } 9 vector<int> a; 10 vector<int> b; 11 int n, m, l, r, k; 12 int main() 13 { 14 cin >> n; 15 for (int i = 0; i < n; i++) 16 { 17 int t; 18 cin >> t; 19 a.push_back(t); 20 } 21 cin >> m; 22 while (m--) 23 { 24 int t; 25 cin >> l >> r >> k; 26 for (int i = l - 1; i < r; i++) 27 { 28 b.push_back(a[i]); 29 } 30 sort(b.begin(), b.begin() + (r - l + 1), cmp); 31 cout << b[k - 1] << endl; 32 b.clear(); 33 } 34 return 0; 35 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/285772.html
標籤:其他
