浙江大学软件学院2020年保研上机模拟练习

文章目录

  • 7-1 Standard Form of Polynomial (20 分)
  • 7-2 Distance of Triples (25 分)
  • 7-3 Partial School Ranking (25 分)
  • 7-4 Shopping With Coupons (30 分)

7-1 Standard Form of Polynomial (20 分)

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
vector<int> roots, coefficients;
void dfs(int level, int power, int coef){
    if (roots.size() == level){
        coefficients[power] += coef;
        return;
    }
    dfs(level+1, power+1, coef);
    dfs(level+1, power, -coef*roots[level]);
}
int main(){
    int n;
    cin >> n;
    for (int i = 0; i < n; i++){
        int num;
        cin >> num;
        roots.push_back(num);
    }
    coefficients = *new vector<int>(n, 0);
    dfs(0, 0, 1);
    auto it = coefficients.rbegin();
    cout << *it;
    it++;
    while(it != coefficients.rend()){
        cout << " " << *it;
        it++;
    }
    return 0;
}

7-2 Distance of Triples (25 分)

#include 
#include 
#include 
#include 
#include 
using namespace std;
int distance(int& a, int& b, int& c){
    int res = 0;
    res += abs(a-b);
    res += abs(b-c);
    res += abs(c-a);
    return res;
}
int main(){
    cin.tie(0);
    vector<int> n;
    for (int i = 0; i < 3; i++){
        int num;
        cin >> num;
        n.push_back(num);
    }
    vector<vector<int>> v;
    for (int i = 0; i < 3; i++){
        v.push_back(*new vector<int>());
        for (int j = 0; j < n[i]; j++){
            int num;
            cin >> num;
            v[i].push_back(num);
        }
        sort(v[i].begin(), v[i].end(), greater<int>());
    }
    vector<int> res(3, 0);
    int m = INT_MAX;
    auto i = v[0].begin(), j = v[1].begin(), k = v[2].begin();
    while(i!=v[0].end() && j!=v[1].end() && k!=v[2].end()){
        int d = distance(*i, *j, *k);
        if (d < m){
            m = d;
            res = {*i, *j, *k};
        }
        if ((*k >= *i) && (*k >= *j)){
            k++;
        }else if ((*j >= *i) && (*j >= *k)){
            j++;
        }else{
            i++;
        }
    }
    printf("MinD(%d, %d, %d) = %d", res[0], res[1], res[2], m);
    return 0;
}

7-3 Partial School Ranking (25 分)

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
class Union{
public:
    struct Rank{
        int represent, nums, total_score;
        Rank(int r){
            represent = r;
            nums = 0;
            total_score = 0;
        }
        void add(int s){
            nums++;
            total_score += s;
        }
    };
    vector<int> students = *new vector<int>(10000, -1);
    unordered_map<int, int> scores;
    int count = 0;
    int find(int p_){
        int p = p_;
        while(students[p]!=-1&&students[p] != p) p = students[p];
        if (students[p] == -1) {
            students[p_] = p_;
            p = p_;
        }
        return p;
    }
    void combine(int p, int q){
        int p_root = find(p);
        int q_root = find(q);
        if (p_root < q_root){
            students[q_root] = p_root;
        }else{
            students[p_root] = q_root;
        }
    }
    vector<Rank> ranks;
    void statistic(){
        for (int i = 0; i < 10000; i++){
            if (students[i] != -1){
                if (students[i] == i){
                    ranks.push_back(*new Rank(i));
                }
                int s = 0;
                if (scores.find(i) != scores.end()){
                    s = scores.at(i);
                }
                int i_root = find(i);
                for(auto i = ranks.begin(); i != ranks.end(); i++){
                    if ((*i).represent == i_root){
                        (*i).add(s);
                        break;
                    }
                }
            }
        }
    }
    static bool cmp(Rank a, Rank b){
        if (a.total_score == b.total_score){
            if (a.nums == b.nums){
                return a.represent < b.represent;
            }else{
                return a.nums < b.nums;
            }
        }else{
            return a.total_score > b.total_score;
        }
    }
    void show(){
        sort(ranks.begin(), ranks.end(), cmp);
        cout << ranks.size() << endl;
        for (auto i : ranks){
            printf("%04d %d %d\n", i.represent, i.nums, i.total_score);
        }
    }
};
int main(){
    Union u;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++){
        int stu, k, score, mate;
        cin >> stu >> k;
        u.combine(stu, stu);
        for (int j = 0; j < k; j++){
            cin >> mate;
            u.combine(stu, mate);
        }
        cin >> score;
        u.scores.insert(make_pair(stu, score));
    }
    u.statistic();
    u.show();
    return 0;
}

7-4 Shopping With Coupons (30 分)

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
vector<int> items, coupons;
class Cost{
public:
    int item, coupon, cost;
    Cost(int i, int c){
        item = i;
        coupon = c;
        cost = items[i] - coupons[c];
    }
    friend bool operator < (Cost a, Cost b){
        return a.cost > b.cost;
    }
};
int main(){
    int n, d;
    cin >> n >> d;
    int n_item = 0;
    for (int i = 0; i < n; i++){
        int num;
        cin >> num;
        items.push_back(num);
    }
    sort(items.begin(), items.end());
    for (int i = 0; i < n; i++){
        int num;
        cin >> num;
        coupons.push_back(num);
    }
    sort(coupons.begin(), coupons.end(), greater<int>());
    priority_queue<Cost> pq;
    for (int i = 0; i < n; i++){
        pq.push(*new Cost(i, 0));
    }
    while (d > 0){
        int to_buy = 0;
        if (!pq.empty()){
            Cost c = pq.top();
            pq.pop();
            if (c.coupon != n-1){
                pq.push(*new Cost(c.item, c.coupon+1));
            }
            to_buy = c.cost;
        }else{
            break;
        }
        if (to_buy > d) break;
        n_item++;
        d -= to_buy;
    }
    cout << n_item << " " << d;
    return 0;
}

你可能感兴趣的:(c++,深度优先,算法)