排序 贪心算法 注意一下临界值

剩2题时 判断一下中间的题目难度值是否可以满足要求,如果是 则只需要额外出一题。

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

using namespace std;

void solve(vector<int> &inputs) {
    sort(inputs.begin(), inputs.end());
    int len = inputs.size();
    int result = 0;
    
    for (int i = 0; i < len;) {
        if (len - i >= 3) {
            int n1 = inputs[i];
            int n2 = inputs[i + 1];
            int n3 = inputs[i + 2];
            if ((n3 - n2) <= 10 && (n2 - n1) <= 10) {
                i += 3;
            } else if ((n2 - n1) <= 10 && (n3 - n2) > 10) {
                i += 2;
                result++;
            } else if((n2 - n1) >10) {
                i++;
                result += 2;
            }
        } else if (len - i >= 2) {
            int n1 = inputs[i];
            int n2 = inputs[i +1];
            if (n2 - n1 <= 10) {
                result++;
                i += 2;
            } else if ((n2 - n1) > 10 && (n2 - n1) <= 20) {
                result++;
                i+=2;
            } else {
                result+=4;
                i+=2;
            }
        } else if (len - i == 1) {
            result += 2;
            i++;
        }
}
    cout << result;
}

int main() {
    int n;
    while(cin>>n) {
        vector<int> inputs;
        for (int i = 0; i < n; i++) {
            int x;
            cin>>x;
            inputs.push_back(x);
        }
        solve(inputs);
    }
    return 0;
}