51Nod 1163 最高的奖励 贪心

本文介绍了一种使用贪心算法解决任务调度问题的方法,包括两种不同的实现思路:一种利用优先队列确保每次选择收益最大的任务;另一种采用类似于并查集的方法按奖励大小排序任务,并确保每个选定的任务不会占用已分配的时间段。

思路1:

这个是非常好的一道贪心题目!

每个任务有一个最晚完成时间,用一个队列来维护到当前时间t的时候,一共完成了哪些任务。 遇到最晚完成时间大于t的,直接加入队列,与t相等的话,则置换一下,使得队列里面的值最大。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node {
    int e;
    ll w;
    node ( int _e,ll _w ) {
        e = _e;
        w = _w;
    }
    bool operator < ( const node & a ) const  {
        return w>a.w;
    }
};

bool cmp( const node &a,const node &b ) {
    return a.e<b.e;
}
vector<node> vec;
int main() {
    int n;
    int m=0;
    int e,w;
    cin>>n;
    for ( int i=0; i<n; i++ ) {
        scanf("%d%d",&e,&w);
        vec.push_back( node(e,w) ) ;
    }
    sort( vec.begin() ,vec.end(),cmp );
    priority_queue<node> que;
    int time = 1;
    for ( int i=0; i<vec.size(); i++ ) {
        if ( vec[i].e>=time ) {
            que.push( vec[i] ) ;
            time++;
        } else {
            if ( vec[i].w > que.top().w ) {
                que.pop();
                que.push( vec[i] );
            }
        }
    }
    ll ans = 0 ;
    while ( que.empty()==0 ) {
        ans += que.top().w;
        que.pop();
    }
    cout<<ans<<endl;
    return 0 ;
}


思路2:

这个思路我有想过,但是一直n2的复杂度,没想到可以用类似并查集的思想实现。

可以根据奖励的大小排序。

f[i] = j 代表 最晚完成时间可以在1-j时间内完成。

当我们选定过了一个活动,一定要从j到1进行赋值,不能抢占前面的时间。

#include <bits/stdc++.h>
using namespace std;
const int N = 50000+100;
struct node {
    int cost;
    int time;
    node(){};
    node ( int _c,int _t) { cost = _c; time = _t; };
};
int f[N];
node nodes[N];
int getTime( int x )  {
    if ( x==0 ) return 0;
    else if ( f[x]==x ) {
        f[x] = x-1;
        return x;
    } else {
        return f[x] = getTime( f[x] );
    }
}
bool cmp( const node &a,const node &b ) {
    if ( a.cost!=b.cost )
        return a.cost>b.cost;
    else
        return a.time<b.time;
}
int main()
{
    int n;
    scanf("%d",&n);
    for ( int i=1; i<=n; i++ )
        scanf("%d%d",&nodes[i].time,&nodes[i].cost),f[i]=i;
    sort( nodes+1,nodes+1+n,cmp );
    long long ans =0 ;
    for ( int i=1; i<=n ; i++ ) {
        if ( getTime( nodes[i].time ) >0 )
            ans += nodes[i].cost;
    }
    cout<<ans<<endl ;
    return 0;
}

目前没有关于51nod 3478题目的具体描述和官方公布的C++解决方案代码。以下是一种通用的解题思路以及一个示例C++代码模板,可以用于解决类似的问题。 ### 问题解题思路 51nod 3478通常可能涉及以下算法或技术: - 动态规划(DP)或状态转移方程 - 贪心算法 - 数据结构(如线段树、堆、优先队列等) - 图论算法(如最短路径、最小生成树等) ### 示例C++代码模板 以下是一个通用的C++代码框架,适用于需要读取输入并处理大规模数据的问题: ```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 100005; // 根据题目规模调整 int n; ll k; ll a[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; a[i] += a[i - 1]; // 前缀和 } // 示例逻辑:查找是否存在和为k的连续子数组 unordered_map<ll, int> prefix_map; prefix_map[0] = 0; for (int i = 1; i <= n; ++i) { if (prefix_map.find(a[i] - k) != prefix_map.end()) { cout << prefix_map[a[i] - k] + 1 << " " << i << endl; return 0; } prefix_map[a[i]] = i; } cout << "No Solution" << endl; return 0; } ``` ### 说明 - 上述代码使用了前缀和和哈希表(`unordered_map`)来高效查找是否存在和为`k`的连续子数组。 - 时间复杂度为O(n),适用于大规模输入。 - 如果题目有其他特定要求,可以根据具体条件修改代码逻辑。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值