首页 > 编程语言 >2024牛客寒假算法基础集训营1

2024牛客寒假算法基础集训营1

时间:2024-02-06 11:36:12浏览次数:35  
标签:count int res ++ cin 2024 牛客 solve 集训营

2024牛客寒假算法基础集训营1

A.DFS搜索

思路:看dfs其实就是一道字符题目

#include <bits/stdc++.h>

using namespace std;
string x = "dfs";
string y = "DFS";

void solve() {
    int n;
    cin >> n;
    string st;
    cin >> st;
    int xx = 0, yy = 0;
    for (int i = 0; i < n; i++) {
        if (st[i] == x[xx]) xx++;
        if (st[i] == y[yy]) yy++;
    }
    if (xx == 3) xx = 1;
    else xx = 0;
    if (yy == 3) yy = 1;
    else yy = 0;
    cout << (yy) << " " << (xx) << endl;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

B.关鸡

思路:1.最多就3个就是(0,1)(0,-1)(1,0) 2.左右怎么才能不出去? 左右都被堵死 3.如何堵死? 左右同一根线或者相距一格

#include <bits/stdc++.h>

using namespace std;
#define int long long

void solve() {
    int n;
    cin >> n;
    if (n == 0) {
        cout << "3" << endl;
        return;
    }
    set<pair<int, int>> q;
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        q.insert({a - 1, b});
    }

    int res = 4;
    int ll = 2;
    int rr = 2;
    int a = q.count({0, -1}), b = q.count({0, 1}), c = q.count({1, 0});
    if (c) {
        ll = 1;
        rr = 1;
    }
    if (a || b || c) {
        res = min(res, 2ll);
        if (a && b || a && c || b && c) {
            res = min(res, 1ll);
        }
        if (a && b && c) {
            res = 0;
        }
    }
    for (auto [r, c]: q) {
        if (c < 0) {
            if (q.count({r ^ 1, c - 1}) || q.count({r ^ 1, c + 1}) || q.count({r ^ 1, c})) {
                ll = 0;
            } else {
                ll = min(ll, 1ll);
            }
        } else if (c > 0) {
            if (q.count({r ^ 1, c - 1}) || q.count({r ^ 1, c + 1}) || q.count({r ^ 1, c})) {
                rr = 0;
            } else {
                rr = min(rr, 1ll);
            }
        }
    }
    res = min(res, ll + rr);
    cout << res << endl;
}

signed main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

C.按闹分配

思路:很明显不满意度最小的时候就是前面时间短,后面时间长,可以用前缀和来维护一下
$$
S_c-S_min=n-i(假设他是第i个去的)
$$

#include <bits/stdc++.h>

using namespace std;
#define int long long

signed main() {
    int n, Q, tc;
    cin >> n >> Q >> tc;
    vector<int> t(n);
    vector<int> s(n + 2);
    for (int i = 0; i < n; i++) {
        cin >> t[i];
    }
    sort(t.begin(), t.end());
    for (int i = 0; i < n; i++) {
        s[i + 1] = s[i] + t[i];
    }
    while (Q--) {
        int m;
        cin >> m;
        int c = min(n, m / tc);
        int ans = s[n - c] + tc;
        cout << ans << endl;
    }
}

E.本题又主要考查了贪心

思路:这题mn很小,根本不用想就狠狠暴力就行(debug搞了好久、、),后面发现是我后面ans的问题、、、

#include <bits/stdc++.h>

using namespace std;
int m, n, num, ans;
const int MAX = 1e6;
int a[MAX], v[MAX], u[MAX];


void dfs(int x) {
    if (x == num) {
        int res = 0;
        for (int i = 1; i < n; i++) {
            if (a[i] > a[0])res++;
        }
        ans = min(ans, res);
        return;
    }
    a[u[x]] += 3;
    dfs(x + 1);
    a[u[x]] -= 3;
    a[u[x]] += 1;
    a[v[x]] += 1;
    dfs(x + 1);
    a[u[x]] -= 1;
    a[v[x]] -= 1;
    a[v[x]] += 3;
    dfs(x + 1);
    a[v[x]] -= 3;
}

void solve() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    num = 0;
    for (int i = 0; i < m; i++) {
        int u1, v1;
        cin >> u1 >> v1;
        if (u1 == 1 || v1 == 1)a[0] += 3;
        else {
            u[num] = u1 - 1;
            v[num] = v1 - 1;
            num++;
        }
    }
    ans = 1e6;
    dfs(0);
    cout << min(ans + 1, n) << endl;
}

signed main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

G.why买外卖

思路:只要超过就能用,那假设都能用,怎么会出现不能用的? 减去还不行的那种

#include <bits/stdc++.h>

using namespace std;
#define int long long

void solve() {
    int n, m;
    cin >> n >> m;
    vector<pair<int, int>> q(n + 1);
    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        q[i].first = a;
        q[i].second = b;
        m+=b;
    }
    sort(q.rbegin(), q.rend());
    for(int i=0;i<n;i++){
        if(m>=q[i].first) break;
        else{
            m-=q[i].second;
        }
    }
    cout << m << endl;
}

signed main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

L.要有光

思路:纯纯几何题,画个图推一下就行

#include <bits/stdc++.h>

using namespace std;

void solve() {
    int c, d, h, w;
    cin >> c >> d >> h >> w;
    cout << 3 * c * w << endl;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

M.牛客老粉才知道的秘密

思路:就是一个找规律的题就是看与6的关系

#include <bits/stdc++.h>

using namespace std;
#define int long long

void solve() {
    int n;
    cin >> n;
    if (n % 6 == 0) {
        cout << n / 6 << "\n";
        return;
    }
    int x = n / 6 + 1;
    x = x * 2 - 2;
    cout << x << endl;
}

signed main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

标签:count,int,res,++,cin,2024,牛客,solve,集训营
From: https://www.cnblogs.com/bbbbear/p/18009436

相关文章

  • 2024年1月文章一览
    2024年1月编程人总共更新了6篇文章:1.2023年12月文章一览2.ProgrammingAbstractionsinC阅读笔记:p242-p2453.ProgrammingAbstractionsinC阅读笔记:p246-p2474.ProgrammingAbstractionsinC阅读笔记:p248-p2535.ProgrammingAbstractionsinC阅读笔记:p254-p2576.ProgrammingAb......
  • 2024年1月文章一览
    2024年1月编程人总共更新了6篇文章:1.2023年12月文章一览2.ProgrammingAbstractionsinC阅读笔记:p242-p2453.ProgrammingAbstractionsinC阅读笔记:p246-p2474.ProgrammingAbstractionsinC阅读笔记:p248-p2535.ProgrammingAbstractionsinC阅读笔记:p254-p2576.Program......
  • 2024 NOIWC 游记
    复盘前天晚上去试机,调了一下Geany的配置。这玩意确实好用,很合我胃口。进赛场,发现给了吃的和牛奶,非常良心啊。PKUWC甚至没有清试机留下来的东西,WC清掉了,比较可惜。先看题。看T1,不会。看T2,感觉比较可做。看T3,这是啥玩意??于是开始想T2。发现了一些性质,注意到一个区间在\(L......
  • 复杂系统 | 20240116 · 考试题目回忆版
    相关链接:RL基础|ValueIteration的收敛性证明RL基础|PolicyIteration的收敛性证明复杂系统|考前知识点总结(不完全)“嵌套分区法,是一种良策;将海洋分成块,每块都探测。”概述:基于事件的优化方法/事件驱动优化/Event-BasedOptimization/EBO十个判断题,感觉......
  • 对话苏光牛:国内数据库市场已进入关键转折点,2024年或是分水岭
    “中国数据库市场已进入关键阶段,2024年或是分水岭!”“目前,国内数据库产品数量接近300款,我们真的需要这么多数据库吗?”面对这个问题,华为云数据库业务CTO苏光牛不假思索地给出了他的见解:“不仅是中国市场,全球范围内,也不需要如此多的商业数据库。”他进一步预测,随着市场的自然淘汰......
  • 2024年2月5号总结
    P1194买礼物洛谷题目链接解题思路这个题目是一个最小生成树或者说是贪心的题目,在这里我们把买的东西定义成顶点,边是优惠的价格那么我们只要把每一个顶点连接起来可以了,但要注意优惠的价格​ 可能大于A,因此我们要比较A的价格和优惠的价格谁的花费少接下来就是最小生成树的......
  • WC2024 水镜
    洛谷传送门WC2024被打爆了,呜呜。我赛时会这题\(8\)分指数级暴力,哈哈。真不知道自己在干嘛。下文令\(T=2L\)。考虑如何判定一个序列\(a\)是否合法。考虑先枚举一个\(T\)。因为要求\(r_i<r_{i+1}\),考虑讨论相邻两项的取值:若\(a_i<a_{i+1}\)则\(r_i=a_i,......
  • WC2024 游记
    2月1日测试开场读三道题,题好长!T1看起来是数数,T2是神秘构造,T3还是数数。开赛后15分钟开始想T1。直接做好像不太可做啊,然后立刻想到了拆贡献看看。发现拆贡献后问题变成了背包问题,可以\(\mathcalO(nT)\)解决。看完数据范围有点惊讶,我的做法能拿满分!在去年,金牌分数线不......
  • 洛谷 P10145 [WC/CTS2024] 线段树 题解--zhengjun
    提供一种考场做法,在思路上和官方题解的差异蛮大的,虽然结果差不多。首先需要发现\([l,r)\)区间可以算出来的充要条件是:如果对于每个选中的节点\(u\),连无向边\((L_u,R_u)\),则当且仅当\(l\)和\(r\)连通时区间\([l,r)\)可以算出来。证明的话,用前缀和理解这些东西,分别考虑......
  • 2024牛客寒假集训营第二场
    总的来说,这一场还是很不错的,但是还是有做的不好的地方,比方说靠别人给了D的思路,还有思维的太慢。不过继续努力吧!A.TokitsukazeandBracelet思路:签到题,直接按着题目的意思模拟就可以了。code:点击查看代码#include<bits/stdc++.h>usingnamespace......