-
Notifications
You must be signed in to change notification settings - Fork 156
Added Tutorial LOJ-1234 #456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rebornplusplus
merged 4 commits into
lightoj-dev:main
from
Talha-Taki002:Feature-LOJ-1234
May 28, 2023
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# LOJ 1234 - Harmonic Number | ||
|
||
## Solution | ||
From the definition of H<sub>n</sub> we perceive: **H<sub>n+1</sub> = H<sub>n</sub> + (1/ n)** | ||
|
||
So we need not to calculate **H<sub>n</sub>** using the straighforward formula for every single test case. We go from non-decreasing order of test cases whilst keeping a variable for harmonic sum so far. This way not only we save time but memory as well, because there's no need for storing all the harmonic values beforehand. | ||
|
||
## Complexity | ||
Talha-Taki002 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- Time Complexity: O($T$). | ||
Talha-Taki002 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- Memory Complexity: O($T$). | ||
|
||
## Code | ||
|
||
### C++ | ||
Talha-Taki002 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```cpp | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
|
||
typedef pair <int, int> pii; | ||
Talha-Taki002 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
int main() { | ||
|
||
// For fast I/O | ||
ios_base::sync_with_stdio(false); | ||
cin.tie(nullptr); | ||
|
||
const int MAXN = 1e8; | ||
|
||
int t; | ||
cin >> t; | ||
|
||
// 1-based indexing | ||
vector <pii> N(t+1); | ||
for(int i = 1; i <= t; ++i) { | ||
cin >> N[i].first; | ||
// Saving corresponding index of the test case | ||
N[i].second = i; | ||
} | ||
sort(N.begin(), N.end()); | ||
|
||
vector <double> ans(t+1); | ||
double now = 0; | ||
int idx = 1; | ||
for(int i = 1; i <= MAXN; ++i) { | ||
now += 1. / i; | ||
Talha-Taki002 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
while (idx < (int)N.size() && N[idx].first == i) { // Could be duplicates, that's why using 'while' loop | ||
Talha-Taki002 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ans[N[idx++].second] = now; | ||
} | ||
} | ||
|
||
for(int ts = 1; ts <= t; ++ts) { | ||
cout << fixed << setprecision(10) << "Case " << ts << ": " << ans[ts] << '\n'; | ||
} | ||
|
||
return 0; | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.