#include <iostream>
#include <cmath>
#include <unordered_map>
using namespace std;
const long long mod = 9999999999999937LL;
const long long p = 10;
const int maxN = 7e4 + 5;
long long ModSum(const long long x, const long long y) {
return (x + y) % mod;
}
long long ModSubtraction(const long long x, const long long y) {
return (x - y + mod) % mod;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
long long last_h = 0;
long long diff[maxN];
for (int i = 0; i < n; i++) {
string s;
cin >> s;
long long h = 0;
for (int j = 0; j < s.size(); j++) {
h = ModSum(h * p, s[j] - '0');
}
diff[i] = ModSubtraction(h, last_h);
last_h = h;
}
const int length = (int)sqrt(n) + 1;
long long h_sum = 1;
long long need[maxN];
unordered_map<long long, int> st[maxN];
for (int i = 0; i < n; i++) {
need[i] = ModSubtraction(h_sum, diff[i]);
st[i / length][need[i]] = i;
h_sum = ModSum(h_sum, diff[i]);
}
bool stat[maxN];
long long push[maxN];
int result[maxN];
fill(stat, stat + maxN, true);
fill(push, push + maxN, 0LL);
fill(result, result + maxN, 0);
const int maxSegmIndex = (n - 1) / length;
for (int j = n; j > 0; j--) {
int pos = 0;
for (int index = maxSegmIndex; index >= 0; index--) {
if (st[index].find(push[index]) != st[index].end()) {
pos = st[index][push[index]];
break;
}
}
result[pos] = j;
stat[pos] = false;
const int segmIndex = pos / length;
const int l = segmIndex * length;
const int r = min(n, l + length);
st[segmIndex].clear();
for (int i = l; i < r; i++) {
if (!stat[i]) {
continue;
}
if (i > pos) {
need[i] = ModSubtraction(need[i], diff[pos]);
}
st[segmIndex][need[i]] = i;
}
for (int index = segmIndex + 1; index <= maxSegmIndex; index++) {
push[index] = ModSum(push[index], diff[pos]);
}
}
for (int i = 0; i < n; i++) {
cout << result[i] << ' ';
}
return 0;
}
Compilation message
permutation.cpp: In function 'int main()':
permutation.cpp:34:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for (int j = 0; j < s.size(); j++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
5 ms |
6080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
5 ms |
6080 KB |
Output is correct |
3 |
Correct |
4 ms |
6152 KB |
Output is correct |
4 |
Correct |
4 ms |
6100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
5 ms |
6080 KB |
Output is correct |
3 |
Correct |
4 ms |
6152 KB |
Output is correct |
4 |
Correct |
4 ms |
6100 KB |
Output is correct |
5 |
Correct |
408 ms |
12380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
5 ms |
6080 KB |
Output is correct |
3 |
Correct |
4 ms |
6152 KB |
Output is correct |
4 |
Correct |
4 ms |
6100 KB |
Output is correct |
5 |
Correct |
408 ms |
12380 KB |
Output is correct |
6 |
Correct |
916 ms |
15320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
5 ms |
6080 KB |
Output is correct |
3 |
Correct |
4 ms |
6152 KB |
Output is correct |
4 |
Correct |
4 ms |
6100 KB |
Output is correct |
5 |
Correct |
408 ms |
12380 KB |
Output is correct |
6 |
Correct |
916 ms |
15320 KB |
Output is correct |
7 |
Correct |
849 ms |
15032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
5 ms |
6080 KB |
Output is correct |
3 |
Correct |
4 ms |
6152 KB |
Output is correct |
4 |
Correct |
4 ms |
6100 KB |
Output is correct |
5 |
Correct |
408 ms |
12380 KB |
Output is correct |
6 |
Correct |
916 ms |
15320 KB |
Output is correct |
7 |
Correct |
849 ms |
15032 KB |
Output is correct |
8 |
Correct |
617 ms |
7388 KB |
Output is correct |
9 |
Correct |
1149 ms |
9516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
5 ms |
6080 KB |
Output is correct |
3 |
Correct |
4 ms |
6152 KB |
Output is correct |
4 |
Correct |
4 ms |
6100 KB |
Output is correct |
5 |
Correct |
408 ms |
12380 KB |
Output is correct |
6 |
Correct |
916 ms |
15320 KB |
Output is correct |
7 |
Correct |
849 ms |
15032 KB |
Output is correct |
8 |
Correct |
617 ms |
7388 KB |
Output is correct |
9 |
Correct |
1149 ms |
9516 KB |
Output is correct |
10 |
Correct |
1306 ms |
9600 KB |
Output is correct |