UVa 11389 – The Bus Driver Problem

#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    int n,d,r,i,overtime,temp,morning[105],evening[105];

    while (true){
        cin>>n>>d>>r;
        if (!n || !d || !r)return 0;

        for (i = 0 ; i < n ; i ++)
            cin>>morning[i];
        for (i = 0 ; i < n ; i ++)
            cin>>evening[i];

        sort(morning,morning+n);
        sort(evening,evening+n);

        overtime = 0;
        for (i = 0 ; i < n ; i ++){
            temp = morning[i]+evening[n-i-1];
            if (temp > d)
                overtime+=r*(temp-d);

        }
        cout<<overtime<<endl;

    }

    return 0;
}
Advertisements

UVa 11567 – Moliu Number Generator

#include <cstdio>
#include <algorithm>

using namespace std;

unsigned int solve (unsigned int i){

    unsigned int count = 1;
    while (i>1){
        if ((i&1)>0)
            return (count  + min(solve(i+1),solve(i-1)));
        count+=1+(1&i);
        i>>=1;
    }
    return count;
}

int main()
{
    unsigned int n;
    while (scanf("%d",&n)==1){
        if (n)
            printf("%d\n",solve(n));
        else
            printf("0\n");

    }

}

UVa 574 – Sum It Up


#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int n,t,nums[13];
map <string,char> mymap ;

char* itoa(int val, int base){
    static char buf[32] = {0};
    int i = 30;
    for(; val && i ; --i, val /= base)
        buf[i] = "0123456789abcdef"[val % base];
    return &buf[i+1];
}

void solve(int i, int sum,string collect){
    if (sum == t){
        string T = collect.substr(0,collect.length()-1);
        if (mymap.find(T) == mymap.end()){
            cout<<T<<endl;
            mymap[T]++;
        }
    }else if (sum > t)
        return ;
    else for (int j = i ; j > -1;j--){
        solve(j-1,sum+nums[j],collect+itoa(nums[j],10)+"+");
    }
}

int main()
{
    while (cin>>t>>n &&n&&t){
        for (int i = 0 ; i < n; i++){
            cin>>nums[i];
        }
        cout<<"Sums of "<<t<<":"<<endl;
        sort(nums,nums+n);
        mymap.clear();
        solve(n - 1,0,"");
        if (!mymap.size())
            cout<<"NONE"<<endl;
    }
}