牛客一起来做题~欢乐赛5题解

真实爆零现场 好长时间没有码,手生了~
(题的话说实话都是基础题 别骂了
比赛不补题 等于没有比 补题抄题解 一月忘干净 训练法不对 亲人两行泪

比赛不补题

题解:这道题就是单纯的二分题目
stl里面的二分函数安利一下
binary_search:查找某个元素是否出现 binary_search(arr[],arr[]+size , indx)
lower_bound:查找第一个大于或等于某个元素的位置 lower_bound(arr[],arr[]+size , indx)
upper_bound:查找第一个大于某个元素的位置 upper_bound(arr[],arr[]+size , indx)

#include <algorithm>
#include <iostream>

using namespace std;

const int N = 1e5 + 10;
typedef long long ll;
ll n, m, x, y;
ll a[N];
ll d, u;

int check(ll d)
{
    for (int i = 1;i <= n; i ++)
    {
        if (binary_search(a + i + 1, a + n + 1, d + a[i]) == 0) continue;
        if (d == x || d == y) return 1;
        if (d == y - x){
            if (a[i] > x) {
                u = a[i] - x;
                return 1;
            }
            if (a[i] + y <= m) {
                u = a[i] + y;
                return 1;
            }
        }
        if (d == y + x) {
            u = a[i] + x;
            return 1;
        }
    }
    return 0;
}

int main()
{
    cin >> n >> m >> x >> y;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    if (check(x) && check(y)) cout << 0 << endl;
    else if (check(x) || check(y)) {
            cout << 1 << endl;
            if (check(x)) cout << y << endl;
            else cout << x << endl;
    }
    else if (check(y - x)|| check(y + x)) cout << 1 << "\n" << u << endl;
    else {
        cout << 2 << endl;
        cout << x << " " << y << endl;
    }
    return 0;
}

等于没有比

题解:水题 当时我还在想怎么贪心结果贪了个寂寞 嘤嘤嘤~(还是做题太少

#include <algorithm>
#include <iostream>

using namespace std;

int main()
{
    long long a[5];
    cin >> a[1] >> a[2] >> a[3];
    sort(a + 1, a + 4);
    if (2 * (a[1] + a[2]) <= a[3]) cout << a[1] + a[2] << endl;
    else {
        cout << (a[1] + a[2] + a[3]) / 3 << endl;
    }
    return 0;
}

补题抄题解

这不是说的就是我吗(手动狗头
这个题我开始想的是不是要并查集呀但是我看见数据范围懵逼了1e9~
可是看了题解的确是并查集(可以用unordered_map存欸

#include <algorithm>
#include <iostream>
#include <unordered_map>

using namespace std;

int n, t;
int a, b, c;
int d;

unordered_map<int,int>f;
int find(int x)
{
    if (f[x] == 0) return x;
    return f[x] = find(f[x]);
}

int main()
{
    scanf("%d",&t);
    while(t --)
    {
        scanf("%d",&n);
        f.clear();
        d = 0;
        for (int i = 1;i <= n; i ++)
        {
            scanf("%d%d%d",&a,&b,&c);
            int l = find(a), r = find(b);
            if (c)
            {
                if (l != r && l != a && r != b) d = 1;//只有a和b友好 并且a和b都有自己的团体的情况下
                else f[l] = r;
            }
            else {
                if (l == r) d = 1;
            }
        }
        if (d) cout << "NO" << endl;
        else cout << "YES" << endl;
    }
    return 0;
}

一月忘干净

动态规划(

训练法不对

动态规划(

亲人两行泪

差分
给区间[l, r]中的每个数加上c:B[l] += c, B[r + 1] -= c
区间合并

#include <algorithm>
#include <iostream>

using namespace std;

const int N = 1e5 + 10;
struct hw{
    int l, r, d;
};
hw a[N];
int b[N];
int l ,r, d;

bool cmp(hw x, hw y)
{
    if (x.d != y.d) return x.d < y.d;
    return x.l < y.l;
    //else return x.r < y.r;
}

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m;
    cin >> n >> m;
    for (int i = 1; i <= m; i ++)
    {
        cin >> l >> r >> d;
        a[i] = {l, r, d};
    }
    sort(a + 1, a + m + 1, cmp);
    l = a[1].l, r = a[1].r, d = a[1].d;
    for (int i = 1; i <= n; i ++)
    {
        if (d == a[i].d && r >= a[i].l)
        {
            r = max(r, a[i].r);
        }
        else if (d == a[i].d && r < a[i].l)
        {
            b[l]++;
            b[r + 1]--;
            l = a[i].l;
            r = a[i].r;
        }
        else if (d != a[i].d)
        {
            b[l]++;
            b[r + 1]--;
            l = a[i].l, r = a[i].r, d = a[i].d;
        }
    }
    b[l]++;
    b[r + 1]--;
    int num = 0, maxx = 0, j = 0;
    for (int i = 1; i <= 100000; i ++)
    {
        num += b[i];
        if (num > maxx)
        {
            maxx = num;
            j = i;
        }
    }
    cout << j << endl;
}

D题和E题等我学会了再来补题解吧
欢迎一起讨论~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值