zoukankan      html  css  js  c++  java
  • bzoj 1029 贪心

    贪心的一种,维护一种尽可能优的状态(即不会比最优解差),将这种状态保持到最后。

     1 /**************************************************************
     2     Problem: 1029
     3     User: idy002
     4     Language: C++
     5     Result: Accepted
     6     Time:400 ms
     7     Memory:2368 kb
     8 ****************************************************************/
     9  
    10 #include <cstdio>
    11 #include <queue>
    12 #include <algorithm>
    13 #define maxn 150010
    14 using namespace std;
    15  
    16 int n, cnt, ctm;
    17 priority_queue<int> heap;
    18  
    19 struct Pair {
    20     int t1, t2;
    21     bool operator<( const Pair & b ) const {
    22         return t2<b.t2;
    23     }
    24 };
    25 Pair jobs[maxn];
    26  
    27 int main() {
    28     scanf( "%d", &n );
    29     for( int i=1; i<=n; i++ ) 
    30         scanf( "%d%d", &jobs[i].t1, &jobs[i].t2 );
    31     sort( jobs+1, jobs+1+n );
    32     cnt = ctm = 0;
    33     for( int i=1; i<=n; i++ ) {
    34         if( jobs[i].t1+ctm <= jobs[i].t2 ) {
    35             heap.push( jobs[i].t1 );
    36             cnt++;
    37             ctm += jobs[i].t1;
    38         } else {
    39             if( jobs[i].t1 < heap.top() ) {
    40                 cnt--;
    41                 ctm -= heap.top();
    42                 heap.pop();
    43                 heap.push( jobs[i].t1 );
    44                 cnt++;
    45                 ctm += jobs[i].t1;
    46             }
    47         }
    48     }
    49     printf( "%d
    ", cnt );
    50 }
    View Code
  • 相关阅读:
    C#Web网站的创建
    C#Repeater控件的使用
    C#LINQ
    C#异常处理
    C#LINQ
    C#匿名委托,匿名函数,lambda表达式
    C#chart图表的应用
    C#用户控件的使用
    用 Python写 daemon
    CentOS 5.4 制作 Python 2.6 RPM 包的方法
  • 原文地址:https://www.cnblogs.com/idy002/p/4300024.html
Copyright © 2011-2022 走看看