zoukankan      html  css  js  c++  java
  • P1368 工艺

    (color{#0066ff}{ 题目描述 })

    小敏和小燕是一对好朋友。

    他们正在玩一种神奇的游戏,叫Minecraft。

    他们现在要做一个由方块构成的长条工艺品。但是方块现在是乱的,而且由于机器的要求,他们只能做到把这个工艺品最左边的方块放到最右边。

    他们想,在仅这一个操作下,最漂亮的工艺品能多漂亮。

    两个工艺品美观的比较方法是,从头开始比较,如果第i个位置上方块不一样那么谁的瑕疵度小,那么谁就更漂亮,如果一样那么继续比较第i+1个方块。如果全都一样,那么这两个工艺品就一样漂亮。

    (color{#0066ff}{输入格式})

    第一行两个整数n,代表方块的数目。

    第二行n个整数,每个整数按从左到右的顺序输出方块瑕疵度的值。

    (color{#0066ff}{输出格式})

    一行n个整数,代表最美观工艺品从左到右瑕疵度的值。

    (color{#0066ff}{输入样例})

    10
    10 9 8 7 6 5 4 3 2 1
    

    (color{#0066ff}{输出样例})

    1 10 9 8 7 6 5 4 3 2
    

    (color{#0066ff}{数据范围与提示})

    对于20%的数据,n<=1000

    对于40%的数据,n<=10000

    对于100%的数据,n<=300000

    (color{#0066ff}{ 题解 })

    把数组复制一倍加在后面

    跑一遍SA

    然后找到排名第一的输出就行

    注意要离散化

    #include<bits/stdc++.h>
    using namespace std;
    #define LL long long
    LL in() {
    	char ch; int x = 0, f = 1;
    	while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    	for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    	return x * f;
    }
    const int maxn = 6e5 + 5;
    int sa[maxn], x[maxn], y[maxn], c[maxn];
    int a[maxn], b[maxn];
    int n, m;
    void SA() {
    	for(int i = 1; i <= n; i++) c[x[i] = a[i]]++;
    	for(int i = 1; i <= m; i++) c[i] += c[i - 1];
    	for(int i = n; i >= 1; i--) sa[c[x[i]]--] = i;
    	for(int k = 1; k <= n; k <<= 1) {
    		int num = 0;
    		for(int i = n - k + 1; i <= n; i++) y[++num] = i;
    		for(int i = 1; i <= n; i++) if(sa[i] > k) y[++num] = sa[i] - k;
    		for(int i = 1; i <= m; i++) c[i] = 0;
    		for(int i = 1; i <= n; i++) c[x[i]]++;
    		for(int i = 1; i <= m; i++) c[i] += c[i - 1];
    		for(int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i], y[i] = 0;
    		std::swap(x, y);
    		x[sa[1]] = 1, num = 1;
    		for(int i = 2; i <= n; i++) 
    			x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k])? num : ++num;
    		if(num == n) break;
    		m = num;
    	}
    }
    int main() {
    	n = in();
    	for(int i = 1; i <= n; i++) b[i] = a[i] = in();
    	int len = 1;
    	std::sort(b + 1, b + n + 1);
    	for(int i = 2; i <= n; i++) if(b[i] != b[i - 1]) b[++len] = b[i];
    	for(int i = 1; i <= n; i++) a[i] = std::lower_bound(b + 1, b + len + 1, a[i]) - b;
    	for(int i = 1; i <= n; i++) a[n + i] = a[i];
    	n <<= 1;
    	m = n;
    	SA();
    	for(int i = 1; i <= n; i++) if(sa[i] <= (n >> 1)) {
    		for(int j = sa[i]; j <= sa[i] + (n >> 1) - 1; j++)
    			printf("%d%c", b[a[j]], j == sa[i] + (n >> 1) - 1? '
    ' : ' ');
    		return 0;
    	}	
    }
    
  • 相关阅读:
    Python 集合
    Python sorted()
    CodeForces 508C Anya and Ghosts
    CodeForces 496B Secret Combination
    CodeForces 483B Friends and Presents
    CodeForces 490C Hacking Cypher
    CodeForces 483C Diverse Permutation
    CodeForces 478C Table Decorations
    CodeForces 454C Little Pony and Expected Maximum
    CodeForces 313C Ilya and Matrix
  • 原文地址:https://www.cnblogs.com/olinr/p/10255945.html
Copyright © 2011-2022 走看看