Sorting by Shelves: Numbers हा एक ब्राउझर-आधारित कोडे गेम आहे, जो खेळाडूंना मर्यादित शेल्फवर क्रमांकित चेंडूंना व्यवस्थित मांडण्याचे आव्हान देतो. प्रत्येक शेल्फमध्ये फक्त तीन चेंडू बसतात आणि तुम्हाला ते एकतर सारख्या क्रमांकांचे तीन चेंडू जुळवून किंवा त्यांना लक्ष्यित बेरजेपर्यंत पोहोचण्यासाठी एकत्र करून साफ करावे लागतात. हा गेम या दोन मोड्समध्ये बदलत राहतो, ज्यासाठी तार्किक नियोजन आणि जलद अंकगणित दोन्ही आवश्यक आहे. त्याची मिनिमलिस्ट रचना रणनीतीवर लक्ष केंद्रित करते, तर गुळगुळीत कार्यक्षमता डेस्कटॉप आणि मोबाइलवर खेळण्याची क्षमता सुनिश्चित करते. कोणत्याही डाउनलोडची आवश्यकता नसल्यामुळे, गणिताची कौशल्ये वाढवण्यासाठी आणि मेंदू प्रशिक्षणाच्या लहान सत्रांचा आनंद घेण्यासाठी हा एक विनामूल्य, सुलभ मार्ग आहे. Y8 वर गेम खेळा - सर्वात मोठे आधुनिक HTML5 गेमिंग प्लॅटफॉर्म!
शेल्फ्सनुसार क्रमवारी: संख्या हा एक ब्राउझर-आधारित कोडे आणि गणिताचा खेळ आहे. यात तार्किक मांडणी आणि अंकगणितीय आव्हानांचा संगम आहे: तुम्ही क्रमांकित चेंडू अशा शेल्फ्सवर ठेवता, जे एका वेळी फक्त तीनच चेंडू ठेवू शकतात. नंतर तुम्ही ते एकतर समान संख्यांचे त्रिकूट बनवून किंवा लक्ष्य बेरीज गाठण्यासाठी त्यांची बेरीज करून रिकामे करता. खेळाचे बदलणारे प्रकार खेळाला ताजेतवाने ठेवतात, तर किमान डिझाइन आणि जलद फेऱ्या याला प्रासंगिक बुद्धी प्रशिक्षणासाठी आदर्श बनवतात.
To solve this problem, we need to count all contiguous subsequences where the number of elements is equal to the number of distinct elements. This condition implies that all elements within such a contiguous subsequence must be distinct. If any element appears more than once, the number of distinct elements would be strictly less than the total number of elements.
We can solve this efficiently using a sliding window approach. We'll maintain a window [left, right] (inclusive) and a std::unordered_set to keep track of the distinct elements within the current window.
Here's the algorithm:
1. Initialize a long long variable count to 0. This variable will store the total number of valid contiguous subsequences.
2. Initialize a left pointer to 0. This marks the beginning of our current valid window.
3. Initialize an empty std::unordered_set<int> named seen. This set will store the unique elements currently within the window [left, right].
4. Iterate with a right pointer from 0 to n-1 (where n is the length of the input array). For each right element a[right]:
a. Check for duplicates: While a[right] is already present in the seen set, it means we have a duplicate in our current window [left, right]. To resolve this, we need to shrink the window from the left:
i. Remove the element a[left] from the seen set.
ii. Increment the left pointer.
b. Add current element: After the while loop, a[right] is guaranteed to be distinct within the window [left, right]. Add a[right] to the seen set.
c. Count subsequences: At this point, the window [left, right] contains only distinct elements. Any contiguous subsequence starting from left and ending at right, or starting from left+1 and ending at right, and so on, up to starting at right and ending at right, will also contain only distinct elements. The number of such valid subsequences ending at right is (right - left + 1). Add this value to count.
right pointer has traversed the entire array, count will hold the total number of contiguous subsequences with distinct elements. Print count.Example Walkthrough: A = [1, 2, 1, 3]
n = 4, count = 0, left = 0, seen = {}
right = 0 (element a[0] = 1):
1 is not in seen.1 to seen. seen = {1}.count += (0 - 0 + 1) = 1. count = 1. (Subsequence: [1])right = 1 (element a[1] = 2):
2 is not in seen.2 to seen. seen = {1, 2}.count += (1 - 0 + 1) = 2. count = 1 + 2 = 3. (Subsequences: [2], [1, 2])right = 2 (element a[2] = 1):
1 is in seen.while loop starts:
a[left] (a[0] = 1) from seen. seen = {2}.left. left = 1.a[2] = 1 is not in seen. while loop ends.1 to seen. seen = {2, 1}.count += (2 - 1 + 1) = 2. count = 3 + 2 = 5. (Subsequences: [1] (from index 2), [2, 1])right = 3 (element a[3] = 3):
3 is not in seen.3 to seen. seen = {2, 1, 3}.count += (3 - 1 + 1) = 3. count = 5 + 3 = 8. (Subsequences: [3], [1, 3], [2, 1, 3])Loop ends. Final count = 8.
Time Complexity:
The right pointer iterates from 0 to n-1 once. The left pointer also iterates from 0 to n-1 at most once (it only moves forward). Each element is inserted into and removed from the unordered_set at most once. unordered_set operations (insert, erase, count) take average O(1) time. Therefore, the overall time complexity is O(N) on average.
Space Complexity:
The unordered_set stores at most N distinct elements in the worst case (e.g., if all elements are distinct). Thus, the space complexity is O(N).
Given the constraints (N <= 10^5, a[i] up to 10^9), an O(N) solution with a hash set is efficient enough. The long long type for count is necessary because for N=10^5, the maximum possible count is N*(N+1)/2 which is approximately 5 * 10^9.
```cpp
int main() {
// Optimize C++ standard streams for faster input/output.
std::iosbase::syncwith_stdio(false);
std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector a(n);
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
}
long long count = 0;
int left = 0; // Left pointer of the sliding window
std::unordered_set seen; // Stores distinct elements in the current window [left, right]
// Right pointer of the sliding window
for (int right = 0; right < n; ++right) {
// If the element at 'right' is already in our 'seen' set,
// it means we have a duplicate. We need to shrink the window
// from the left until the duplicate is removed.
while (seen.count(a[right])) {
seen.erase(a[left]); // Remove the element at 'left' from the set
left++; // Shrink the window from the left
}
// Now, a[right] is guaranteed to be distinct within the window [left, right].
seen.insert(a[right]); // Add the current element to the set
// The number of distinct contiguous subarrays ending at 'right'
// and starting from 'left' up to 'right' is (right - left + 1).
// Each of these subarrays contains only distinct elements.
count += (right - left + 1);
}
std::cout << count << std::endl;
return 0;
}
```
हे मजेदार आहे कारण ते मानसिक कसरत आणि खेळकर आव्हानाचे मिश्रण असल्यासारखे वाटते, जे तुम्हाला जलद झटक्यात आराम आणि उत्तेजना दोन्ही देते.