Translate

Views

Showing posts with label UVa. Show all posts
Showing posts with label UVa. Show all posts

Monday, September 9, 2024

Solution UVA: 11428 - Cubes

 

 Problem  VerdictLangTimeBestRankSubmit Time
 | discuss11428 - Cubes AcceptedPython0.0200.00029291 mins ago

Suggest:

61^3 - 60^3 > 10000 so x_max <= 60


r = {}
for x in range(1, 61):
    for y in range(1, x):

        if x**3-y**3 in r:
       
            u, v = r[x**3-y**3]
            if (v > y):
                r[x**3-y**3] = (x,y)
       
        else:
           
            r[x**3-y**3] = (x,y)
while True:
    n = int(input())
    if n == 0:
        break
    if n not in r:
        print('No solution')
    else:
        x, y = r[n]
        print(f"{x} {y}")

Friday, September 6, 2024

Solution UVA: 10878 - Decode the tape

 

 Problem  VerdictLangTimeBestRankSubmit Time
 | discuss10878 - Decode the tape AcceptedPython0.1800.00056151 mins ago

Suggest:

- In the problems decode, i think we should make doing step:

1/ Look overview and get information easy know like

+ input we have 8 char / line -> now we know need use bit
+ number of line = nuimber of char in string -> now we know every line is a present a char of output

2/ Thinking top down problem 

+ A -> 65 -> 01000001 -> |_o___.__o|


_ = input()
while True:
    s = input()
    if s == '___________':
        break

    k = 0
    n = 0
   
    for i in range(len(s)-1, 0, -1):
       
        if s[i] == 'o':
            n += 1 << k
            k += 1
       
        if s[i] == ' ':
            k += 1
           
    print(chr(n), end='')

Sunday, August 25, 2024

Solution Uva: 10338 - Mischievous Children

 

 Problem  VerdictLangTimeBestRankSubmit Time
 | discuss10338 - Mischievous Children AcceptedPython1.0200.00044645 mins ago

Suggest:

1/ RESULT = ALL CASE  - SAME CASE

2/ ALL CASE  = n! (n is length string)

3/ SAME CASE = a! * b! * ... * c! (with a,b,c is number of same chars ) 


from functools import cache

@cache
def fac(n):
    if n == 1:
        return 1
    return fac(n - 1) * n


n = int(input())
for i in range(1, n+1):
    s = input()
   
    d = {}
    for c in s:
        if c not in d:
            d[c] = 1
        else:
            d[c] += 1

    r = fac(len(s))
    for key in d:
        r //= fac(d[key])
    print(f'Data set {i}: {r}')

Monday, August 19, 2024

Solution UVA: 10268 - 498-bis


 Problem  VerdictLangTimeBestRankSubmit Time
 | discuss10268 - 498-bis AcceptedC++110.0200.0007051 mins ago

Susgest:

- Use pow default function couldn't AC so we need code Pow binary function  

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

string line1, line2;

long long Pow(long long a, long long b) {
    if (!b) return 1;
    long long x = Pow(a, b / 2);
    if (b % 2 == 0)
        return x * x;
    else
        return x * x * a;
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    while(getline(cin, line1)){
        getline(cin, line2);
               
        stringstream l1(line1);
        stringstream l2(line2);

        int x;  l1 >> x;
       
        vector<int> a;
        int v;

        while(l2 >> v) a.push_back(v);  
        reverse(a.begin(), a.end());

        int s = 0;
        for(int i=1; i<a.size(); i++)
            s += a[i]*i*Pow(x, i-1);
        cout<<s<<"\n";

    }
}

 

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

Sunday, May 12, 2024

Solution UVA: 378 - Intersecting Lines

 

  Problem  VerdictLangTimeBestRankSubmit Time
 | discuss378 - Intersecting Lines AcceptedC++110.0000.000273314 mins ago

Suggest: (from Udebug)


This is a pretty simple analytical problem. Find the slope and y-intercept of each line, and then solve the following system:

y = m1x + b1

y = m2x + b2

...to get...

x = (b1 - b2) / (m2 - m1)

Substitute back into the previous equations to get y.

Watch out for vertical lines, and handle them separately!


*Note: I get WA if not print '\n' in last

Code:

/* 378 Intersecting Lines
 y = ax + b
 a = a , b = b => //
 a = a, b != b => =
 else => X
*/
#include<bits/stdc++.h>
using namespace std;

struct Point{
    double x, y;
};

struct Line{
   
    //Kind = 1, 2, 3, 4
    //<1> y = ax + b
    //<2> x = ?
    //<3> y = ?
    //<4> Point
   
    int kind;
    double a, b;
    double x, y;
};

Line create_line(Point A, Point B){
    Line AB;
   
    if (A.x == B.x && A.y == B.y){ // AB is Point
       
        AB.kind = 4;
        AB.x = A.x;
        AB.y = B.y;
       
        return AB;
    }
   
    if (A.x == B.x){ // AB is x = ?
        AB.kind = 2;
        AB.x = A.x;
       
        return AB;
    }
   
    if (A.y == B.y){ // AB is y = ?
        AB.kind = 3;
        AB.y = A.y;
       
        return AB;
    }
   
    AB.kind = 1;
    AB.a = (A.y - B.y) / (A.x - B.x);
    AB.b = A.y - AB.a * A.x;
    return AB;      //AB is y = ax + b
}

void find_intersecting(Line AB, Line CD, Point A, Point B, Point C, Point D){
//  cout<<"\n-----------------\n";  
//  cout<<AB.a<<" "<<AB.b<<"\n";
//  cout<<CD.a<<" "<<CD.b<<"\n";
//  cout<<"-----------------\n";
   
    if (AB.kind == 1){ // y = ax + b
   
        if (CD.kind == 1){
           
            //Find point
            double a = AB.a - CD.a;
            double b = CD.b - AB.b;
            double x = b / a;
            double y = AB.a * x + AB.b;
            if (isinf(x) && isinf(y)) cout<<"NONE\n";
            else if(isnan(x) && isnan(y)) cout<<"LINE\n";
                else cout << "POINT "<<fixed<<setprecision(2)<<x<<" "<<y<<"\n";
            return;
        }
       
        if (CD.kind == 2){ // x = ?
           
            double x = CD.x;
            double y = AB.a * x + AB.b;
            cout << "POINT "<<fixed<<setprecision(2)<<x<<" "<<y<<"\n";  
            return;
        }
       
        if (CD.kind == 3){ // y = ?
           
            double y = CD.y;
            double x = (y - AB.b) / AB.a;
            cout << "POINT "<<fixed<<setprecision(2)<<x<<" "<<y<<"\n";  
            return;
        }  
           
    }
   
   
    if (AB.kind == 2){ //x
       
   
        if (CD.kind == 2){ //x
            if (AB.x == CD.x) cout<<"LINE\n";  
            else
                cout<<"NONE\n";
            return;
        }
   
        if (CD.kind == 3){//y
            double x = AB.x;
            double y = CD.y;
            cout << "POINT "<<fixed<<setprecision(2)<<x<<" "<<y<<"\n";
            return;
        }
       
    }
   
    if (AB.kind == 3 && CD.kind == 3){ // y y
        if (AB.y == CD.y) cout<<"LINE\n";  
        else
            cout<<"NONE\n";
        return;
    }
   
    find_intersecting(CD, AB, C, D, A, B); //only 1 call because return
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
   
    cout<<"INTERSECTING LINES OUTPUT\n";
    int t; cin >> t;
    while(t--){
       
        Point A, B, C, D;
        cin >> A.x >> A.y;
        cin >> B.x >> B.y;
        cin >> C.x >> C.y;
        cin >> D.x >> D.y;
       
        Line AB = create_line(A, B);
        Line CD = create_line(C, D);
        find_intersecting(AB, CD, A, B, C, D);
    }
    cout<<"END OF OUTPUT\n";
}

Friday, May 10, 2024

Solution UVA: 191 - Intersection

 

Problem  VerdictLangTimeBestRankSubmit Time
 | discuss191 - Intersection AcceptedC++110.0000.00019422 mins ago

Suggest:

0. https://www.geeksforgeeks.org/program-for-point-of-intersection-of-two-lines/
1. check Point P,Q in ABCD
2. check Line PQ cut Edges //fucking hard implenment


//UVA 191 Intersection
//https://www.geeksforgeeks.org/program-for-point-of-intersection-of-two-lines/
#include <bits/stdc++.h>
#define oo LLONG_MAX
#define int long long
using namespace std;

int t;

struct Point{
    double x, y;
};

struct Equation{ //ax + by = c
    double a, b, c;
};

struct Line{ //y = ax + b
    double a=0, b=0, y= oo, x= oo;  
};

Line create_line(Point P, Point Q){    
    Equation e1;
    e1.a = P.x;
    e1.b = 1;
    e1.c = P.y;
   
    Equation e2;
    e2.a = Q.x;
    e2.b = 1;
    e2.c = Q.y;    
   
    double c = e1.c - e2.c;
    double a = e1.a - e2.a;
    Line PQ;
   
    if (a == 0.0) { // x = a
        PQ.x = e1.a;
    }
    if (c == 0.0) { // y = c
        PQ.y = e1.c;
    }
    if (PQ.x != oo || PQ.y != oo) return PQ;
   
   
    PQ.a = c / a;
    PQ.b = P.y - PQ.a * P.x;
    return PQ;
}

Point P, Q;

bool cut(Line PQ, Line AB, Point A, Point B){//check Line PQ cut Edges
    double x, y, x_min, x_max, y_min, y_max;

    //PQ is a straight
    if (PQ.x != oo && AB.y != oo){
       
        x = PQ.x;
        y = AB.y;
       
        x_min = min(A.x, B.x);
        x_max = max(A.x, B.x);
        y_min = min(P.y, Q.y);
        y_max = max(P.y, Q.y);
       
        return (x_min <= x && x <= x_max) && (y_min <= y && y <= y_max);
    }
    else
    if (PQ.y != oo && AB.x != oo){
   
        y = PQ.y;
        x = AB.x;
       
        x_min = min(P.x, Q.x);
        x_max = max(P.x, Q.x);
        y_min = min(A.y, B.y);
        y_max = max(A.y, B.y);
       
        return (x_min <= x && x <= x_max) && (y_min <= y && y <= y_max);
    }

    //PQ is a cross
    if (AB.x != oo){
       
        x = AB.x;
        y = x*PQ.a + PQ.b;
       
        y_min = min(A.y, B.y);
        y_max = max(A.y, B.y);
        x_min = min(P.x, Q.x);
        x_max = max(P.x, Q.x);
       
        return (x_min <= x && x <= x_max) && (y_min <= y && y <= y_max);
    }
   
    if (AB.y != oo){
       
        y = AB.y;
        x = (y - PQ.b) / PQ.a;
   
        x_min = min(A.x, B.x);
        x_max = max(A.x, B.x);
        y_min = min(P.y, Q.y);
        y_max = max(P.y, Q.y);
       
        return (x_min <= x && x <= x_max) && (y_min <= y && y <= y_max);
    }
}

bool cum(Point P, Point Q, Point A, Point D){ //check Point P,Q in ABCD
    double x_min = min(A.x, D.x);
    double x_max = max(A.x, D.x);
    double y_min = min(A.y, D.y);
    double y_max = max(A.y, D.y);
    return (x_min <= P.x && P.x <= x_max) && (x_min <= Q.x && Q.x <= x_max) && (y_min <= P.y && P.y <= y_max) && (y_min <= Q.y && Q.y <= y_max);
}

int32_t main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
       
    cin >> t;
    while(t--){
        Point A, B, C, D; double ax, ay, cx, cy;
        cin >> P.x >> P.y >> Q.x >> Q.y >> ax >> ay >> cx >> cy;
       
        if (ay > cy){ // give A C
           
            A.x = ax;
            A.y = ay;
            C.x = cx;
            C.y = cy;
           
            B.x = C.x;
            B.y = A.y;
            D.x = A.x;
            D.y = C.y;
        }
        else{//give D B
       
            D.x = ax;
            D.y = ay;
            B.x = cx;
            B.y = cy;
           
            A.x = D.x;
            A.y = B.y;
            C.x = B.x;
            C.y = D.y;
        }
       
       
        Line PQ = create_line(P, Q);
       
        Line AB = create_line(A, B);
        Line BC = create_line(B, C);
        Line CD = create_line(C, D);
        Line AD = create_line(A, D);
   
           
        if (cut(PQ, AB, A, B) ||  cut(PQ, BC, B, C) || cut(PQ, CD, C, D) || cut(PQ, AD, A, D) || cum(P, Q, A, C)) cout<<"T\n";
            else cout<<"F\n";
       
    }
}