2 solutions

  • 1
    #include <bits/stdc++.h>
    #define endl '\n'
    using namespace std;
    typedef pair<int, int> PII;
    using ll = long long;
    using ULL = unsigned long long;
    const int N = 1e7 + 5;
    
    int n, a[N];
    inline void solve() {
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> a[i];
        int m; cin >> m;
        for (; m-- ;) {
            int l, r, k;
            cin >> l >> r >> k;
             vector<int> b;  
            b.reserve(r - l + 1);  
            for (int i = l; i <= r; i++) {
                b.push_back(a[i]);
            }
            nth_element(b.begin(), b.begin() + (k-1), b.end(), greater<int>());
            cout << b[k-1] << endl;
        }
    }
    
    int main() {
        ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        int _ = 1;
        //int _; cin >> _;
        while (_--) solve();
        return 0;
    }
    
    • 0
      @ 1 year ago

      一开始并非从大到小 需要先选择l和r,再进行sort排序

      #include <iostream>
      #include <bits/stdc++.h>
      using namespace std;
      struct number{
      	int l;
      	int r;
      	int K;
      	int b[10005];
      	int ret;
      }pp[10005];
      int main()
      {
      	int n;
      	int a[1005];
      	cin >> n;
      	for(int i = 1 ;i <= n ;i++){
      		cin >> a[i];
      	}
          int m;
      	cin >> m;
      	for(int i = 1 ;i <= m ;i++){
      		cin >> pp[i].l >>pp[i].r >> pp[i].K;
      		int k = 0;
      		for(int j = pp[i].l ;j <= pp[i].r ;j++){
      			k++;
      			pp[i].b[k] = a[j]; 
      		}
      		sort(pp[i].b + 1 ,pp[i].b + 1 + k);
      		pp[i].ret = pp[i].b[k+1-pp[i].K];
      	}
      	for(int i = 1 ;i <= m ;i++){
      		cout << pp[i].ret << "\n";
      	}
      	return 0;
      }
      
      • 1

      Information

      ID
      1799
      Time
      1000ms
      Memory
      512MiB
      Difficulty
      7
      Tags
      # Submissions
      26
      Accepted
      8
      Uploaded By