zoukankan      html  css  js  c++  java
  • 最高的奖励

    题目地址:http://www.51cpc.com/web/problem.php?id=1587

    Summarize:

    优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先;

            2. 使用数组记录时间节点是否有任务,时间节点从最晚倒序遍历;

            3. 若此刻时间节点有任务,则从此时间节点往前推,直到某一刻无任务,否则放弃该任务;

    附贪心代码:

    (此处并未使用优先队列,以vector代替)

    #include<iostream>
    #include<algorithm>
    #include<vector>
    using namespace std;
    #define LL long long
    
    const int N = 5e5+5;
    int n;
    struct Task {
        LL t,v;
        bool operator<(const Task &a) {
            return v == a.v? t>a.t: v>a.v;
        }
    };
    vector<Task> task;
    int vis[N];
    
    int main()
    {
        ios::sync_with_stdio(false);
        
        while(cin>>n)
        {
            LL ans=0, count=0;
            for(int i=0; i<n; i++) { 
                vis[i]=0;
                LL t,v;
                cin>>t>>v;
                task.push_back(Task{t,v});
            }
            
            sort(task.begin(), task.end());
            for(int i=0; i<n; i++) {
                int t = task[i].t;
                while(vis[t] && t) t--;
                if(!t) continue;
                
                vis[t] = 1;
                ans += task[i].v;
                if(++count == n)
                    break;
            }
    
            cout<<ans<<endl;
            task.clear();
        }
        
        return 0;
    }
    View Code
  • 相关阅读:
    bae问题
    union intersect minus
    在搭建SpringMvc Hibernate框架时遇到的问题
    详细解读Spring2.5 +Struts1.3 框架(使用Spring声明式事物管理和springjjdbc模板)
    javaee缓存技术 oscache ehcache
    Warning
    Math
    Test
    网络流24题-最小路径覆盖问题
    Wannafly summer camp Day3--Knight
  • 原文地址:https://www.cnblogs.com/liubilan/p/9370601.html
Copyright © 2011-2022 走看看