[bzoj3325] [Scoi2013]密码【malachar】【贪心】

【题目链接】
  https://www.lydsy.com/JudgeOnline/problem.php?id=3325
【题解】
  由于一定有解,所以每个位置只要计算一次,所以考虑用马拉车来解决。可以通过马拉车的思路,如果之前算到这里,那么就不再算下去。同时再开一个数组记录每个数不能填的数,显然复杂度是 O(N) O ( N )
【代码】

/* - - - - - - - - - - - - - - -
    User :      VanishD
    problem :   [bzoj3325] 
    Points :    malachar
- - - - - - - - - - - - - - - */
# include <bits/stdc++.h>
# define    ll      long long
# define    inf     0x3f3f3f3f
# define    N       200010
using namespace std;
int read(){
    int tmp = 0, fh = 1; char ch = getchar();
    while (ch < '0' || ch > '9'){ if (ch == '-') fh = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9'){ tmp = tmp * 10 + ch - '0'; ch = getchar(); }
    return tmp * fh;
}
int p[N], s[N], tag[N][31], n;
int main(){
//  freopen("bzoj3325.in", "r", stdin);
//  freopen("bzoj3325.out", "w", stdout);
    n = read();
    for (int i = 1; i <= n; i++)
        p[i * 2 - 1] = read();
    for (int i = 1; i < n; i++)
        p[i * 2] = read();
    s[0] = 28, s[1] = 1;
    int r = 0;
    for (int i = 1; i <= n * 2; i++){
        if (s[i] == 0){
            for (int j = 1; j <= 27; j++)
                if (tag[i][j] == false){
                    s[i] = j;
                    break;
                }
        }
        int now = i + p[i];
        for (int j = r + 1; j <= now; j++)
            s[j] = s[i - (j - i)];
        tag[now + 1][s[i - (now  + 1 - i)]] = true;
        r = max(r, now);
    }
    for (int i = 1; i <= n; i++)
        printf("%c", s[i * 2 - 1] + 'a' - 1);
    printf("\n");
    return 0;
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页