首页 > 其他分享 >Educational Codeforces Round 123

Educational Codeforces Round 123

时间:2023-09-01 22:33:17浏览次数:38  
标签:Educational return int Codeforces long pos -- 123 solve

A. Doors and Keys

#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
    string s;
    cin >> s;
    map<char,int> pos;
    for( int i = 0 ; i < 6 ; i ++ )
        pos[s[i]] = i;
    if( pos['r'] < pos['R'] and pos['g'] < pos['G'] and pos['b'] < pos['B'] )
        cout << "YES\n";
    else
        cout << "NO\n";

}

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

B. Anti-Fibonacci Permutation

暴力枚举前两位,暴搜后面的。

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 55;
array<int, N> a, vis;
int n , cnt;

void dfs( int i ){
    if( cnt == n ) return;
    if( i == n+1 ){
        for( int i = 1 ; i <= n ; i++ )
            cout << a[i] << " ";
        cout << "\n";
        cnt ++;
        return ;
    }
    for( int j = 1 ; j <= n ; j ++ ){
        if( vis[j] or j == a[i-1] + a[i-2] ) continue;
        a[i] = j , vis[j] = 1;
        dfs( i + 1 );
        vis[j] = 0;
    }
    return ;
}

void solve() {
    cin >> n , cnt = 0;
    fill(vis.begin(), vis.end(), 0);
    for (a[1] = 1; a[1] <= n and cnt != n ; a[1]++)
        for (a[2] = 1; a[2] <= n and cnt != n ; a[2]++)
            if (a[1] != a[2])
                vis[a[1]] = vis[a[2]] = 1, dfs(3), vis[a[1]] = vis[a[2]] = 0;
}

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

C. Increase Subarray Sums

\(f[i][j]\)表示前\(i\)位加了\(j\)个\(x\)的最大值。

因为题目要求了连续子区间,所以转移只有两类,一类是自己做起点,另一类是从前一个转移过来。

#include <bits/stdc++.h>

using namespace std;

#define int long long

void solve() {
    int n , x;
    cin >> n >> x;
    vector<int> a(n+1) , res(n+1);
    for( int i = 1 ; i <= n ; i ++ ) cin >> a[i];
    vector<vector<int>> f( n+1 , vector<int>(n+1 , INT_MIN));
    for( int i = 1 ; i <= n ; i ++ ){
        for( int j = 0 ; j <= n ; j ++ ){
            f[i][j] = max( a[i] , f[i-1][j] + a[i] );
            if( j > 0 ) f[i][j] = max( { f[i][j] , a[i] + x , f[i-1][j-1] + a[i] + x } );
            f[i][j] = max( f[i][j] , 0ll );
            res[j] = max( res[j] , f[i][j] );
        }
    }
    for( auto i : res )
        cout << i << " ";
    cout << "\n";
    return ;
}


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

D. Cross Coloring

每一行,每一列只有最后一次染色有效,所以我们倒序来做。

维护每一行每一列是否被染色。并且如果所有的行或列都被染色了,就不能继续染了。

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int mod = 998244353;

const int N = 2e5 + 5;
array<int, N> x, y, r, c;

void solve() {
    int n, m, k, q;
    cin >> n >> m >> k >> q;
    fill(x.begin(), x.end(), 0);
    fill(y.begin(), y.end(), 0);
    for (int i = 1; i <= q; i++) cin >> r[i] >> c[i];
    int res = 1;
    for (int i = q, cntX = 0, cntY = 0; i >= 1; i--) {
        if (cntX == n or cntY == m or (x[r[i]] + y[c[i]] == 2))
            continue;
        if (x[r[i]] == 0)
            x[r[i]] = 1, cntX++;
        if (y[c[i]] == 0)
            y[c[i]] = 1, cntY++;
        res = res * k % mod;
    }
    cout << res << "\n";
    return;
}


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

E. Expand the Path

这道题我认为写的最好的就是官解。

能够被访问的单元格一在下两条路径所形成的空间内:

  • 第一个R重复次数最多,然后最后一个D重复次数最多;
  • 第一个D重复次数最多,然后最后一个R重复次数最多。

这点其实还是比较好想的,官解大部分篇幅就是在证明这一点,我们直接跳过就好了。

求解中间部分的面积是本题的难点。实际上我们可以知道的是空的部分一定是左上角和右下角。我们考虑求出这两部分的面积即可。

对于左上角,我们从下往上一行一行的看,当前行空了\(y\)列。我们倒序看操作数组,如果s[i]=R,则y++,否则会向上移动一行,此时我们要把y累加到答案中去。只要模拟这个过程就可以算出左上角的面积。

对于右下角,我们翻转原序列统计过程就和上一步完全相同。

官解最妙其实是写法,不需要真的去扩展数组,

这里看到了,其实第一个\(R\)的目的是为了把其他的操作推到最后后面,所以第一个\(R\)之前对应的\(D\)累加的都是\(n-1\),这也是为什么要倒序处理操作序列。

#include <bits/stdc++.h>

using namespace std;

#define int long long

int calc(string s, int n) {
    int l = s.find('R'), ans = l * (n - 1);
    for (int i = s.size() - 1, y = 0; i > l; i--) {
        if (s[i] == 'D') ans += y;
        else y++;
    }
    return ans;
}

void solve() {
    int n;
    string s;
    cin >> n >> s;
    if (s == string(s.size(), s[0])) {
        cout << n << "\n";
        return;
    }
    int res = n * n;
    res -= calc(s, n);
    for (auto &i: s)
        i = (i == 'R' ? 'D' : 'R');
    res -= calc(s, n);
    cout << res << "\n";
    return;
}

int32_t main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int t;
    for (cin >> t; t; t--)
        solve();
    return 0;
}

标签:Educational,return,int,Codeforces,long,pos,--,123,solve
From: https://www.cnblogs.com/PHarr/p/17672981.html

相关文章

  • Educational Codeforces Round 15 A - E
    EducationalCodeforcesRound15目录EducationalCodeforcesRound15A-MaximumIncreaseB-PowersofTwoC-CellularNetworkD-RoadtoPostOfficeE.AnalysisofPathesinFunctionalGraphA-MaximumIncrease一段上升子数组\([l,r]\)最大化\(r-l+1\),我们......
  • Educational Codeforces Round 5 A-E
    EducationalCodeforcesRound5垫底咯,中间老师找我去聊了下新学期的机房借用和训练,但出去前就只有我没出E目录EducationalCodeforcesRound5AComparingTwoLongIntegersBDinnerwithEmmaCTheLabyrinthDLongestk-GoodSegmentE-SumofRemaindersAComparingTwo......
  • COMP123 2D图形算法难点讨论
    COMP123Primitive2DDrawingAssignmentSpecificationInthisassignment,youwillberequiredtoimplementsomeofthealgorithmsthatwehavediscussedinlectures.Youwillneedtowriteagenericframebufferclassthatisabletorepresentimagesandd......
  • Educational Codeforces Round 148 (Rated for Div. 2)E. Combinatorics Problem(组合
    题目链接:https://codeforces.com/contest/1832/problem/E 题意:  当然这是化简后的题意,原题面和这个差距还是有点大的; 分析: 因为组合数有公式:  所以:   嗯,然后就没有了; 时间复杂度:O(n*k); 代码: #include<bits/stdc++.h>#defineintlonglong......
  • Educational Codeforces Round 118
    好烦,又是只有三题,讲课的老师实在是太吵了,没法思考细节A题开始还搞麻烦了B题纯诈骗,找最小的做y即可C题直接二分判断即可D题其实没看多久我就秒了,对于当前的数x来说无非就是mex=x-1mex=xmex=x+1\(f[x]\)表示mex=x,后面没有数\(g[x]\)表示mex=x,后面有x+1,并且只可能是x+1,不可......
  • 基于友晶科技 FPGA开发板 DE2-115、DE1-SOC 和 DE10-STANDARD 的VGA图片显示(ADV7123)
      选择一个图 调整像素 转换成mif文件   ......
  • Codeforces Round 888 (Div. 3)G. Vlad and the Mountains(数据结构,图论)
    题目链接:https://codeforces.com/contest/1851/problem/G 大致题意: 给出n个点m条边的无向图,每个点有点权h【i】。从点i到点j会消耗h【j】-h【i】的能量,如果小于0,那么就是恢复对应绝对值的能量。 进行q次询问,每次询问包含起点s,终点t,能量e,能量在移动过程中不能小......
  • Codeforces Round 887 (Div. 1)C. Ina of the Mountain(数据结构,反悔贪心)
    题目链接:https://codeforces.com/problemset/problem/1852/C 题意: 给定一个长度为n的序列和正整数k; 每次可以选取任意一个区间,将区间内每个数减1; 如果出现一个数变成0,那么那个数变成k; 问至少操作多少次可以使得每个数变成k; 分析: 将每个数值抽象为对应高度的......
  • Codeforces Round 892 (Div. 2)E. Maximum Monogonosity(动态规划,数学)
    题目链接:https://codeforces.com/contest/1859/problem/E 题意: 有长度为n的a和b俩个序列,定义f【l,r】=abs(a【l】-b【r】)+abs(b【l】-a【r】); 给正整数k,求 不相交的区间且  所有  区间的长度 的 和 为k的最大值 是多少? 分析: 这里借鉴一个佬......
  • Educational Codeforces Round 151 (Rated for Div. 2)E. Boxes and Balls(数学,动态规
    题目链接:https://codeforces.com/contest/1845/problem/E 题意: 给定长度为n且只含0和1的数组,你可以进行以下操作: 交换相邻的0和1; 给正整数k,问经过k次操作后,会有多少种本质不同的结果; 分析: 如果1比0多,我们可以把他们取反(让0比1多,结果是一样的) 设计状态dp[i][j......