度小满笔试 64 100 0 求讨论

第一题:
思路是从后往前考虑怎么杀敌人,这样能保证每次杀的敌人都是最多的。
#include<iostream>
using namespace std;
int main(){
    int n;
    cin >> n;
    int A[n + 1];
    for (int i = 0; i < n; ++i)
        cin >> A[i + 1];
    int ans = 0;
    int head = n;
    while (head > 0){
        if (head >= 3){
            if ((head - 1) % 2 == 0){
                ans += A[head];
                A[head - 1] = A[head - 1] > A[head] ? A[head - 1] - A[head] : 0;
                A[(head - 1) / 2] = A[(head - 1) / 2] > A[head] ? A[(head- 1) / 2] - A[head] : 0;
            }
            else{
                ans += A[head];
                A[head / 2] = A[head / 2] > A[head] ? A[head - 1] - A[head] : 0;
            }
        }
        else if (head == 2){
            ans += A[2];
            A[1] = A[1] > A[2] ? A[1] - A[2] : 0;
        }
        else{
            ans += A[1];
        }
        --head;
    }
    cout << ans << endl;
}
第二题用一个最小堆来记录在桥上的车,每次发现下一辆车可以上桥的时候,就加入堆,否则就把堆顶的车pop,即堆顶的车通过了桥,此时时间也往前走。
#include <queue>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
int main(){
    int N, W;
    cin >> N >> W;
    int w[N], t[N];
    for (int i = 0; i < N; ++i)
        cin >> w[i];
    for (int i = 0; i < N; ++i)
        cin >> t[i];
    priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> Q;
    int cnt = 0;
    int sum = 0;
    int T = 0;
    while (cnt < N){
        if (sum + w[cnt] <= W){
            Q.push(make_pair(T + t[cnt], w[cnt]));
            sum += w[cnt];
            ++cnt;
        }
        
        else{
            sum -= Q.top().second;
            T = Q.top().first;
            Q.pop();
        }
    }
    while (!Q.empty()){
        T = Q.top().first;
        Q.pop();
    }
    cout << T << endl;
    return 0;
}



#度小满##校招##题解#
全部评论

相关推荐

10-30 19:23
已编辑
山东大学(威海) C++
牛至超人:我了个雷 1.实习经历写太长了吧,精简一点,你写那么老多,面试官看着都烦 2.项目经历你放俩竞赛干啥单独拿出来写上几等奖就行了呗 3.一大雷点就是项目经历里的那个课程设计,大家都知道课程设计巨水,不要写课程设计,换一个名字,就叫学生管理系统,面试官问就说是自己做的项目,不要提课程设计的事 4.那个交流经历,简化一下塞到最上面的教育经历里就行了 5.简历尽量一页纸
点赞 评论 收藏
分享
评论
点赞
3
分享

创作者周榜

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