QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#634141 | #9310. Permutation Counting 4 | klhwong# | WA | 0ms | 3852kb | C++17 | 1.7kb | 2024-10-12 16:45:32 | 2024-10-12 16:45:34 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
int t{}, n{}, temp{};
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> t;
while(t--) {
cin >> n;
stack<pair<int, int>> stk;
int answer = 0;
for (int i = 0; i < n; i++) {
cin >> temp;
if (stk.empty()) {
stk.push({temp, 1});
}
else if (stk.top().first == temp) {
// Merge with the previous group of same elements
int cnt = stk.top().second + 1;
stk.pop();
stk.push({temp, cnt});
}
else if (stk.top().first < temp) {
int add = 0;
int cnt = 1;
// Corrected here: Remove 'add += add;'
while (!stk.empty() && stk.top().first < temp) {
add += stk.top().second; // Add only once per popped element
cnt += stk.top().second;
stk.pop();
}
answer += add;
// Merge with existing same elements after popping smaller ones
while (!stk.empty() && stk.top().first == temp) {
cnt += stk.top().second;
stk.pop();
}
stk.push({temp, cnt});
}
else {
stk.push({temp, 1});
}
}
int add = 0;
// Corrected here: Remove 'add += add;'
while (stk.size() > 1) {
add += stk.top().second; // Add only once per popped element
stk.pop();
}
cout << answer + add << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3852kb
input:
4 5 1 2 1 5 1 2 1 2 2 2 5 1 1 2 4 2 3 5 5 3 4 5 3 5 1 2 3 4 3 5 3 3 5 1 5 1 4 4 5 5 5 1 2
output:
5 1 1 0
result:
wrong answer 1st words differ - expected: '0', found: '5'