腾讯笔试 90 100 0 100 40,求3,5题的解法
第一题
只需看0 到 n-11 有没有8即可,看别的帖,只通过90%好像是因为题目测试用例本身有错误
#include <bits/stdc++.h>
using namespace std;
int main() {
    int t = 0;
    cin >> t;
    for (int i = 0; i < t; i++) {
        int n = 0;
        cin >> n;
        string s;
        cin >> s;
        bool valid = false;
        for (int j = 0; j <= n - 11; j++) {
            if (s[j] == '8') {
                valid = true;
                break;
            }
        }
        if (valid) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}第二题
整个排序,然后每次两两取首尾求和,求最大值
一开始直接一个vector存所有的延时时间,结果提交发现超内存了。
所以还是直接存 x, y, 然后再遍历
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n = 0;
    cin >> n;
    vector<pair<int, int>> delay(n);
    int total = 0;
    for (int i = 0; i < n; i++) {
        cin >> delay[i].first >> delay[i].second;
        total += delay[i].first;
    }
    auto cmp = [](const pair<int, int>& x, const pair<int, int>& y) {
        return x.second < y.second;
    };
    sort(delay.begin(), delay.end(), cmp);
    int minCost = 0;
    int i = 0;
    int j = delay.size() - 1;
    int count = 0;
    while (count < total) {
        if (delay[i].first == 0) {
            i++;
        }
        if (delay[j].first == 0) {
            j--;
        }
        minCost = max(minCost, delay[i].second + delay[j].second);
        delay[i].first--;
        delay[j].first--;
        count += 2;
    }
    cout << minCost << endl;
    return 0;
}第四题
只要记录每次输出x的和sumX,然后与sumX相等的数直接略过就可以了,我用了小根堆,实际上直接vector存,然后排序,再遍历就可以了
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n = 0;
    int k = 0;
    cin >> n >> k;
    priority_queue<int, vector<int>, greater<int>> minHeap;
    for (int i = 0; i < n; i++) {
        int number = 0;
        cin >> number;
        minHeap.push(number);
    }
    int sumX = 0;
    for (int i = 0; i < k; i++) {
        while (!minHeap.empty() && minHeap.top() == sumX) {
            minHeap.pop();
        }
        if (minHeap.empty()) {
            cout << 0 << endl;
        } else {
            cout << minHeap.top() - sumX << endl;
            sumX = minHeap.top();
        }    
    }
    return 0;
}第五题
完全暴力解的,过了40%
感觉腾讯的题还算友好吧,毕竟这是我这么多笔试以来做的最好的一次了。。。
求第三题、第五题解法
#笔试题目##腾讯#
 查看1道真题和解析
查看1道真题和解析 投递浦发银行等公司10个岗位
投递浦发银行等公司10个岗位