QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#634141#9310. Permutation Counting 4klhwong#WA 0ms3852kbC++171.7kb2024-10-12 16:45:322024-10-12 16:45:34

Judging History

你现在查看的是最新测评结果

  • [2024-10-12 16:45:34]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3852kb
  • [2024-10-12 16:45:32]
  • 提交

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'