携程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

相关推荐

wu970:标准北漂配置,怎么看着装修风格有点像自如的😭
点赞 评论 收藏
分享
04-16 10:27
已编辑
美团_Saas_后端开发
今天周一休息,突发奇想写一篇阶段总结。如题,我已经去了一个和Java彻底毫无关联的行业。曾经我以为自己能在计算机行业发光发热,拿到美团offer那会感觉自己天都亮了。没想到刚入行一年多就当了逃兵。从最开始的热爱到现在一看到代码就厌恶,不知道自己经历了什么。所以我去干什么了?答案是:在成都当了租房销售。上班那会压力大了就念叨着去干租房中介,但是一直下不去这个决心,想着自己学了四年多的计算机知识,终究还是不甘心。终于在某一天准备八股文的时候,看着无数篇和工作内容关系不大的理论知识,那一刻下定决心,决定尝试一下销售行业,也算是给自己一个交代。后面阴差阳错的投了成都自如去当租房管家,没想到面试很顺利,在当天一百多个面试的人里面,我成为了为数不多通过的几个幸运儿之一。目前已经培训通过,正式入职,也开了单,有压力但是每天过得很开心,真心喜欢那种和人交流的感觉,哪怕是最后没有选择找我租房。说这些也是想告诉那些大三,大四正在找Java实习而焦虑的同学:你们现在还年轻,选择很多,容错率也很高,可以尽情去尝试自己喜欢的行业和工作。不用因为某一次的面试没通过或者简历石沉大海而焦虑,更不用因为身边人都在挤编程的独木桥就强迫自己跟风。也算是自己的碎碎念吧,也希望自己能在新的领域取得一点小成就。也祝牛油工作顺利!
沉淀小子:干啥都不丢人啊,生存是必须要的,销售很考验一个人综合素质能力的,好的销售人脉和资源可不比写字楼的白领差啊
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务