头像

合计飞

中北大学




在线 


活动打卡代码 AcWing 1343. 挤牛奶

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 5005;

struct Farmer
{
    int l ,r;

    bool operator < (const Farmer &f) const
    {
        return l < f.l;
    }
}farmer[N];
int n;

int main()
{
    cin >> n;

    for(int i = 0; i < n; i ++)
    {
        int l, r;
        cin >> l >> r;
        farmer[i] = {l, r};
    }

    sort(farmer, farmer + n);

    int x = 0, y = 0, l = 0, r = 0;
    for(int i = 0; i < n; i ++)
    {
        auto f = farmer[i];
        if(!i) 
        {
            l = f.l, r = f.r;
            x = r - l;
        }
        else
        {
            if(f.l <= r) r = max(r, f.r);
            else
            {
                x = max(x, r - l);
                y = max(y, f.l - r);
                l = f.l, r = f.r;
            }
        }
    }

    cout << x << " " << y << endl;
    return 0;
}


活动打卡代码 AcWing 1341. 十三号星期五

#include<iostream>
#define YEAR 1900
using namespace std;

int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool rn(int y)
{
    if(y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) 
        return true;

    return false;
}
int n;
int ans[7];

int main()
{
    cin >> n;

    int x = 1;

    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j < 12; j ++)
        {
            int y = month[j];
            if(j == 1 && rn(YEAR + i)) y ++;

            for(int k = 1; k <= y; k ++)
            {
                if(k == 13) ans[x] ++;
                x = (x + 1) % 7;
            }
        }
    }
    cout << ans[6] << " " << ans[0];
    for(int i = 1; i < 6; i ++)
        cout << " " << ans[i];

    puts("");
    return 0;
}


活动打卡代码 AcWing 125. 耍杂技的牛

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 50010;

struct Cow
{
    int w, s;

    bool operator < (const Cow &W) const
    {
        return w + s < W.w + W.s;
    }
}cows[N];
int n;
int main()
{
    scanf("%d", &n);

    for(int i = 0; i < n; i ++) 
    {
        int w, s;
        scanf("%d%d", &w, &s);
        cows[i] = {w, s};
    }

    sort(cows, cows + n);

    int ma = -2e9, x = 0;

    for(int i = 0; i < n; i ++)
    {
        ma = max(ma, x - cows[i].s);
        x += cows[i].w;
    }

    printf("%d", ma);

    return 0;
}


活动打卡代码 AcWing 104. 货仓选址

#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100010;

int a[N];

int main()
{
    int n;
    scanf("%d", &n);

    for(int i = 0; i < n; i ++) scanf("%d", &a[i]);

    sort(a, a + n);

    int res = 0, mid = n / 2;
    for(int i = 0; i < n; i ++) res += abs(a[i] - a[mid]);

    printf("%d", res);
    return 0;
}


活动打卡代码 AcWing 913. 排队打水

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100010;
typedef long long ll;

int a[N], b[N];

int main()
{
    int n;
    scanf("%d", &n);

    for(int i = 0; i < n; i ++) scanf("%d", &a[i]);

    sort(a, a + n);

    for(int i = 1; i < n; i ++) b[i] = b[i - 1] + a[i - 1];

    ll res = 0;
    for(int i = 0; i < n; i ++) res += b[i];

    printf("%lld", res);

    return 0;
}


活动打卡代码 AcWing 148. 合并果子

#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>

using namespace std;
const int N = 10010;

int a[N];

int main()
{
    int n;
    scanf("%d", &n);

    priority_queue<int, vector<int>, greater<int>> q;

    for(int i = 0; i < n; i ++) 
    {
        scanf("%d", &a[i]);
        q.push(a[i]);
    }

    int res = 0;

    while(q.size() > 1) 
    {
        int x = q.top();q.pop();
        int y = q.top();q.pop();

        res += (x + y);

        q.push(x + y);
    }

    printf("%d\n", res);
    return 0;
}


活动打卡代码 AcWing 907. 区间覆盖

#include<iostream>
#include<algorithm>

using namespace std;
const int N = 100010;

struct Range
{
    int l, r;
    bool operator < (const Range &w) const
    {
        return l < w.l;
    }
}range[N];

int main()
{
    int n, a, b;

    scanf("%d%d%d", &a, &b, &n);

    for(int i = 0; i < n; i ++) 
    {
        int l, r;
        scanf("%d%d", &l, &r);
        range[i] = {l, r};
    }

    sort(range, range + n);
    int res = 0;

    int r = -2e9;
    for(int i = 0; i < n; i ++)
    {
        int j = i;
        r = -2e9;
        while(j < n && range[j].l <= a)
        {
            r = max(r, range[j].r);
            j ++;
        }

        if(r < a)
        {
            res = -1;
            break;
        }

        res ++;
        a = r;
        i = j - 1;

        if(r >= b) break;
    }

    if(r < b) res = -1;
    printf("%d", res);
    return 0;
}


活动打卡代码 AcWing 906. 区间分组

#include<iostream>
#include<queue>
#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;
const int N = 100010;

struct Range{
    int l, r;
    bool operator < (const Range &w) const
    {
        return l < w.l;
    }
}range[N];
int main()
{
    int n;
    scanf("%d", &n);

    for(int i = 0; i < n; i ++) 
    {
        int l, r;
        scanf("%d%d", &l, &r);
        range[i] = {l, r};
    }

    sort(range, range + n);
    priority_queue<int, vector<int>, greater<int>> heap;

    for(int i = 0; i < n; i ++)
    {
        if(heap.empty() || range[i].l <= heap.top()) heap.push(range[i].r);
        else {
            heap.pop();
            heap.push(range[i].r);
        }
    }

    printf("%d\n", heap.size());
    return 0;
}



#include<iostream>
#include<algorithm>

using namespace std;
const int N = 100010;

struct Range {
    int l, r;

    bool operator < (const Range &w)const
    {
        return r < w.r;
    }
}range[N];

int main()
{
    int n;
    scanf("%d", &n);

    for(int i = 0; i < n; i ++)
    {
        int l, r;
        scanf("%d%d", &l, &r);

        range[i] = {l, r};
    }

    sort(range, range + n);

    int res = 0, end = -2e9;
    for(int i = 0; i < n; i ++)
        if(range[i].l > end)
        {
            res ++;
            end = range[i].r;
        }

    printf("%d\n", res);
    return 0;
}


活动打卡代码 AcWing 905. 区间选点

#include<iostream>
#include<algorithm>

using namespace std;
const int N = 100010;

struct Range {
    int l, r;

    bool operator < (const Range &w)const
    {
        return r < w.r;
    }
}range[N];

int main()
{
    int n;
    scanf("%d", &n);

    for(int i = 0; i < n; i ++)
    {
        int l, r;
        scanf("%d%d", &l, &r);

        range[i] = {l, r};
    }

    sort(range, range + n);

    int res = 0, end = -2e9;
    for(int i = 0; i < n; i ++)
        if(range[i].l > end)
        {
            res ++;
            end = range[i].r;
        }

    printf("%d\n", res);
    return 0;
}