• UVA 11729


    UVA 11729

    大体是贪心做法,执行时间长的先交代

    训练指南原题解

    #include<cstdio>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    
    struct Job 
    {
    	int j, b;
    	bool operator < (const Job& x) const 
    	{
    		return j > x.j;
    	}
    };
    
    int main() 
    {
    	int n, b, j, kase = 1;
    	while(scanf("%d", &n) == 1 && n) 
    	{
    		vector<Job> v;
    		for(int i = 0; i < n; i++) 
    		{
    			scanf("%d%d", &b, &j); 
    			v.push_back((Job){j,b});
    		}
    		sort(v.begin(), v.end());
    
    		int s = 0;
    		int ans = 0;
    
    		for(int i = 0; i < n; i++)
    		{
    			s += v[i].b;
    			ans = max(ans, s+v[i].j);
    		}
    		printf("Case %d: %d
    ", kase++, ans);
    	}
    	return 0;
    }
    
    

    用cmp函数进行排序而非重载运算符

    #include<cstdio>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    
    struct Job 
    {
    	int j, b;
    	bool operator < (const Job& x) const 
    	{
    		return j > x.j;
    	}
    };
    
    int main() 
    {
    	int n, b, j, kase = 1;
    	while(scanf("%d", &n) == 1 && n) 
    	{
    		vector<Job> v;
    		for(int i = 0; i < n; i++) 
    		{
    			scanf("%d%d", &b, &j); 
    			v.push_back((Job){j,b});
    		}
    		sort(v.begin(), v.end());
    
    		int s = 0;
    		int ans = 0;
    
    		for(int i = 0; i < n; i++)
    		{
    			s += v[i].b;
    			ans = max(ans, s+v[i].j);
    		}
    		printf("Case %d: %d
    ", kase++, ans);
    	}
    	return 0;
    }
    
    
    透过泪水看到希望
  • 相关阅读:
    连接池——tomcat自带连接池。
    jsp小记
    结合properties配置文件优化jdbc连接工具类
    StringBuffer
    多重继承关系初始化顺序及初始化
    Error
    算法:插入排序
    算法:冒泡排序
    算法:选择排序
    注册Activity
  • 原文地址:https://www.cnblogs.com/ronnielee/p/9495967.html
Copyright © 2020-2023  润新知