Programming Contest Problem Set and Solutions

Diamond Planting

#include <iostream>
using namespace std;

int main() {
    int plants, rate;
    cin >> plants >> rate;
    cout << plants / rate << endl;
    return 0;
}

Pyramid Pattern Generation

#include <iostream>
using namespace std;

int main() {
    cout << "   *\n";
    cout << "  ***\n";
    cout << " *****\n";
    cout << "*******\n";
    return 0;
}

Date Format Correction

#include <iostream>
#include <string>
using namespace std;

int main() {
    string input;
    cin >> input;
    if (input.length() < 6) {
        string prefix = (input.substr(0, 2) < "22") ? "20" : "19";
        input = prefix + input;
    }
    cout << input.substr(0, 4) + '-' + input.substr(4) << endl;
    return 0;
}

Resource Allocation Aanlysis

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    if (a > 0 && b > 0) {
        cout << max(a, b) << ' ' << a + b << "\n^_^";
    } else if (a < 0 && b < 0) {
        cout << "0 0\n-_-";
    } else {
        int combined = (a + b > 0) ? a + b : 0;
        cout << max(a, b) << ' ' << combined << "\nT_T";
    }
    return 0;
}

Tire Pressure Validation

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> readings(6);
    for (int i = 0; i < 6; i++) cin >> readings[i];
    
    int max_val = *max_element(readings.begin(), readings.begin() + 4);
    int error_count = 0, error_index = -1;
    
    for (int i = 0; i < 4; i++) {
        if (abs(readings[i] - max_val) > readings[5] || readings[i] < readings[4]) {
            error_count++;
            error_index = i;
        }
    }
    
    if (error_count == 0) cout << "Normal";
    else if (error_count == 1) cout << "Warning: please check #" << error_index + 1 << '!';
    else cout << "Warning: please check all the tires!";
    return 0;
}

Task Filtering System

#include <iostream>
#include <string>
using namespace std;

int main() {
    int total, threshold;
    cin >> total >> threshold;
    cin.ignore();
    
    string line;
    for (int i = 0; i < total; i++) {
        getline(cin, line);
        if (line.find("easy") == string::npos && line.find("qiandao") == string::npos) {
            if (--threshold == -1) {
                cout << line << endl;
                return 0;
            }
        }
    }
    cout << "Wo AK le" << endl;
    return 0;
}

Selection Process Optimization

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int candidates, limit, benchmark;
    cin >> candidates >> limit >> benchmark;
    vector<int> scores(300, 0);
    int selected = 0;
    
    for (int i = 0; i < candidates; i++) {
        int scoreA, scoreB;
        cin >> scoreA >> scoreB;
        if (scoreA < 175) continue;
        if (scoreB >= benchmark) selected++;
        else scores[scoreA]++;
    }
    
    for (int i = 175; i <= 290; i++) {
        selected += min(scores[i], limit);
    }
    cout << selected << endl;
    return 0;
}

Grid Modification System

#include <iostream>
#include <vector>
using namespace std;

int main() {
    long rows, cols, queries;
    cin >> rows >> cols >> queries;
    vector<bool> row_removed(100100, false);
    vector<bool> col_removed(100100, false);
    
    while (queries--) {
        int type, position;
        cin >> type >> position;
        if (type == 0 && !row_removed[position]) {
            rows--;
            row_removed[position] = true;
        } else if (type == 1 && !col_removed[position]) {
            cols--;
            col_removed[position] = true;
        }
    }
    cout << rows * cols << endl;
    return 0;
}

Containre Management Simulation

#include <iostream>
#include <stack>
using namespace std;

int main() {
    int capacity, stack_size, tests;
    cin >> capacity >> stack_size >> tests;
    
    while (tests--) {
        stack<int> storage, sequence;
        bool valid = true;
        
        for (int i = 0; i < capacity; i++) {
            int num;
            cin >> num;
            if (sequence.size() > stack_size) continue;
            
            if (sequence.empty() && num == 1) {
                sequence.push(num);
            } else if (!sequence.empty() && num == sequence.top() + 1) {
                sequence.push(num);
            } else {
                storage.push(num);
            }
            
            while (!sequence.empty() && !storage.empty() && storage.top() == sequence.top() + 1) {
                sequence.push(storage.top());
                storage.pop();
            }
        }
        cout << (sequence.size() == capacity ? "YES" : "NO") << endl;
    }
    return 0;
}

Arithmetic Expression Evaluator

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int count;
    cin >> count;
    vector<int> values(count);
    for (int i = 0; i < count; i++) cin >> values[i];
    
    vector<char> operators(count - 1);
    for (int i = 0; i < count - 1; i++) cin >> operators[i];
    
    while (values.size() > 1) {
        int right = values.back(); values.pop_back();
        int left = values.back(); values.pop_back();
        char op = operators.back(); operators.pop_back();
        
        switch (op) {
            case '+': values.push_back(left + right); break;
            case '-': values.push_back(left - right); break;
            case '*': values.push_back(left * right); break;
            case '/': 
                if (right == 0) {
                    cout << "ERROR: " << left << "/0\n";
                    return 0;
                }
                values.push_back(left / right);
                break;
        }
    }
    cout << values[0] << endl;
    return 0;
}

Delivery Route Optimization

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Tree {
    vector<vector<int>> connections;
    vector<int> depths;
    vector<vector<int>> ancestors;
    
    Tree(int size) {
        connections.resize(size + 1);
        depths.resize(size + 1);
        ancestors.resize(size + 1, vector<int>(21, 0));
    }
    
    void add_edge(int u, int v) {
        connections[u].push_back(v);
        connections[v].push_back(u);
    }
    
    void traverse(int node, int parent) {
        depths[node] = depths[parent] + 1;
        ancestors[node][0] = parent;
        for (int i = 1; i < 20; i++) {
            ancestors[node][i] = ancestors[ancestors[node][i-1]][i-1];
        }
        for (int neighbor : connections[node]) {
            if (neighbor != parent) traverse(neighbor, node);
        }
    }
};

int main() {
    int nodes, deliveries;
    cin >> nodes >> deliveries;
    Tree tree(nodes);
    int hub = 0;
    
    for (int i = 1; i <= nodes; i++) {
        int parent;
        cin >> parent;
        if (parent == -1) hub = i;
        else tree.add_edge(parent, i);
    }
    
    tree.traverse(hub, 0);
    vector<bool> visited(nodes + 1, false);
    visited[hub] = true;
    long total_distance = 0, max_depth = 0;
    
    while (deliveries--) {
        int destination;
        cin >> destination;
        int current = destination;
        while (!visited[current]) {
            visited[current] = true;
            total_distance += 2;
            current = tree.ancestors[current][0];
        }
        max_depth = max(max_depth, (long)tree.depths[destination]);
        cout << total_distance - max_depth + 1 << '\n';
    }
    return 0;
}

Hierarchical Data Management

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
    int nodes, edges;
    cin >> nodes >> edges;
    map<int, int> parent, counts;
    map<string, int> ids;
    int counter = 1;
    
    while (edges--) {
        string child, parent_name;
        cin >> child >> parent_name;
        if (!ids.count(parent_name)) ids[parent_name] = counter++;
        int parent_id = ids[parent_name];
        
        if (isdigit(child[0])) {
            int person = stoi(child);
            parent[person] = parent_id;
            while (parent_id) {
                counts[parent_id]++;
                parent_id = parent[parent_id];
            }
        } else {
            if (!ids.count(child)) ids[child] = counter++;
            parent[ids[child]] = parent_id;
            int child_count = counts[ids[child]];
            while (parent_id) {
                counts[parent_id] += child_count;
                parent_id = parent[parent_id];
            }
        }
    }
    
    char command;
    while (cin >> command && command != 'E') {
        if (command == 'T') {
            int person;
            string new_parent;
            cin >> person >> new_parent;
            int old_parent = parent[person];
            while (old_parent) {
                counts[old_parent]--;
                old_parent = parent[old_parent];
            }
            parent[person] = ids[new_parent];
            int current = ids[new_parent];
            while (current) {
                counts[current]++;
                current = parent[current];
            }
        } else {
            string name;
            cin >> name;
            cout << counts[ids[name]] << '\n';
        }
    }
    return 0;
}

Tags: C++ STL simulation stack tree

Posted on Mon, 26 Oct 2026 16:37:01 +0000 by Irksome