首页 > 其他分享 >Codeforces Round 966 (Div. 3)

Codeforces Round 966 (Div. 3)

时间:2024-08-21 15:06:48浏览次数:7  
标签:966 int vi Codeforces long cin solve using Div

A. Primary Task

#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;

void solve() {
    string s;
    cin >> s;
    if (s.size() <= 2) {
        cout << "NO\n";
        return;
    }
    if (s[0] != '1' or s[1] != '0') {
        cout << "NO\n";
        return;
    }
    auto t = s.substr(2);
    if (t.empty() or stoi(t) < 2 or t.front() == '0') {
        cout << "NO\n";
        return;
    }
    cout << "YES\n";
    return;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

B. Seating in a Bus

#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;

void solve() {
    int n;
    cin >> n;
    vi a(n + 5);
    bool f = true;
    for (int i = 1, x; i <= n; i++) {
        cin >> x;
        if (i != 1 and a[x - 1] == 0 and a[x + 1] == 0) f = false;
        a[x] = 1;
    }
    if (f) cout << "YES\n";
    else cout << "NO\n";
    return;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

C. Numeric String Template

#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;

const int inf = INT_MAX / 2;

void solve() {
    int n;
    cin >> n;
    vi a(n);
    for (auto &i: a) cin >> i;
    int m;
    cin >> m;
    for (string s; m; m--) {
        cin >> s;
        if (s.size() != n) {
            cout << "NO\n";
            continue;
        }
        map<int, set<char>> cnt1;
        map<char, set<int>> cnt2;
        for (int i = 0; i < n; i++) {
            cnt1[a[i]].insert(s[i]);
            cnt2[s[i]].insert(a[i]);
        }
        bool f = true;
        for (auto [key, val]: cnt1)
            if (val.size() > 1) f = false;

        for (auto [key, val]: cnt2)
            if (val.size() > 1) f = false;
        if (f) cout << "YES\n";
        else cout << "NO\n";
    }
    return;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int T;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

D. Right Left Wrong

贪心,从左到右遍历,对于每一个L找到最右侧没有被使用过的R,求和的部分可以用前缀和优化。

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;
using ldb = long double;

#define int i64

using vi = vector<int>;

void solve() {
    int n;
    cin >> n;
    vi a(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = 1; i <= n; i++)
        a[i] += a[i - 1];


    string b;
    cin >> b;
    b = " " + b;

    vi R;
    for (int i = 1; i <= n; i++)
        if (b[i] == 'R') R.push_back(i);

    int res = 0;
    for (int i = 1, x; i <= n; i++) {
        if (b[i] != 'L') continue;
        if (R.empty()) continue;
        x = R.back(), R.pop_back();
        if (x < i) continue;
        res += a[x] - a[i - 1];
    }

    cout << res << "\n";
    return;
}

i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int T;
    for (cin >> T; T; T--)
        solve();
    return 0;
}

E. Photoshoot for Gorillas

二维前缀和算出每一格被多少个子矩形覆盖,然后贪心放置。

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;
using ldb = long double;

const i32 inf = INT_MAX / 2;
const i64 INF = LLONG_MAX / 2;
#define int i64


using vi = vector<int>;

void solve() {
    int n, m, k;
    cin >> n >> m >> k;
    vector a(n + 2, vi(m + 2));
    for (int i = 1, x = k; x <= n; i++, x++)
        for (int j = 1, y = k; y <= m; j++, y++)
            a[i][j]++, a[i][y + 1]--, a[x + 1][j]--, a[x + 1][y + 1]++;
    vi val;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            a[i][j] += a[i][j - 1] + a[i - 1][j] - a[i - 1][j - 1];
            val.push_back(a[i][j]);
        }
    sort(val.begin(), val.end(), greater<>());

    int w;
    cin >> w;
    vi b(w);
    for (auto &i: b) cin >> i;
    sort(b.begin(), b.end(), greater<>());
    int res = 0;
    for (int i = 0; i < w; i++)
        res += b[i] * val[i];
    cout << res << "\n";
    return;
}

i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int T;
    for (cin >> T; T; T--)
        solve();
    return 0;
}

F. Color Rows and Columns

首先考虑只有一个矩形的情况。

对于一个矩形,如果是\(1\times 1\)的矩形,则只能填这个一个格子,并获得\(2\)分。否则的话,一定是贪心的填满一个短边,并获得\(1\)分。所以对于一个矩形,我们可以贪心求出获得\(w\)分,需要操作\(v\)次。

这样的话,我们可以采用分组背包来解决,对于每一个矩形贪心求出最优解即可。

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;
using ldb = long double;

const i32 inf = INT_MAX / 2;
const i64 INF = LLONG_MAX / 2;
#define int i64


using vi = vector<int>;

void solve() {
    int n, k;
    cin >> n >> k;
    vi f(k + 2, inf);
    f[0] = 0;
    for (int i = 1, x, y, v, w; i <= n; i++) {
        cin >> x >> y, v = 0, w = 0;
        auto g = f;
        while (x > 0 and y > 0) {
            if (x > y) swap(x, y);
            if (x == 1 and y == 1) v += 1, w += 2, x = y = 0;
            else v += x, w += 1, y--;
            for (int j = w; j <= k + 1; j++)
                g[j] = min(g[j], f[j - w] + v);
        }
        f = move(g);
    }
    int res = min(f[k], f[k + 1]);
    if (res == inf) cout << "-1\n";
    else cout << res << "\n";
    return;
}

i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int T;
    for (cin >> T; T; T--)
        solve();
    return 0;
}

标签:966,int,vi,Codeforces,long,cin,solve,using,Div
From: https://www.cnblogs.com/PHarr/p/18371653

相关文章

  • 题解:Codeforces Round 967 (Div. 2) [暴力/贪心]
    A.MakeAllEqualtimelimitpertest:1secondmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputYouaregivenacyclicarray\(a_1,a_2,\ldots,a_n\).Youcanperformthefollowingoperationon\(a\)atmost\(n-......
  • Codeforces Round 967 (Div. 2) C题 类分治解法
    废话不多说,先上代码t=int(input())whilet>0:n=int(input())pre_d={1:[iforiinrange(2,n+1)]}pair_l=[]whilelen(pre_d)!=0:item=pre_d.items()now_d={}fork,vinitem:forii......
  • Codeforces Round 967 (Div. 2)
    A.MakeAllEqual题意:给定n个数每次可以选2个相邻的数,并且前面的数不能比后面的数大,并且删除其中的一个。这个数组是循环数组,最后一个数是第一个数的前一个数。问最少操作多少次,可以让剩下的数全都相等。思路:红黑树+一次遍历,记录出现次数最多的数,剩下的数全部删掉即可。总结:看......
  • CodeForces 360D Levko and Sets
    洛谷传送门CF传送门求出\(p\)的原根\(g\),对每个\(a_i\)求出一个\(x_i\)表示\(g^{x_i}\equiva_i\pmod{p}\)(这部分可以BSGS)。之后的表述中\(a_i\)指\(x_i\)。那么集合生成方式相当于初始\(c=0\),每次让\(c\gets(c+a_ib_j)\bmod(p-1)\)。根据裴蜀定......
  • 【LGR-196-Div.4】洛谷入门赛 #26 题A - H 详细题解--优化思路简洁代码(C++,Python语
    前言:    觉得这个比赛很有意思的,都是暴力题,涉及一些细节,难度比较适合刚学编程语言的,可以很好的锻炼基础还有手速,最后两题也是比较有意思,之后也准备更新atc的比赛题解和洛谷的一些高质量比赛题解(算法网瘾就是想参加各种比赛)   如果觉得有帮助,或者觉得我写的好,......
  • Educational Codeforces Round 168 (Rated for Div. 2) D题
    文章目录题目来源题意思路code题目来源D.MaximizetheRoot题意给定一棵n个点的数,根节点为1,每个点都有权值aia_i......
  • Codeforces Round 966 (Div. 3) (A~F)
    文章目录写在前面A-PrimaryTask思路codeB.SeatinginaBus思路codeC-NumericStringTemplate思路codeD-RightLeftWrong思路codeE-PhotoshootforGorillas思路codeF-ColorRowsandColumns思路codeCodeforcesRound966(Div.3)写在前面赛时写的......
  • cf966:E. Photoshoot for Gorillas(一个格子被多少个方格包裹了)
    题目你非常喜欢大猩猩,于是决定为它们组织一次拍摄活动。大猩猩生活在丛林中,丛林被表示为一个有......
  • CF Round 966 Div3
    A给定一个字符串,判断是不是大于等于10210^2102的形式,例如......
  • Codeforces 169 Div2
    AClosestPoint由题意可得三个及以上的点无法插入新的点,只有两个点时可以插入但当两个点间隔为1时同样无法插入先判断,后输出就行#include<bits/stdc++.h>usingnamespacestd;constintN=50;intt,n;inta[N];intmain(){ cin>>t; while(t--){ cin>>n; for(i......