• JSOI2007 建筑抢修(贪心)


    满分做法:

    按照结束时间从小到大排序,一个一个进行处理。遇到当前时间+处理时间>结束时间的建筑时,把这个建筑和之前修理过的建筑中处理时间最大的进行比较。

    如果当前处理时间小于最大值,那么可以进行替换,使当前时间变小,否则就放弃此建筑。剩下的就是直接加进来就可以了。

    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    #include<vector>
    using namespace std;
    typedef long long ll;
    const int maxm=150007;
    int n;
    struct node
    {
     ll t,ed;
     bool operator < (const node &s) const
     {
     	return ed<s.ed;
     }
    }a[maxm];
    priority_queue<ll>q;
    ll k=1;
    int ans; 
    int main()
    {
     scanf("%d",&n);
     for(int i=1;i<=n;i++)
     scanf("%lld%lld",&a[i].t,&a[i].ed);
     sort(a+1,a+n+1); 
     for(int i=1;i<=n;i++)
     {
       if(k+a[i].t>a[i].ed)
       {
       	if(a[i].t<q.top())//找之前最大的且小于这个值的进行替换 
    	{ 
         k-=q.top();
         q.pop();
         k+=a[i].t;
         q.push(a[i].t);
        }
       }
       else
       {
       	 k+=a[i].t;
       	 ans++;
       	 q.push(a[i].t);
       }
     }
     printf("%d
    ",ans);
     return 0;
    }
    
  • 相关阅读:
    C#微信开发
    3-4:字符串方法
    2-4-1 元组
    2-3-3 列表方法
    2-2-3:序列(字符串)乘法(p32)
    3-3字符串格式化(p47)
    2-2:分片
    2-1:Print date(p28)
    old.2.三次登录机会
    old.2.sum(1-2+3-4+...+99)
  • 原文地址:https://www.cnblogs.com/lihan123/p/11674069.html
Copyright © 2020-2023  润新知