zoukankan      html  css  js  c++  java
  • Luogu1868 饥饿的奶牛 (动态规划)

    开始以为是贪心,10分;想了个DP估计会超时,一翻题解各路初中神仙,背包都有。
    (n^2)很好想,考虑单调性用二分优化出log

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
    #define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
    #define Max(a,b) ((a) > (b) ? (a) : (b))
    #define Min(a,b) ((a) < (b) ? (a) : (b))
    #define Fill(a,b) memset(a, b, sizeof(a))
    #define Abs(a) ((a) < 0 ? -(a) : (a))
    #define Swap(a,b) a^=b^=a^=b
    #define ll long long
    
    #define ON_DEBUG
    
    #ifdef ON_DEBUG
    
    #define D_e_Line printf("
    
    ----------
    
    ")
    #define D_e(x)  cout << #x << " = " << x << endl
    #define Pause() system("pause")
    #define FileOpen() freopen("in.txt","r",stdin);
    
    #else
    
    #define D_e_Line ;
    #define D_e(x)  ;
    #define Pause() ;
    #define FileOpen() ;
    
    #endif
    
    struct ios{
        template<typename ATP>ios& operator >> (ATP &x){
            x = 0; int f = 1; char c;
            for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
            while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
            x*= f;
            return *this;
        }
    }io;
    using namespace std;
    
    const int N = 150007;
    
    struct Field{
    	int l, r, w;
    	bool operator < (const Field &com) const{
    		if(r != com.r) return r < com.r;
    		return l < com.l;
    	}
    }a[N];
    
    int f[N];
    
    inline int Find(int x){
    	int l = 1, r = x;
    	int ans = -1;
    	while(l <= r){
    		int mid = (l + r) >> 1;
    		if(a[mid].r < a[x].l){
    			l = mid + 1;
    			ans = mid;
    		}
    		else{
    			r = mid - 1;
    		}
    	}
    	return ans;
    }
    int main(){
    	//FileOpen();
    	int n;
    	io >> n;
        R(i,1,n){
        	io >> a[i].l >> a[i].r;
        	a[i].w = a[i].r - a[i].l + 1;
        }
    		
    
        sort(a + 1, a + n + 1);
        
        f[1] = a[1].r - a[1].l + 1;
        R(i,2,n){
            int j = Find(i);
            if(j != -1)
            	f[i] = Max(f[i - 1], f[j] + a[i].w);
            else
            	f[i] = Max(f[i - 1], a[i].w);
        }
        
        printf("%d
    ",f[n]);
        
        return 0;
    }
    

  • 相关阅读:
    zoj 2316 Matrix Multiplication 解题报告
    BestCoder7 1001 Little Pony and Permutation(hdu 4985) 解题报告
    codeforces 463C. Gargari and Bishops 解题报告
    codeforces 463B Caisa and Pylons 解题报告
    codeforces 463A Caisa and Sugar 解题报告
    CSS3新的字体尺寸单位rem
    CSS中文字体对照表
    引用外部CSS的link和import方式的分析与比较
    CSS样式表引用方式
    10个CSS简写/优化技巧
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11221301.html
Copyright © 2011-2022 走看看