21号和26号测试样例不过
查看原帖
21号和26号测试样例不过
1503229
uwxjsr楼主2025/6/22 19:08

21号和26号测试样例不过,其余可过,求调

#include<bits/stdc++.h>
using namespace std;

struct tool {
    int kind;
    long long cost;
};

bool cmp(tool x, tool y) {
    return x.cost < y.cost;
}

int main() {
    int n, m;
    long long final_cost = 0;
    cin >> n >> m;
    
    vector<int> count(n + 1, 0);
    vector<tool> materials(m + 1);
    
    for(int i = 1; i <= m; i++) {
        cin >> materials[i].kind >> materials[i].cost;
        count[materials[i].kind]++;
    }
    
    sort(materials.begin() + 1, materials.end(), cmp);
    
    while(true) {
        int max_others = 0;
        for(int i = 2; i <= n; i++) {
            max_others = max(max_others, count[i]);
        }
        
        if(count[1] > max_others) {
            break;
        }
        
        bool found = false;
        for(int i = 1; i <= m; i++) {
            if(materials[i].kind != 1) {  
                final_cost += materials[i].cost;
                count[materials[i].kind]--;
                count[1]++;
                materials[i].kind = 1;  
                found = true;
                break;
            }
        }
        
        if(!found) break;  
    }
    
    cout << final_cost << endl;
    return 0;
}
2025/6/22 19:08
加载中...