栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

AtCoder Regular Contest 127 B - Ternary Strings(构造)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AtCoder Regular Contest 127 B - Ternary Strings(构造)

传送门

题意:

构造 3 × n 3 times n 3×n 个长度为 l l l 的字符串,每一位只有 0 , 1 , 2 0,1,2 0,1,2 三个数字,且满足第 i i i 位一共有 n n n 个 0 0 0, n n n 个 1 1 1 , n n n 个 2 2 2​

并且使得这些字符串中字典序最大的字符串字典序尽可能小。

题解:

第 1 1 1 位一定有 n n n 个 2 2 2 ,考虑三进制,那么字典序最大的字符串 t t t ≥ 2 × 3 l − 1 + n − 1 geq 2 times 3^{l-1} +n-1 ≥2×3l−1+n−1

我们可以使得 t = 2 × 3 l − 1 + n − 1 t=2 times 3^{l-1} +n-1 t=2×3l−1+n−1​ ,并且一定能构造出满足题意的字符串。

我们只需让已经构造出来的以 2 2 2为开头的字符串以如下形式变换:

1.0 − > 1 1.0->1 1.0−>1 1 − > 2 1->2 1−>2 2 − > 0 2->0 2−>0

2. 2. 2. 0 − > 2 0->2 0−>2 1 − > 0 1->0 1−>0 2 − > 1 2->1 2−>1

假设以 2 2 2​开头第 i i i​ 位有 x x x​ 个 0 0 0​ ,那么 1 1 1​, 2 2 2​ 总共有 n − x n-x n−x​ 个,经过上述变换,就可以满足最终 0 0 0 的个数有 x + n − x = n x+n-x=n x+n−x=n

代码:

#pragma GCC diagnostic error "-std=c++11"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair pii;
const int mod=1e9+7;
const int MAXN=5e5+5;
const int inf=0x3f3f3f3f;
int ans[MAXN][20];
int main()
{
    int n,l;
    cin >> n >> l;
    ll x = 2 * pow(3, l - 1);
    for (int i = 1; i <= n;i++){
        ll tp = x;
        vector v;
        for (int j = 1; j <= l;j++){
            v.push_back(tp % 3);
            tp /= 3;
        }
        reverse(v.begin(), v.end());
        x++;
        for (int j = 1; j <= l;j++){
            ans[i][j] = v[j - 1];
        }
    }
    for (int i = 1; i <= n;i++){
        for (int j = 1; j <= l;j++){
            if(ans[i][j]==0){
                ans[i + n][j] = 1;
                ans[i + n * 2][j] = 2;
            }
            else if(ans[i][j]==1){
                ans[i + n][j] = 2;
                ans[i + n * 2][j] = 0;
            }
            else{
                ans[i + n][j] = 0;
                ans[i + n * 2][j] = 1;
            }
        }
    }
    for (int i = 1; i <= 3 * n;i++){
        for (int j = 1; j <= l;j++){
            printf("%d", ans[i][j]);
        }
        printf("n");
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/296542.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号