QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#566027#9310. Permutation Counting 4oufanWA 2ms8224kbC++174.7kb2024-09-15 22:54:252024-09-15 22:54:29

Judging History

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

  • [2024-09-18 14:56:40]
  • hack成功,自动添加数据
  • (/hack/835)
  • [2024-09-18 14:41:06]
  • hack成功,自动添加数据
  • (/hack/831)
  • [2024-09-17 12:14:52]
  • hack成功,自动添加数据
  • (/hack/825)
  • [2024-09-15 22:54:29]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:8224kb
  • [2024-09-15 22:54:25]
  • 提交

answer

#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;

const int N = 2e5 + 10;

int n;
int a[N];
vector<int>pos[N];
set<int> num;
set<int> s;
set<int> s2;
vector<int> get(vector<int>& vec)
{
    vector<int> vi = vec;
    sort(vi.begin(), vi.end());
    vi.erase(unique(vi.begin(), vi.end()), vi.end());
    unordered_map<int, int> mp;
    for (int i = 0; i < vi.size(); i++)
    {
        mp[vi[i]] = i;
    }
    vector<int> res(vec.size());
    for (int i = 0; i < vec.size(); i++)
    {
        res[i] = mp[vec[i]];
    }
    return res;
}
void solve()
{
    cin >> n;
    num.clear();
    s.clear();
    s2.clear();
    vector<int>tmp;
    for (int i = 1;i <= n;i++)
    {
        int x;cin >> x;
        tmp.push_back(x);
    }
    tmp = get(tmp);
    int mx = 0;
    for (int i = 1;i <= n;i++)
    {
        a[i] = tmp[i - 1];
        mx = max(mx, a[i]);
        num.insert(-a[i]);
    }
    for (int i = 0;i <= mx;i++)pos[i].clear();
    for (int i = 1; i <= n; i++)
    {
        pos[a[i]].push_back(i);
    }
    int ans = 0;
    int cnt = 0;
    for (auto& it : num)
    {
        int t = -it;
        for (int i = 0;i < pos[t].size();i++)
        {
            s.insert(pos[t][i]);
            s2.insert(-pos[t][i]);
        }
        int lst = -1;
        for (int i = 0;i < pos[t].size();i++)
        {
            int p = pos[t][i];
            auto it_1 = upper_bound(s.begin(), s.end(), p);
            auto it_2 = upper_bound(s2.begin(), s2.end(), -p);
            if (it_1 != s.end())
            {
                if (it_2 != s2.end())
                {
                    if (-*(it_2) != lst)
                    {
                        ans += p + *(it_2)-1;
                        ans += *(it_1)-p - 1;
                    }
                    else ans += *(it_1)-p - 1;
                }
                else
                {
                    ans += p - 1;
                    ans += *(it_1)-p - 1;
                }
            }
            else
            {
                if (it_2 != s2.end())
                {
                    if (-*(it_2) != lst)
                    {
                        ans += p + *(it_2)-1;
                    }
                    ans += n - p;
                }
                else
                {
                    ans += n - p;
                    ans += p - 1;
                }
            }
            lst = p;
        }
    }
    cout << ans << endl;
}
void solve2()
{
    cin >> n;
    num.clear();
    s.clear();
    s2.clear();
    vector<int>tmp;
    for (int i = 1;i <= n;i++)
    {
        int x;cin >> x;
        tmp.push_back(x);
    }
    tmp = get(tmp);
    int mx = 0;
    for (int i = 1;i <= n;i++)
    {
        a[i] = tmp[i - 1];
        mx = max(mx, a[i]);
        num.insert(-a[i]);
    }
    for (int i = 0;i <= mx;i++)pos[i].clear();
    for (int i = 1; i <= n; i++)
    {
        pos[a[i]].push_back(i);
    }
    int ans = 0;
    int cnt = 0;
    int tot = 0;
    for (auto it : num)
    {
        int t = -it;
        for (int i = 0;i < pos[t].size();i++)
        {
            s.insert(pos[t][i]);
            s2.insert(-pos[t][i]);
            tot++;
            tot++;
        }
        int lst = -1;
        for (int i = 0;i < pos[t].size();i++)
        {
            int p = pos[t][i];
            auto it_1 = s.upper_bound(p);
            auto it_2 = s2.upper_bound(-p);
            tot++;
            tot++;
            if (it_1 != s.end())
            {
                if (it_2 != s2.end())
                {
                    if (-*(it_2) != lst)
                    {
                        ans += p + *(it_2)-1;
                        ans += *(it_1)-p - 1;
                    }
                    else ans += *(it_1)-p - 1;
                }
                else
                {
                    ans += p - 1;
                    ans += *(it_1)-p - 1;
                }
            }
            else
            {
                if (it_2 != s2.end())
                {
                    if (-*(it_2) != lst)
                    {
                        ans += p + *(it_2)-1;
                    }
                    ans += n - p;
                }
                else
                {
                    ans += n - p;
                    ans += p - 1;
                }
            }
            lst = p;
        }
    }
    cout << ans << endl;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin >> T;
    while (T--)
    {
        solve2();
    }
    return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 8224kb

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:

6
1
1
0

result:

wrong answer 1st words differ - expected: '0', found: '6'