这道题即THUSC 2015 t3...只不过数据范围$n, m ≤ 10^5$

可以上网查这个鬼畜的东西"Burrows-Wheeler Transform"

这道题要用到解压缩也就是IBWT算法,复杂度$O(n + m)$

 

 1 /**************************************************************
 2     Problem: 2408
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:24 ms
 7     Memory:884 kb
 8 ****************************************************************/
 9  
10 #include <cstdio>
11  
12 using namespace std;
13 const int N = 1e4 + 5;
14  
15 int getint();
16  
17 int n, m, st;
18 int a[N], next[N], cnt[15];
19  
20 int main() {
21     int i, j;
22     n = getint(), m = getint();
23     for (i = 1; i <= n; ++i) ++cnt[(a[i] = getint()) + 1];
24     for (i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
25     for (i = 1; i <= n; ++i) next[++cnt[a[i]]] = i;
26     for (i = st = 1; i <= n; ++i) if (a[i] < a[st]) st = i;
27     for (i = 1, j = st; i <= n; ++i) printf("%d%c", a[j], i == n ? '\n' : ' '), j = next[j];
28     return 0;
29 }
30  
31 const int BUF_SIZE = 30;
32 char buf[BUF_SIZE], *buf_s = buf, *buf_t = buf + 1;
33 #define isdigit(x) ('0' <= x && x <= '9')
34 #define PTR_NEXT() { \
35     if (++buf_s == buf_t) \
36         buf_s = buf, buf_t = buf + fread(buf, 1, BUF_SIZE, stdin); \
37 }
38  
39 inline int getint() {
40     register int x = 0;
41     while (!isdigit(*buf_s)) PTR_NEXT();
42     while (isdigit(*buf_s)) {
43         x = x * 10 + *buf_s - '0';
44         PTR_NEXT();
45     }
46     return x;
47 }
View Code

(p.s. 窝考试的时候sb。。。只想出来了$O(mn)$的算法QAQAQQQ)

posted on 2015-06-02 18:49  Xs酱~  阅读(668)  评论(0编辑  收藏  举报