Codeforces Round #704 (Div. 2) A~E

比赛链接:https://codeforces.com/contest/1492

1492A.Three swimmers

题意:

有三名游泳的人,他们分别需要 a,b,c 分钟才能在一个游泳池游一个来回,第一个游泳者将在开始时间 0a2a3a 分钟后在游泳池的左侧,第二个游泳者将在 0b2b3b 分钟后,第三个将在 0c2c3c 分钟后出现在池的左侧。
p​ 分钟后最少需要等待多长时间会有一个游泳者到达游泳池左侧。

思路:

签到题,

可以暴力遍历 a,b,c 寻找最小值,

也可以直接找 a,b,c 的倍数离 p​​ 差多少

注意开 long long....

【AC Code】

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int _; for (cin >> _; _--;) {
        ll p, a, b, c;
        cin >> p >> a >> b >> c;
        if (p % a == 0 || p % b == 0 || p % c == 0)
            cout << "0\n";
        else cout << min(a - p % a, min(b - p % b, c - p % c)) << "\n";
    }
}

1492B. Card Deck

题意:

n 张卡,每张卡的值 1n,等于 pip1 代表底卡,pn 是顶卡,在每个步骤中,选择一些 k>0 的整数,从原始卡片组中取出前 k 张卡片,然后按它们现在的顺序将它们放置在新卡片组的顶部。让阶数最大。

思路:

找最大的数输出最大的数加后边的所有数,接着找第二个······注意不要重复输出

【AC Code】

const int N = 1e5 + 10;
ll a[N], pos[N];
bool st[N];
int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int _; for (cin >> _; _--;) {
        vector<int>A;
        int n; cin >> n;
        for (int i = 1; i <= n; ++i) {
            cin >> a[i];
            pos[i] = i;
            st[a[i]] = 0;
        }
        ll t = n;
        for (ll i = n; i >= 1; i -= 1) {
            if (a[i] == t)
                for (ll j = i; !st[a[j]] && j <= n; j += 1) {
                    st[a[j]] = 1;
                    A.emplace_back(a[j]);
                }
            while (st[t] && t >= 1) t -= 1;
        }
        for (ll i = 0; i < n; i += 1) cout << A[i] << " \n"[i == n - 1];
    }
}

1492C. Maximum width

题意:

给你两个字符串 s,ts 串和 t 串匹配的下标,然后找相邻下标之间的最大值,管你懂不懂看样例就完事了

思路:

看了半天样例发现只需要前后扫一遍,分别用两个数组去存前后扫的下标结果,取两个数组相邻位置的最大值

【AC Code】

const int N = 2e5 + 10;
int n, m, a[N], b[N];
string s, t;
int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cin >> n >> m >> s >> t;
    int i = 0, j = 0;
    while (j < m) {
        if (s[i] == t[j]) {
            a[j] = i + 1;
            i ++; j ++;
        } else i ++;
    }
    i = n - 1, j = m - 1;
    while (j > 0) {
        if (s[i] == t[j]) {
            b[j] = i + 1;
            i --; j --;
        } else i --;
    }
    int ans = 0;
    for (int q = 0; q < m; q ++)
        ans = max(ans, b[q + 1] - a[q]);
    cout << ans << "\n";
}

1492D. Genius’s Gambit

题意:

给你三个数,a 代表 0 的个数,b 代表 1 的个数,k 代表 xy1 的个数,让你求出 xy 满足 abk 三个条件,如果没有输出 NO

思路:

构造题,说实话赛时没构造出来,这里参考了一下题解

由题目可知 x>y 是肯定的,所以我们不妨假设 xy1100 ,然后对 y 进行变换,我们发现 y 最右边的1往后移动一位 xy 的值就多一个 1 ,最大为 a​ ,然后移动倒数第二个 1 ,发现只能移动一次,如果移动两次不会多一个 1​ 。

假设
x 1110000
y 1110000

y 的倒数第一个 1 往右移动时发现 xy1 的个数 +1 ,当y 1100001时 xy1 的个数为 4 ,然后移动倒数第二个 1,移动一次,发现 xy1 的个数变成了 5 ,再移动一次发现又变回 4 ,所以除了倒数第一个 1 可以移动多次,第一个 1 不能移动中间的 1 只能移动一次,我们可以找出规律,最大的值为 a+b2​​​ 。

【AC Code】

int a, b, k;

int main(void) {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> a >> b >> k;
    if (k == 0) {
        cout << "Yes" << '\n';
        string x = "", y = "";
        x += '1';
        b--;
        for (int i = 0; i < a; i++) x += '0';
        for (int i = 0; i < b; i++) x += '1';
        cout << x << '\n';
        cout << x << '\n';
        return 0;
    }
    k--;
    if (a > 0 && b >= 2 && (a + b - 3 >= k)) {
        cout << "Yes" << '\n';
        string x = "";
        string y = "";
        a--; b -= 2;
        x += '1'; y += '1';

        x += '1'; y += '0';
        for (int i = 0; i < k; i++) {
            if (a) {
                a--;
                x += '0'; y += '0';
            } else {
                b--;
                x += '1'; y += '1';
            }
        }
        x += '0'; y += '1';
        while (a--) {
            x += '0'; y += '0';
        }
        while (b--) {
            x += '1'; y += '1';
        }

        cout << x << '\n';
        cout << y << '\n';
    } else
        cout << "No" << '\n';
}

模拟构造,我不会(QAQ

1492E. Almost Fault-Tolerant Database

题意:

给定 n 个长度为 m 的数组,需要输出一个长度为 m 的数组,使得这些数组之间不同的数不超过两个,输出YES,输出你构造的数组,若有多种情况,可任意输出一种。如若不存在,输出NO。

思路:

假设第一个数组为标准数组,遍历

  1. 不同数小于等于 2,没问题
  2. 不同数大于 4 ,直接输出NO
  3. 就是 3,4 的情况,我们找差异数最大的那个也就是 4 的情况,枚举修改的两个位置,判断是否可以成立。

【AC Code】

const int N = 250010;
int n, m;
vector <int> s[N];
bool check2(vector<int> &v) {
    for (int i = 1; i < n; i ++) {
        int cnt = 0;
        for (int j = 0; j < m; j ++)
            if (s[i][j] != v[j]) cnt ++;

        if (cnt > 3) return false;
        else if (cnt == 3) {
            int ans = 1;
            for (int j = 0; j < m; j ++) {
                if (s[i][j] != v[j] && v[j] == -1) {
                    ans = 0;
                    v[j] = s[i][j];
                    break;
                }
            }
            if (ans) return false;
        }
    }
    return true;
}


bool check() {
    int cnt = 0, ax = 0, id;
    for (int i = 1; i < n; i ++) {
        int ans = 0;
        for (int j = 0; j < m; j ++)
            if (s[i][j] != s[0][j]) ans ++;

        if (ans > 4) return false;
        if (ans <= 2) cnt ++;
        if (ans > ax)ax = ans, id = i;
    }

    if (cnt == n - 1) {
        cout << "Yes\n";
        for (int i = 0; i < m; ++i) cout << s[0][i] << " \n"[i == m - 1];
        return true;
    }

    vector<int> a;
    for (int i = 0; i < m; i ++)
        if (s[0][i] != s[id][i]) a.push_back(i);

    for (int i = 0; i < a.size(); i ++) {
        for (int j = 0; j < a.size(); j ++) {
            if (i == j) continue;

            vector<int> b = s[0];

            b[a[i]] = s[id][a[i]];
            b[a[j]] = -1;

            if (check2(b)) {
                cout << "Yes\n";
                for (int i = 0; i < m; ++i) cout << (b[i] == -1 ? s[0][i] : b[i]) << " \n"[i == m - 1];
                return true;
            }
        }
    }
    return false;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    cin >> n >> m;
    for (int i = 0; i < n; i ++) {
        for (int j = 0, x; j < m; j ++) {
            cin >> x;
            s[i].push_back(x);
        }
    }
    if (!check()) puts("No");
}
posted @   RioTian  阅读(42)  评论(0编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· 分享4款.NET开源、免费、实用的商城系统
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
· 5. Nginx 负载均衡配置案例(附有详细截图说明++)
历史上的今天:
2020-09-01 递归 & 分治算法深度理解
2020-09-01 0/1 分数规划技巧
点击右上角即可分享
微信分享提示

📖目录