QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#860#568701#9310. Permutation Counting 4N_z_KJJDFailed.2024-09-20 19:22:452024-09-20 19:22:46

Details

Extra Test:

Accepted
time: 2881ms
memory: 32064kb

input:

1
1000000
1 1
1 2
2 3
1 4
2 5
3 6
4 7
1 8
2 9
3 10
4 11
5 12
6 13
7 14
8 15
1 16
2 17
3 18
4 19
5 20
6 21
7 22
8 23
9 24
10 25
11 26
12 27
13 28
14 29
15 30
16 31
1 32
2 33
3 34
4 35
5 36
6 37
7 38
8 39
9 40
10 41
11 42
12 43
13 44
14 45
15 46
16 47
17 48
18 49
19 50
20 51
21 52
22 53
23 54
24 55
25...

output:

1

result:

ok "1"

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#568701#9310. Permutation Counting 4KJJDAC ✓980ms33536kbC++201.7kb2024-09-16 17:49:442024-09-18 14:59:20

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int N = 1e6 + 7;
#define lc p << 1
#define rc p << 1 | 1
// mt19937_64 rng{chrono::steady_clock::now().time_since_epoch().count()};//随机数
ll a[N], b[N];
void slove()
{
    ll n, m, k;
    cin >> n;
    vector<pii> p(n + 1);
    priority_queue<pii, vector<pii>, greater<pii>> q;
    for (int i = 1; i <= n; i++)
        cin >> p[i].first >> p[i].second, q.push(p[i]);
    // priority_queue<pii,vector<pii>,greater<pii>>q_m;
    vector<pii> ans;
    for (int i = 1; i <= n; i++)
    {
        vector<pii> tmp;
        while (!q.empty() && q.top().first == i)
        {
            auto now = q.top();
            q.pop();
            tmp.push_back(now);
        }
        for (int j = tmp.size() - 1; j > 0; j--)
        {
            auto [x, y] = tmp[j];
            auto [x1, y1] = tmp[j - 1];
            // if (tmp[j] == tmp[j - 1])
            // {
                // cout << "0\n";
                // return;
            // }
            if(y1+1<=y)
            q.push(make_pair(y1 + 1, y));
        }
        if (tmp.size() > 0)
            ans.push_back(tmp[0]);
    }
    if (ans.size() < n)
        cout << 0 << '\n';
    else
    {
        for (int i = 1; i <= n; i++)
        {
            if (ans[i - 1].first != i)
            {
                cout << 0 << '\n';
                return;
            }
        }
        cout << 1 << '\n';
    }
}
int main()
{

    // cout << fixed << setprecision(10);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--)
        slove();
}