调度问题-完成事件需要时间:a[i],截止时间:t[i]

问题一:https://nanti.jisuanke.com/t/43373

水灾了。有n个城市。抢救每个城市需要时间a[i]。每个城市会在t[i]时被销毁。
必须在<=t[i](小于等于)才能抢救这个城市。问你最多能够抢救多少城市,时间从0开始。
in:
5
5 10
6 15
2 7
3 3
4 11
out:
4

在这里插入图片描述

#include <bits/stdc++.h>
#define LL long long
using namespace std;

struct node{
    LL t, h;
}a[200005];
struct rule{
    int operator()(const node &a, const node &b){
        return a.t<b.t;
    }
};
priority_queue<node, vector<node>, rule> q;
int main(){
    LL n; scanf("%lld", &n);
    for(LL i=1; i<=n; i++){
        scanf("%lld%lld", &a[i].t, &a[i].h);
    }
    sort(a+1, a+n+1, [](node &a, node &b){return a.h<b.h;});
    LL H=0, ans=0;
    for(LL i=1; i<=n; i++){
        if(H+a[i].t<=a[i].h){
            ans++;
            H+=a[i].t;
            q.push(a[i]);
        }
        else if(!q.empty()){
            node t=q.top();
            if(t.t>a[i].t&&H-t.t+a[i].t<=a[i].h){
                q.pop();
                q.push(a[i]);
                H=H-t.t+a[i].t;
            }
        }
    }
    printf("%lld\n", ans);

    return 0;
}

问题二:https://codeforces.com/contest/864/problem/E

起火了。有n个文件。抢救每个文件需要时间a[i]。每个文件会在t[i]时被销毁,有价值c[i]。
必须在<t[i](严格小于)才能抢救这个文件。问你最多能够抢救多少价值的文件,时间从0开始。

我 们 还 是 按 t [ i ] 排 序 。 用 f [ i ] [ j ] : 前 i 个 文 件 在 时 间 j 以 内 最 多 能 够 抢 救 最 大 价 值 。 我们还是按t[i]排序。用f[i][j]:前i个文件在时间j以内最多能够抢救最大价值。 t[i]f[i][j]ij

#include <bits/stdc++.h>
#define LL long long
using namespace std;

struct node{
    int t, d, p, i;
}a[105];

int f[105][2005];//前i个文件。时间恰好是j能保存的最多价值
int g[105][2005];
int main(){

    int n; scanf("%d", &n);
    for(int i=1; i<=n; i++){
        scanf("%d%d%d", &a[i].t, &a[i].d, &a[i].p);
        a[i].i=i;
    }
    sort(a+1, a+n+1, [](node &a, node &b){return a.d<b.d;});
    int ans=0, s=0;
    for(int i=1; i<=n; i++){
        for(int j=0; j<=2000; j++){
            if(j-a[i].t>=0&&j<a[i].d){
                if(f[i-1][j]>f[i-1][j-a[i].t]+a[i].p){
                    f[i][j]=f[i-1][j];
                    g[i][j]=0;
                }
                else{
                    f[i][j]=f[i-1][j-a[i].t]+a[i].p;
                    g[i][j]=1;
                }

            }
            else{
                f[i][j]=f[i-1][j];
                g[i][j]=0;
            }
            if(f[i][j]>ans){
                ans=f[i][j];
                s=j;
            }
        }
    }
    cout<<ans<<endl;
    vector<int> v;
    while(n>=1){
        if(g[n][s]){
            v.push_back(a[n].i);
            s-=a[n].t;
        }
        n--;
    }

    cout<<v.size()<<endl;
    for(int i=v.size()-1; i>=0; i--){
        cout<<v[i]<<" ";
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值