답안 #697513

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
697513 2023-02-10T07:28:13 Z borisAngelov Cryptography (NOI20_crypto) C++17
100 / 100
168 ms 17784 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

const int maxn = 300005;
const long long mod = 1000000007;

int n;

int a[maxn];

void read()
{
    cin >> n;

    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }
}

void compress()
{
    vector<int> to_compress;

    for (int i = 1; i <= n; ++i)
    {
        to_compress.push_back(a[i]);
    }

    sort(to_compress.begin(), to_compress.end());

    unordered_map<int, int> compressed_values;

    for (int i = 0; i < to_compress.size(); ++i)
    {
        compressed_values[to_compress[i]] = i + 1;
    }

    for (int i = 1; i <= n; ++i)
    {
        a[i] = compressed_values[a[i]];
    }
}

int fenwick[maxn];

void update(int pos, int val)
{
    while (pos <= n)
    {
        fenwick[pos] += val;
        pos += (pos & (-pos));
    }
}

int query(int pos)
{
    int result = 0;

    while (pos >= 1)
    {
        result += fenwick[pos];
        pos -= (pos & (-pos));
    }

    return result;
}

long long factoriels[maxn];

void solve()
{
    factoriels[1] = 1;

    for (int i = 2; i <= n; ++i)
    {
        factoriels[i] = (factoriels[i - 1] * (1LL * i)) % mod;
    }

    for (int i = 1; i <= n; ++i)
    {
        update(a[i], +1);
    }

    long long ans = 0;

    for (int i = 1; i <= n; ++i)
    {
        long long cnt_smaller = query(a[i] - 1);

        ans = (ans + cnt_smaller * factoriels[n - i]) % mod;
        //cout << i << ' ' << a[i] << ' ' << cnt_smaller << ' ' << factoriels[n - i] << endl;

        update(a[i], -1);
    }

    ans = (ans + 1) % mod;

    cout << ans << endl;
}

void fastIO()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int main()
{
    fastIO();

    read();
    compress();
    solve();

    return 0;
}

Compilation message

Crypto.cpp: In function 'void compress()':
Crypto.cpp:38:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for (int i = 0; i < to_compress.size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 328 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 126 ms 17600 KB Output is correct
3 Correct 119 ms 17680 KB Output is correct
4 Correct 122 ms 17764 KB Output is correct
5 Correct 135 ms 17732 KB Output is correct
6 Correct 122 ms 17660 KB Output is correct
7 Correct 116 ms 17644 KB Output is correct
8 Correct 126 ms 17780 KB Output is correct
9 Correct 130 ms 17628 KB Output is correct
10 Correct 116 ms 17652 KB Output is correct
11 Correct 121 ms 17644 KB Output is correct
12 Correct 157 ms 17628 KB Output is correct
13 Correct 119 ms 17656 KB Output is correct
14 Correct 126 ms 17716 KB Output is correct
15 Correct 136 ms 17668 KB Output is correct
16 Correct 132 ms 17648 KB Output is correct
17 Correct 119 ms 17624 KB Output is correct
18 Correct 130 ms 17672 KB Output is correct
19 Correct 133 ms 17612 KB Output is correct
20 Correct 126 ms 17652 KB Output is correct
21 Correct 114 ms 17660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 64 ms 16692 KB Output is correct
3 Correct 76 ms 16688 KB Output is correct
4 Correct 63 ms 16672 KB Output is correct
5 Correct 64 ms 16776 KB Output is correct
6 Correct 62 ms 16720 KB Output is correct
7 Correct 63 ms 16760 KB Output is correct
8 Correct 71 ms 16764 KB Output is correct
9 Correct 69 ms 16728 KB Output is correct
10 Correct 71 ms 16656 KB Output is correct
11 Correct 62 ms 16764 KB Output is correct
12 Correct 66 ms 16652 KB Output is correct
13 Correct 61 ms 16680 KB Output is correct
14 Correct 61 ms 16680 KB Output is correct
15 Correct 65 ms 16764 KB Output is correct
16 Correct 62 ms 16752 KB Output is correct
17 Correct 62 ms 16668 KB Output is correct
18 Correct 61 ms 16748 KB Output is correct
19 Correct 61 ms 16676 KB Output is correct
20 Correct 61 ms 16756 KB Output is correct
21 Correct 64 ms 16776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 328 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 472 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 472 KB Output is correct
10 Correct 1 ms 476 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 1 ms 464 KB Output is correct
14 Correct 1 ms 468 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 328 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 324 KB Output is correct
25 Correct 1 ms 328 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 472 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 1 ms 468 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 472 KB Output is correct
34 Correct 1 ms 476 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 464 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 468 KB Output is correct
45 Correct 1 ms 468 KB Output is correct
46 Correct 1 ms 468 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 468 KB Output is correct
49 Correct 1 ms 500 KB Output is correct
50 Correct 1 ms 504 KB Output is correct
51 Correct 1 ms 468 KB Output is correct
52 Correct 1 ms 468 KB Output is correct
53 Correct 1 ms 468 KB Output is correct
54 Correct 1 ms 468 KB Output is correct
55 Correct 1 ms 468 KB Output is correct
56 Correct 1 ms 468 KB Output is correct
57 Correct 1 ms 472 KB Output is correct
58 Correct 2 ms 468 KB Output is correct
59 Correct 1 ms 468 KB Output is correct
60 Correct 1 ms 468 KB Output is correct
61 Correct 2 ms 340 KB Output is correct
62 Correct 1 ms 468 KB Output is correct
63 Correct 1 ms 468 KB Output is correct
64 Correct 1 ms 468 KB Output is correct
65 Correct 2 ms 468 KB Output is correct
66 Correct 1 ms 468 KB Output is correct
67 Correct 1 ms 468 KB Output is correct
68 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 64 ms 16692 KB Output is correct
3 Correct 76 ms 16688 KB Output is correct
4 Correct 63 ms 16672 KB Output is correct
5 Correct 64 ms 16776 KB Output is correct
6 Correct 62 ms 16720 KB Output is correct
7 Correct 63 ms 16760 KB Output is correct
8 Correct 71 ms 16764 KB Output is correct
9 Correct 69 ms 16728 KB Output is correct
10 Correct 71 ms 16656 KB Output is correct
11 Correct 62 ms 16764 KB Output is correct
12 Correct 66 ms 16652 KB Output is correct
13 Correct 61 ms 16680 KB Output is correct
14 Correct 61 ms 16680 KB Output is correct
15 Correct 65 ms 16764 KB Output is correct
16 Correct 62 ms 16752 KB Output is correct
17 Correct 62 ms 16668 KB Output is correct
18 Correct 61 ms 16748 KB Output is correct
19 Correct 61 ms 16676 KB Output is correct
20 Correct 61 ms 16756 KB Output is correct
21 Correct 64 ms 16776 KB Output is correct
22 Correct 1 ms 328 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 472 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 472 KB Output is correct
31 Correct 1 ms 476 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 1 ms 464 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 468 KB Output is correct
40 Correct 1 ms 468 KB Output is correct
41 Correct 1 ms 468 KB Output is correct
42 Correct 1 ms 468 KB Output is correct
43 Correct 1 ms 468 KB Output is correct
44 Correct 1 ms 328 KB Output is correct
45 Correct 99 ms 16760 KB Output is correct
46 Correct 97 ms 16760 KB Output is correct
47 Correct 101 ms 16680 KB Output is correct
48 Correct 96 ms 16716 KB Output is correct
49 Correct 102 ms 16760 KB Output is correct
50 Correct 95 ms 16764 KB Output is correct
51 Correct 100 ms 16760 KB Output is correct
52 Correct 99 ms 16712 KB Output is correct
53 Correct 97 ms 16776 KB Output is correct
54 Correct 98 ms 16716 KB Output is correct
55 Correct 99 ms 16712 KB Output is correct
56 Correct 101 ms 16704 KB Output is correct
57 Correct 99 ms 16764 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 101 ms 16776 KB Output is correct
60 Correct 98 ms 16780 KB Output is correct
61 Correct 102 ms 16712 KB Output is correct
62 Correct 98 ms 16772 KB Output is correct
63 Correct 101 ms 16704 KB Output is correct
64 Correct 99 ms 16748 KB Output is correct
65 Correct 96 ms 16716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 328 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 324 KB Output is correct
16 Correct 1 ms 324 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 324 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 126 ms 17600 KB Output is correct
27 Correct 119 ms 17680 KB Output is correct
28 Correct 122 ms 17764 KB Output is correct
29 Correct 135 ms 17732 KB Output is correct
30 Correct 122 ms 17660 KB Output is correct
31 Correct 116 ms 17644 KB Output is correct
32 Correct 126 ms 17780 KB Output is correct
33 Correct 130 ms 17628 KB Output is correct
34 Correct 116 ms 17652 KB Output is correct
35 Correct 121 ms 17644 KB Output is correct
36 Correct 157 ms 17628 KB Output is correct
37 Correct 119 ms 17656 KB Output is correct
38 Correct 126 ms 17716 KB Output is correct
39 Correct 136 ms 17668 KB Output is correct
40 Correct 132 ms 17648 KB Output is correct
41 Correct 119 ms 17624 KB Output is correct
42 Correct 130 ms 17672 KB Output is correct
43 Correct 133 ms 17612 KB Output is correct
44 Correct 126 ms 17652 KB Output is correct
45 Correct 114 ms 17660 KB Output is correct
46 Correct 1 ms 324 KB Output is correct
47 Correct 64 ms 16692 KB Output is correct
48 Correct 76 ms 16688 KB Output is correct
49 Correct 63 ms 16672 KB Output is correct
50 Correct 64 ms 16776 KB Output is correct
51 Correct 62 ms 16720 KB Output is correct
52 Correct 63 ms 16760 KB Output is correct
53 Correct 71 ms 16764 KB Output is correct
54 Correct 69 ms 16728 KB Output is correct
55 Correct 71 ms 16656 KB Output is correct
56 Correct 62 ms 16764 KB Output is correct
57 Correct 66 ms 16652 KB Output is correct
58 Correct 61 ms 16680 KB Output is correct
59 Correct 61 ms 16680 KB Output is correct
60 Correct 65 ms 16764 KB Output is correct
61 Correct 62 ms 16752 KB Output is correct
62 Correct 62 ms 16668 KB Output is correct
63 Correct 61 ms 16748 KB Output is correct
64 Correct 61 ms 16676 KB Output is correct
65 Correct 61 ms 16756 KB Output is correct
66 Correct 64 ms 16776 KB Output is correct
67 Correct 1 ms 328 KB Output is correct
68 Correct 1 ms 468 KB Output is correct
69 Correct 1 ms 468 KB Output is correct
70 Correct 1 ms 468 KB Output is correct
71 Correct 1 ms 472 KB Output is correct
72 Correct 1 ms 468 KB Output is correct
73 Correct 1 ms 468 KB Output is correct
74 Correct 1 ms 468 KB Output is correct
75 Correct 1 ms 472 KB Output is correct
76 Correct 1 ms 476 KB Output is correct
77 Correct 1 ms 468 KB Output is correct
78 Correct 1 ms 468 KB Output is correct
79 Correct 1 ms 464 KB Output is correct
80 Correct 1 ms 468 KB Output is correct
81 Correct 0 ms 340 KB Output is correct
82 Correct 1 ms 468 KB Output is correct
83 Correct 1 ms 468 KB Output is correct
84 Correct 1 ms 468 KB Output is correct
85 Correct 1 ms 468 KB Output is correct
86 Correct 1 ms 468 KB Output is correct
87 Correct 1 ms 468 KB Output is correct
88 Correct 1 ms 468 KB Output is correct
89 Correct 1 ms 340 KB Output is correct
90 Correct 1 ms 468 KB Output is correct
91 Correct 1 ms 500 KB Output is correct
92 Correct 1 ms 504 KB Output is correct
93 Correct 1 ms 468 KB Output is correct
94 Correct 1 ms 468 KB Output is correct
95 Correct 1 ms 468 KB Output is correct
96 Correct 1 ms 468 KB Output is correct
97 Correct 1 ms 468 KB Output is correct
98 Correct 1 ms 468 KB Output is correct
99 Correct 1 ms 472 KB Output is correct
100 Correct 2 ms 468 KB Output is correct
101 Correct 1 ms 468 KB Output is correct
102 Correct 1 ms 468 KB Output is correct
103 Correct 2 ms 340 KB Output is correct
104 Correct 1 ms 468 KB Output is correct
105 Correct 1 ms 468 KB Output is correct
106 Correct 1 ms 468 KB Output is correct
107 Correct 2 ms 468 KB Output is correct
108 Correct 1 ms 468 KB Output is correct
109 Correct 1 ms 468 KB Output is correct
110 Correct 1 ms 468 KB Output is correct
111 Correct 1 ms 328 KB Output is correct
112 Correct 99 ms 16760 KB Output is correct
113 Correct 97 ms 16760 KB Output is correct
114 Correct 101 ms 16680 KB Output is correct
115 Correct 96 ms 16716 KB Output is correct
116 Correct 102 ms 16760 KB Output is correct
117 Correct 95 ms 16764 KB Output is correct
118 Correct 100 ms 16760 KB Output is correct
119 Correct 99 ms 16712 KB Output is correct
120 Correct 97 ms 16776 KB Output is correct
121 Correct 98 ms 16716 KB Output is correct
122 Correct 99 ms 16712 KB Output is correct
123 Correct 101 ms 16704 KB Output is correct
124 Correct 99 ms 16764 KB Output is correct
125 Correct 1 ms 212 KB Output is correct
126 Correct 101 ms 16776 KB Output is correct
127 Correct 98 ms 16780 KB Output is correct
128 Correct 102 ms 16712 KB Output is correct
129 Correct 98 ms 16772 KB Output is correct
130 Correct 101 ms 16704 KB Output is correct
131 Correct 99 ms 16748 KB Output is correct
132 Correct 96 ms 16716 KB Output is correct
133 Correct 1 ms 212 KB Output is correct
134 Correct 162 ms 17756 KB Output is correct
135 Correct 153 ms 17732 KB Output is correct
136 Correct 150 ms 17724 KB Output is correct
137 Correct 148 ms 17724 KB Output is correct
138 Correct 150 ms 17660 KB Output is correct
139 Correct 150 ms 17784 KB Output is correct
140 Correct 163 ms 17736 KB Output is correct
141 Correct 162 ms 17756 KB Output is correct
142 Correct 146 ms 17704 KB Output is correct
143 Correct 152 ms 17604 KB Output is correct
144 Correct 153 ms 17604 KB Output is correct
145 Correct 152 ms 17668 KB Output is correct
146 Correct 146 ms 17664 KB Output is correct
147 Correct 0 ms 340 KB Output is correct
148 Correct 150 ms 17636 KB Output is correct
149 Correct 153 ms 17656 KB Output is correct
150 Correct 168 ms 17628 KB Output is correct
151 Correct 157 ms 17668 KB Output is correct
152 Correct 154 ms 17652 KB Output is correct
153 Correct 154 ms 17608 KB Output is correct
154 Correct 148 ms 17672 KB Output is correct