zoukankan      html  css  js  c++  java
  • UVA11729 Commando War

    题目链接

    分析:

    自己写的时候是直接模拟的,但似乎训练指南上的解法更好。

    偶的代码:

    View Code
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    
    const int maxn = 1010;
    
    struct Job{
        int b, j;
    }a[maxn];
    
    int cmp(const Job &x, const Job &y){
        return x.j > y.j;
    }
    
    int main(){
        int n, cnt=0;
        while(cin>>n){
            cnt++;
            if(n == 0) break;
            for(int i=0; i<n; i++) cin>>a[i].b>>a[i].j;
            sort(a, a+n, cmp);
    
            int ans = 0, t=0;
            for(int i=0; i<n; i++){
                if(t < a[i].b){
                    ans += a[i].b - t;
                    ans += a[i].j;
                    t = a[i].j;
                }
                else{
                    t -= a[i].b;
                    if(a[i].j > t){
                        ans += a[i].j - t;
                        t = a[i].j;
                    }
                }
            }
            cout<<"Case "<<cnt<<": "<<ans<<endl;
        }
        return 0;
    }

    训练指南上的代码(并不是和书上一样,只是按着思路来写的):

    View Code
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    
    const int maxn = 1010;
    
    struct Job{
        int b, j;
    }a[maxn];
    
    int cmp(const Job &x, const Job &y){
        return x.j > y.j;
    }
    
    int main(){
        int n, cnt=0;
        while(cin>>n){
            cnt++;
            if(n == 0) break;
            for(int i=0; i<n; i++) cin>>a[i].b>>a[i].j;
            sort(a, a+n, cmp);
    
            int ans = 0, s=0;
    
            for(int i=0; i<n; i++){
                s += a[i].b;
                ans = max(ans, s+a[i].j);
            }
    
            cout<<"Case "<<cnt<<": "<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Android 动画-alpha(渐变透明度动画效果)
    Memento(备忘录)
    Mediator(中介者)
    Iterator(迭代器)
    Command(命令)
    Chain of Responsibility(责任链)
    Template Method(模板方法)
    Interpreter(解释器)
    Proxy(代理)
    Flyweight(享元)
  • 原文地址:https://www.cnblogs.com/tanhehe/p/3023861.html
Copyright © 2011-2022 走看看