constructive algorithms geometry

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    l0=list(map(int,input().split()))
    n,d,h=l0[0],l0[1],l0[2]
    l=list(map(int,input().split()))
    ans=0
    area=d*h/2
    for i in range(len(l)-1):
        if l[i+1]-l[i]<h:
            delta=l[i+1]-l[i]
            ans+=(area-(area)*(1-delta/h)**2)
        else:
            ans+=area
    print(ans+area)

C++ Code:

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

int main(){
    int t;
    cin>>t;
    while(t--){
    int n;
    double h;
    double d;
    cin>>n;
    cin>>d;
    cin>>h;
    double k[n];
    for(int i=0;i<n;i++){
        cin>>k[i];
    }
    double area=(n*h*d)/2;
    double sub;
    for(int i=1;i<n;i++){
        if((k[i]-k[i-1])<h){
            sub=(double)((k[i-1]+h-k[i])*d*(k[i-1]+h-k[i]))/(2*h);
            area=area-sub;
        }
    }
    cout<<setprecision(10)<<fixed;
    cout<<area<<endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles