3 条题解

  • 1
    @ 2023-11-29 18:21:54
    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <cmath>
    #include <cstdio>
    // #define ll long long
    #define Rep(x, a, b) for (int x = a; i <= b; i++)
    #define Dep(x, a, b) for (int x = a; i >= b; i--)
    using namespace std;
    int n, p, x, y, s, sum[10], a[10][300005];
    int main()
    {
        // freopen("GITARA.in", "r", stdin);
        // freopen("GITARA.out", "w", stdout);
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin >> n >> p;
        Rep(i, 1, n)
        {
            cin >> x >> y;
            while (sum[x] >= 1 && a[x][sum[x]] > y)
            {
                sum[x]--;
                s++;
            }
            /*
            for (int i = 1; i <= 7; i++)
                cout << sum[i] << " ";
            cout << endl;
            */
            // cout << a[x][sum[x]] << endl;
            if (a[x][sum[x]] == y)
                continue;
            a[x][++sum[x]] = y;
            // cout << a[x][sum[x]] << endl;
            s++;
        }
        cout << s << endl;
        return 0;
    }
    

    以上是为经简化的AC初稿

    下面是简化过的:

    #include <bits/stdc++.h>
    #define Rep(x, a, b) for (int x = a; i <= b; i++)
    #define Dep(x, a, b) for (int x = a; i >= b; i--)
    using namespace std;
    int n, p, x, y, s, sum[10], a[10][300005];
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        cin >> n >> p;
        Rep(i, 1, n)
        {
            cin >> x >> y;
            while (sum[x] >= 1 && a[x][sum[x]] > y)
            {
                sum[x]--;
                s++;
            }
            if (a[x][sum[x]] == y)
                continue;
            a[x][++sum[x]] = y;
            s++;
        }
        cout << s << endl;
        return 0;
    }
    
    • @ 2023-11-29 18:23:40

      说实话,可以不用栈的。。。

  • 1
    @ 2023-11-2 10:18:30

    这道题目也可以利用栈的思想,我们可以发现不同的弦不会产生冲突,而吉他总共有6根弦,所以我们共需要开6个栈:int sta[7][N];

    每次输入弦号i和段号j后,需要去对应的栈中把栈顶元素大于当前段号j的元素出栈,因为可能会出栈多次,所以要用一个while循环,每一次循环弹出栈顶,其条件为栈不为空和栈顶值大于段号j:

    while (top[i] && sta[i][top[i]]>j) { –-top[i]; ++ans; }

    接着判断栈顶元素和当前段号j是否相等,若不存在栈顶元素或者不相等,则需要将当前段号j入栈,操作值++:

    if (top[i]==0 || sta[i][top[i]]!=j) { sta[i][++top[i]]=j; ans++; }

    #include<bits/stdc++.h>
    using namespace std;
    const int N=3e5+5;
    int n,m,k,s,t,i,j,ans,P,top[7],sta[7][N];
    int main(){
    	cin>>n>>P;
    	while(n--){
    		cin>>i>>j;
    		while (top[i] && sta[i][top[i]]>j) --top[i],++ans;
    		if (top[i]==0 || sta[i][top[i]]!=j) {
        		sta[i][++top[i]]=j;
        		ans++;
    		}
    	}
    	cout<<ans; 
    }
    
    
    • @ 2023-11-29 18:25:35

      我想问一下,为什么用数组模拟栈时间复杂度这么高,请问还能怎么优化啊?相比于我的代码,好像慢了许多欸——

  • 0
    @ 2024-4-8 19:55:30

    题解中有用手搓栈做的,我就提供一个用STL栈做的吧。思路可见其他题解。

    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    ll n,p,ans;
    stack<ll> sta[7];
    int main(){
        scanf("%lld%lld",&n,&p);
        for(ll k=1,i,j;k<=n;k++){
            scanf("%lld%lld",&i,&j);
            while(!sta[i].empty()&&sta[i].top()>j)
                sta[i].pop(),ans++;
            if(sta[i].empty()||sta[i].top()!=j)
                sta[i].push(j),ans++;
        }
        printf("%lld",ans);
        return 0;
    }
    
    • 1

    信息

    ID
    537
    时间
    1000ms
    内存
    32MiB
    难度
    3
    标签
    递交数
    129
    已通过
    65
    上传者