45分求调
查看原帖
45分求调
1029721
AnsonIsTheBest楼主2025/1/1 04:12

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Line {
    int start;
    int end;
    int num;
};

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;

        vector<int> x(n);
        for (int i = 0; i < n; ++i) {
            cin >> x[i];
        }

        vector<Line> lines(k);
        for (int i = 0; i < k; ++i) {
            cin >> lines[i].start >> lines[i].end >> lines[i].num;
        }

        sort(x.begin(), x.end());

       vector<int> diff(n,1);
       vector<int> pref(n+1,0);

       for(int i=1;i<=n;i++){
        pref[i]=pref[i-1]+diff[i-1];
       }
        
        int cut_count = 0;

        for (int i = n - 1; i >= 0; --i) {
            int original_diff = diff[i];
             diff[i] = 0; 
             for(int j=i+1;j<=n;j++){
               pref[j]=pref[j-1]+diff[j-1];
             }

            bool valid_cut = true;
            for (int j = 0; j < k; ++j) {
                int left_bound = lines[j].start;
                int right_bound = lines[j].end;
                int required_trees = lines[j].num;
                
                auto left_it = lower_bound(x.begin(), x.end(), left_bound);
                auto right_it = upper_bound(x.begin(), x.end(), right_bound);
                int left_index = distance(x.begin(),left_it);
                int right_index = distance(x.begin(),right_it);


               int remaining_trees = 0;
              if(left_index<right_index){
                 remaining_trees = pref[right_index]-pref[left_index];
                 
                  }
                
                if (remaining_trees < required_trees) {
                    valid_cut = false;
                    break;
                }
            }

            if (valid_cut) {
                cut_count++;
            } else {
                diff[i] = original_diff;
                   for(int j=i+1;j<=n;j++){
                     pref[j]=pref[j-1]+diff[j-1];
                }
            }
        }
        cout << cut_count << endl;
    }

    return 0;
}
2025/1/1 04:12
加载中...