Translate

Views

Thursday, June 20, 2024

Solution UVA: 11413 - Fill the Containers

 

 Last Submissions
  Problem  VerdictLangTimeBestRankSubmit Time
 | discuss11413 - Fill the Containers AcceptedC++110.0000.00019725 mins ago


Suggest:
- Use binary search to determine the capacity (Once the capacity is determined, simulate and calculate the number of containers)

Note: I use a binary search algorithm along with a while + a for.

#include<bits/stdc++.h>
using namespace std;

int n, m;
int a[1000111];

int main(){
    while(cin >> n >> m){
       
        m = min(n, m);
       
        for(int i=1; i<=n; i++)
            cin >> a[i];
       
        int r = 0;
        for(int i=1; i<=n; i++)
            r += a[i];  
       
       
        int l= 1;
       
        while(r-l+1 >= 3){
           
            int mid = (r + l) / 2;
            int s = 0;
            int container= 0;
            bool bl = false;
           
            for(int i=1; i<=n; i++)
            if (a[i] + s <=  mid){
                s += a[i];  
                bl = true;
            }
            else{
                if (not bl) {
                    container = m + 1;
                    break;  
                }
                if (bl) container++;
                bl = false;
                s = 0;
                i--;
            }
           
            if (bl) container++;
           
        //  cout<<"mid container: "<<mid<<" "<<container<<"\n";
           
            if (container > m) l = mid;
            else
                r = mid;
        }
       
        for(int mid=l; mid<=r; mid++){
           
            int s = 0;
            int container= 0;
            bool bl = false;
           
            for(int i=1; i<=n; i++)
            if (a[i] + s <=  mid){
                s += a[i];  
                bl = true;
            }
            else{
                if (not bl) {
                    container = m + 1;
                    break;  
                }
                if (bl) container++;
                bl = false;
                s = 0;
                i--;
            }
           
            if (bl) container++;
           
            if (container <= m){
                cout << mid <<"\n";
                break;
            }          
           
        }
       
       
    }
   
       
}

Sunday, June 2, 2024

Solution UVA: 10114 - Loansome Car Buyer

 

 Problem  VerdictLangTimeBestRankSubmit Time
 | discuss10114 - Loansome Car Buyer AcceptedPython0.0200.00057521 mins ago


Suggest:


You only need forcus: (for output 1)

Consider the first example below of borrowing $15,000 for 30 months. As the buyer drives off the

lot, he still owes $15,000, but the car has dropped in value by 10% to $13,950. After 4 months, the

buyer has made 4 payments, each of $500, and the car has further depreciated 3% in months 1 and 2

and 0.2% in months 3 and 4. At this time, the car is worth $13,073.10528 and the borrower only owes

$13,000 

AND note

month_pay = loan / months (15000 / 30 = 500) 

I lost many time because think down_payment is month_payment ($500)



while True:
    months, down_pay, loan, n = map(float, input().split())
    months = int(months)
    n = int(n)

    if months < 0:
        break

    depreciations = [-1]*(months+1)  
    for _ in range(n):
        month, depreciation = map(float, input().split())
        month = int(month)
        depreciations[month] = depreciation

   
    car_money = (down_pay + loan)*(1-depreciations[0])
    owe = loan  
    if (owe < car_money):
        print("0 months")
    else:
        month_pay = loan / months
        for i in range(1, months+1):
            if depreciations[i] == -1:
                depreciations[i] = depreciations[i-1]
            owe -= month_pay
            car_money -= car_money * depreciations[i]
           
            if (owe < car_money):
                if (i>1):
                    print(i, 'months')
                else:
                    print(i, 'month')
                break      
   
# 30 500.0 15000.0 3
# 0 .10
# 1 .03
# 3 .002
# 12 500.0 9999.99 2
# 0 .05
# 2 .1
# 60 2400.0 30000.0 3
# 0 .2
# 1 .05
# 12 .025
# -99 0 17000 1