携程9.9算法笔试 零分卷

第一题用前缀和处理应该能全a的,但是我贪快就直接暴力加了,过了87%
int main() {
    int n(0), m(0), k(0);
    cin >> n >> m >> k;
    vector<vector<int> > mat(n, vector<int>(m, 0));
    for (int i = 0; i != n; ++i) {
        for (int j = 0; j != m; ++j) {
            cin >> mat[i][j];
        }
    }
    int max_num(0), current_num(0);
    for (int i = 0; i != n - k; ++i) {
        for (int j = 0; j != m - k; ++j) {
            current_num = 0;
            for (int iter_i = i; iter_i != i + k; ++iter_i) {
                for (int iter_j = j; iter_j != j + k; ++iter_j) {
                    current_num += mat[iter_i][iter_j];
                }
            }
            if (current_num > max_num) max_num = current_num;
        }
    }
    cout << max_num << endl;
    return 0;
}
第二题 直接模拟仓库操作,一共就0~10,1~500的数据范围,不知道为什么只过了27%,有人知道原因吗
bool isBigger(vector<int> a, vector<int> b) {
    return a[0] > b[0];
}
int main() {
    vector<vector<int> > goods;
    int goods_num(0);
    int n(0);
    cin >> n;
    cin.ignore();
    for (int iter = 0; iter != n; ++iter) {
        string input_command;
        getline(cin, input_command);
        vector<int> command;
        istringstream ss(input_command);
        string temp;
        while (ss >> temp) {
            command.push_back(stoi(temp));
        }
        if (command[0] == 1) {
            int pos(-1);
            goods_num += command[1];
            for (int i = goods.size() - 1; i >= 0; --i) {
                if (goods[i][0] == command[2]) {
                    pos = i;
                    goods[i][1] += command[1];
                    break;
                }
            }
            if (pos == -1) {
                vector<int> vtemp = { command[2],command[1] };
                goods.push_back(vtemp);
            }
            sort(goods.begin(), goods.end(), isBigger);
        }
        else if (command[0] == 2) {
            if (command[1] <= goods_num) {
                cout << "yes" << endl;
                goods_num -= command[1];
                int temp_num(command[1]);
                while (temp_num > 0) {
                    if (temp_num < (goods.back())[1]) {
                        (goods.back())[1] -= temp_num;
                        temp_num = 0;
                    }
                    else {
                        temp_num -= (goods.back())[1];
                        goods.pop_back();
                    }
                }
            }
            else {
                cout << "no" << endl;
            }
        }
        else if (command[0] == 3) {
            for (int i = goods.size() - 1; i >= 0; --i) {
                --goods[i][0];
            }
            if ((goods.back())[0] <= 0) {
                goods_num -= (goods.back())[1];
                goods.pop_back();
            }
        }
        else if (command[0] == 4) {
            if (goods_num == 0) cout << 0 << endl;
            else {
                cout << (goods.back())[0] << endl;
            }
        }
    }
    return 0;
}
第三题 从根底部往上推,全过了
int main(){
    int n(0),root(0);
    cin>>n>>root;
    vector<int> treenode(n+1,0);
    for(int i=1;i<=n;++i){
        cin>>treenode[i];
    }
    vector<int> node_deg(n+1,0);
    unordered_map<int,vector<int> > node_indeg;
    for(int i=0;i!=n-1;++i){
        int node1(0),node2(0);
        cin>>node1>>node2;
        if(node_indeg.find(node1)==node_indeg.end()){
            pair<int,vector<int>> ptemp(node1,{node2});
            node_indeg.insert(ptemp);
        }
        else{
            (node_indeg.find(node1)->second).push_back(node2);
        }
        if(node_indeg.find(node2)==node_indeg.end()){
            pair<int,vector<int>> ptemp(node2,{node1});
            node_indeg.insert(ptemp);
        }
        else{
            (node_indeg.find(node2)->second).push_back(node1);
        }
    }
    while(node_indeg.size()>1){
        vector<int> current_del;
        for(auto it=node_indeg.begin();it!=node_indeg.end();++it){
            if((it->second).size()==1 and (it->first)!=root){
                if(treenode[it->first]!=treenode[(it->second)[0]]){
                    node_deg[(it->second)[0]]+=(node_deg[it->first]+1);
                }
                else{
                    node_deg[(it->second)[0]]+=node_deg[it->first];
                }
                current_del.push_back(it->first);
                int del_pos(0);
                for(int i=0;i!=((node_indeg.find((it->second)[0]))->second).size();++i){
                    if((node_indeg.find((it->second)[0])->second)[i]==it->first){
                        del_pos=i;
                        break;
                    }
                }
                ((node_indeg.find((it->second)[0]))->second).erase(((node_indeg.find((it->second)[0]))->second).begin()+del_pos);
            }
        }
        for(int i=0;i!=current_del.size();++i){
            node_indeg.erase(current_del[i]);
        }
    }
    for(int i=1;i<=n;++i){
        if(i!=n) cout<<node_deg[i]<<" ";
        else cout<<node_deg[i];
    }
    return 0;
}

所以第二题到底是为什么只过了27%?想了一个半小时都没想出来


#携程笔试##携程##笔经#
全部评论
第一题暴力也能全过啊😂,直接从k到n
点赞 回复
分享
发布于 2021-09-09 21:46

相关推荐

1 1 评论
分享
牛客网
牛客企业服务