牛客周赛 Round 46 题解 C++

目录

[A 乐奈吃冰](#A 乐奈吃冰)

[B 素世喝茶](#B 素世喝茶)

[C 爱音开灯](#C 爱音开灯)

[D 小灯做题](#D 小灯做题)

[E 立希喂猫](#E 立希喂猫)

[F 祥子拆团](#F 祥子拆团)


A乐奈吃冰

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath> 
#include <queue>
#include <set>
#include <vector>
#include <unordered_map>

using namespace std;

typedef pair<int,int> PII;
typedef long long ll;

const int N = 3e5 + 10,M = 1e9 + 7;

int n,m;
int a[N],b[N];
bool st[N];
ll cnt;

void solve()
{
    cin >> n >> m;
    if(n == 1)
    {
        cout << 1;
        return ;
    }
    if(m > n / 2)
    {
        cout << n / 2 + n;
    }
    else
    {
        cout << n + m;
    }
}

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    while(t --)
    {
        solve();
    }
    return 0;
}

B 素世喝茶

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath> 
#include <queue>
#include <set>
#include <vector>
#include <unordered_map>

using namespace std;

typedef pair<int,int> PII;
typedef long long ll;

const int N = 3e5 + 10,M = 1e9 + 7;

int n,m;
int a[N];
bool st[N];
ll cnt;

void solve()
{
    cin >> n >> m;
    int mx = 0;
    for(int i = 1; i <= n; i ++)
    {
        int x;
        cin >> x;
        if(i == m) continue;
        if(x > mx)
        {
            mx = x;
            cnt = 1;
        }
        else if(x == mx){
            cnt ++;
        }
    }
    cout << cnt;
}

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    while(t --)
    {
        solve();
    }
    return 0;
}

C 爱音开灯

cpp 复制代码
#include <iostream>
#include <cmath>

using namespace std;

void solve() {
    long long n, m;
    cin >> n >> m;
    long long cnt = 0;
    for (long long i = 1; i * i <= m; i++) {
        if(m % i == 0)
        {
            if(i<=n) cnt++;
            if(m/i!=i&&m/i<=n) cnt++;
        }
    }
    if (cnt % 2 == 1) {
        cout << "ON";
    } else {
        cout << "OFF";
    }
}

int main() {
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    while (t--) {
        solve();
    }
    return 0;
}

D 小灯做题

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath> 
#include <queue>
#include <set>
#include <vector>
#include <unordered_map>

using namespace std;

typedef pair<int,int> PII;
typedef long long ll;

const int N = 3e5 + 10,M = 1e9 + 7;

ll n,m;
int a[N];
bool st[N];
ll cnt;
int k,res;

int mex(int a,int b){
    if(a + b == 1)
    {
        return 2;
    }
    else
    {
        if(a == 0 || b == 0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
}

void dfs(int a,int b,int c,int sum){
    
    if(sum > 3 || a == k || b == k || c == k){
        res=min(res,sum);
        return ;
    }
    
    dfs(a,b,mex(a,b),sum+1);
    dfs(a,mex(a,c),c,sum+1);
    dfs(mex(b,c),b,c,sum+1);
}

void solve()
{
    cin >> n;
    while(n --)
    {
        int a,b,c;
        cin >> a >> b >> c >> k;
        res = N;
        if(a == k || b == k || c == k)
        {
            cout << 0 << "\n";
            continue;
        }
        if(k >= 3)
        {
            cout << -1 << "\n";
            continue;
        }
        dfs(a,b,c,0);
        cout << res << "\n";
    }

}
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    while(t --)
    {
        solve();
    }
    return 0;
}

E 立希喂猫

cpp 复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath> 
#include <queue>
#include <set>
#include <vector>
#include <unordered_map>

using namespace std;

typedef pair<int,int> PII;
typedef long long ll;
#define x first
#define y second

const int N = 3e5 + 10,M = 1e9 + 7;

ll n,m;
ll s1[N],s2[N];
ll c[N];
bool st[N];
ll cnt;

PII a[N];

void solve()
{
    cin >> n;

    for(int i = 1; i <= n ;i ++)
    {
        cin >> a[i].y;
    }

    for(int i = 1; i <= n ;i ++)
    {
        cin >> a[i].x;
    }

    sort(a+1,a+1+n);

    for(int i = 1; i <= n ; i ++)
    {
        s1[i] += s1[i-1] + a[i].y * a[i].x;
        s2[i] += s2[i-1] + a[i].y;
    }

    cin >> m;

    while(m --)
    {
        int t;
        cin >> t;

        ll res = 0;
        pair<int,int> p(t,0);
        int id_1 = upper_bound(a + 1,a+ n + 1,p) - a;
        res += s1[id_1 - 1];
        res += (s2[n] - s2[id_1 - 1]) * t; 
        cout << res << "\n";
    }
    return ;
}
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    while(t --)
    {
        solve();
    }
    return 0;
}

F 祥子拆团

没代码思路,后面补

相关推荐
Blossom.1184 分钟前
把AI“贴”进路灯柱:1KB决策树让老旧路灯自己报「灯头松动」
java·人工智能·python·深度学习·算法·决策树·机器学习
QT 小鲜肉9 分钟前
【QT/C++】Qt网络编程进阶:TCP网络编程的基本原理和实际应用(超详细)
c语言·开发语言·网络·c++·qt·学习·tcp/ip
❀͜͡傀儡师17 分钟前
快速定位并解决Java应用CPU占用过高问题
java·开发语言·python
艾莉丝努力练剑29 分钟前
【C++:map和set的使用】C++ map/multimap完全指南:从红黑树原理入门到高频算法实战
大数据·开发语言·c++·人工智能·stl·map
汤姆yu29 分钟前
基于大数据的全国降水可视化分析预测系统
大数据·开发语言·python
VBA63371 小时前
VBA信息获取与处理专题五第三节:发送带附件的电子邮件
开发语言
元亓亓亓1 小时前
Leet热题100--208. 实现 Trie (前缀树)--中等
java·开发语言
拾荒的小海螺1 小时前
C#:OpenCvSharp 实现图像处理的技术指南
开发语言·图像处理·c#
墨染点香2 小时前
LeetCode 刷题【144. 二叉树的前序遍历】
数据结构·算法·leetcode
自由随风飘6 小时前
python 题目练习1~5
开发语言·python