HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Timus Online Judge Problem 1001 - "Reverse Root"

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
problemtimusreversejudge1001onlineroot

Problem

The problem statement can be found here.

Description (short):

Input

The input stream contains a set of integer numbers \$A_i\$ (\$0 \le A_i \le 10^{18}\$). The numbers are separated by any number of spaces and line breaks. A size of the input stream does not exceed 256 KB.

Output

For each number \$A_i\$ from the last one until the first one, you should output its square root. Each square root should be printed in a separate line with at least four digits after the decimal point.

#include 
#include 
#include 
using namespace std;

int main()
{
    double t;
    double v [128 * 1024];
    int idx = 0;
    while (cin >> t) {
        v[idx] = sqrt(t);
        ++idx;
    }

    cout = 0; --i)
        cout << v[i] << endl;

    return 0;
}


I am encountering a TLE on test case 9, with an execution time of 2.031s and memory usage of 1 474 KB. (The allowable limits are 2.0s and 64 MB.) How can I improve (or change) my method to complete the problem successfully?

Solution

The input stream contains a set of integer numbers \$A_i\$ (\$0 \leq A_i \leq 10^{18}\$).

So you should change

double t;


to

unsigned long long t;


This actually speeds up the program a lot.

To speed up the output, instead of

cout = 0; --i)
    cout << v[i] << endl;


Try

for (int i = idx - 1; i >= 0; --i) {
    printf("%.4f\n", v[i]);
}

Code Snippets

unsigned long long t;
cout << fixed;
cout << setprecision(4);

for (int i = idx - 1; i >= 0; --i)
    cout << v[i] << endl;
for (int i = idx - 1; i >= 0; --i) {
    printf("%.4f\n", v[i]);
}

Context

StackExchange Code Review Q#78279, answer score: 5

Revisions (0)

No revisions yet.