头像

罗斯


访客:4624

离线:2个月前


活动打卡代码 AcWing 826. 单链表

罗斯
10个月前
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;
int head, e[N], ne[N], idx;
void init()
{
    head = -1;
}
void add_head(int x)
{
    e[idx] = x, ne[idx] = head, head = idx ++ ;
}
void add_k(int k, int x)
{
    e[idx] = x, ne[idx] = ne[k], ne[k] = idx ++ ;
}
void remove(int k)
{
    ne[k] = ne[ne[k]];
}
int main()
{
    init();
    int m;
    cin >> m;
    while (m -- )
    {
        char op;
        int k, x;
        cin >> op;
        if (op == 'H')
        {
            cin >> x;
            add_head(x);
        }
        else if (op == 'I')
        {
            cin >> k >> x;
            add_k(k - 1, x);
        }
        else
        {
            cin >> k;
            if (!k) head = ne[head];
            else remove(k - 1);
        }
    }
    for (int i = head; i != -1; i = ne[i]) cout << e[i] << ' ';
    cout << endl;
    return 0;
}


活动打卡代码 AcWing 829. 模拟队列

罗斯
10个月前
#include <iostream>

using namespace std;

const int N = 100010;
int m;
int q[N], hh, tt = -1;
int main()
{
    cin >> m;
    while (m -- )
    {
        string op;
        int x;
        cin >> op;
        if (op == "push")
        {
            cin >> x;
            q[ ++ tt] = x;
        }
        else if (op == "pop") hh ++ ;
        else if (op == "empty") cout << (hh <= tt ? "NO" : "YES") << endl;
        else cout << q[hh] << endl;
    }
    return 0;
}


活动打卡代码 AcWing 828. 模拟栈

罗斯
10个月前
#include <iostream>

using namespace std;

const int N = 100010;
int m;
int stk[N], tt;
int main()
{
    cin >> m;
    while (m -- )
    {
        string op;
        int x;
        cin >> op;
        if (op == "push")
        {
            cin >> x;
            stk[ ++ tt] = x;
        }
        else if (op == "pop") tt -- ;
        else if (op == "empty")
        {
            if (tt) puts("NO");
            else puts("YES");
        }
        else cout << stk[tt] << endl;
    }
    return 0;
}



罗斯
10个月前
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;
int tree[10005]={0};
int main()
{
    int l,m,a,b,sum=0;
    cin>>l>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>a>>b;
        for(int j=a;j<=b;j++)
        {
            tree[j]=1;
        }
    }
    for(int i=0;i<=l;i++)
    {
        if(tree[i]==0)
        {
            sum++;
        }
    }
    cout<<sum<<endl;
    return 0;
} 


活动打卡代码 AcWing 803. 区间合并

罗斯
10个月前
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;
struct Seg
{
    int l, r;
}seg[N];
bool cmp(Seg a, Seg b)
{
    return a.l < b.l;
}
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ ) scanf("%d%d", &seg[i].l, &seg[i].r);
    sort(seg, seg + n, cmp);
    int res = 0;
    int st = seg[0].l, ed = seg[0].r;
    for (int i = 1; i < n; i ++ )
        if (seg[i].l > ed)
        {
            res ++ ;
            st = seg[i].l, ed = seg[i].r;
        }
        else ed = max(ed, seg[i].r);
    res ++ ;
    printf("%d\n", res);
    return 0;
}



罗斯
10个月前
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int a,b;
    cin>>a>>b;
    cout<<a+b<<endl;
    return 0;
}



罗斯
10个月前
#include<stdio.h>
#include<algorithm>

using namespace std;

int main()
{
    int w,n,a[30010],x;
    while(scanf("%d",&w)!=EOF)
    {
        scanf("%d",&n);
        x=0;
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n);
        for(int i=0,j=n-1;i<j;)
        {
            if(a[i]+a[j]<=w)
            {
                i++;j--;x++;
            }
            else
                j--;
        }
        printf("%d\n",n-x);
    }
    return 0;
}


活动打卡代码 AcWing 802. 区间和

罗斯
10个月前
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef pair<int, int> PII;
const int N = 300010;
int n, m;
vector<int> alls;
int s[N];
PII add[N], seg[N];
int find(int x)
{
    int l = 0, r = alls.size() - 1;
    while (l < r)
    {
        int mid = l + r >> 1;
        if (alls[mid] >= x) r = mid;
        else l = mid + 1;
    }
    return r + 1;
}
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i ++ )
    {
        int x, c;
        scanf("%d%d", &x, &c);
        add[i] = {x, c};
        alls.push_back(x);
    }
    for (int i = 0; i < m; i ++ )
    {
        int x, y;
        scanf("%d%d", &x, &y);
        seg[i] = {x, y};
        alls.push_back(x), alls.push_back(y);
    }
    sort(alls.begin(), alls.end());
    alls.erase(unique(alls.begin(), alls.end()), alls.end());
    for (int i = 0; i < n; i ++ ) s[find(add[i].first)] += add[i].second;
    for (int i = 1; i <= alls.size(); i ++ ) s[i] += s[i - 1];
    for (int i = 0; i < m; i ++ )
    {
        int l = find(seg[i].first), r = find(seg[i].second);
        printf("%d\n", s[r] - s[l - 1]);
    }
    return 0;
}



罗斯
10个月前
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

const int N=100010;
int n;
int a[N],s[N];
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
    int ans = 0;
    for (int i = 0, j = 0; i < n; i ++ )
    {
        int x = a[i];
        s[x] ++ ;
        while (s[x] > 1)
        {
            s[a[j]] -- ;
            j ++ ;
        }
        ans = max(ans, i - j + 1);
    }
    printf("%d\n", ans);
    return 0;
}



罗斯
10个月前
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 100010;
int n, m, x;
int a[N], b[N];
int main()
{
    scanf("%d%d%d", &n, &m, &x);
    for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
    for (int i = 0; i < m; i ++ ) scanf("%d", &b[i]);
    for (int i = 0, j = m - 1; i < n; i ++ )
    {
        while (j > 0 && a[i] + b[j - 1] >= x) j -- ;
        if (a[i] + b[j] == x)
        {
            printf("%d %d\n", i, j);
            break;
        }
    }
    return 0;
}