10 条题解

  • -2
    @ 2024-10-5 16:15:00

    艹 写了30min 改了90min 还不如重写一遍

    艹 代码:

    #include<iostream> 
    #include<bits/stdc++.h>
    //#include<windows.h>
    using namespace std;
    
    const int N = 1e6 + 1010;
    typedef char ch;
    typedef string str;
    typedef bool bo;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    #define endl "\n";
    
    ll n, a[N];
    set<ll> st;
    ll ans = 1;
    ll d = 0, td = 0;
    ll sz = 1;
    
    ll gcdd( ll a, ll b )
    {
        if ( b == 0 )
        {
            return a;
        }
        return gcdd(b, a % b);
    }
    
    int main()
    {
        ios::sync_with_stdio(0), cout.tie(0), cin.tie(0);
        // freopen( "factory.in", "r", stdin );
        // freopen( "factory.out", "w", stdout );
        cin >> n;
        // td = gcd( d, a[i] - a[i - 1] ); != 1     okabs( a[i] - a[i - 1] ) != 1 != 0ok      st.find(a[i]) == true 不能加重复的   d 上一个公差
        
        for ( ll i = 1; i <= n ; i ++ )
        {
            cin >> a[i];
        }
        sz = 1;
        ans = 1;
        st.insert(a[1]);
        for ( ll i = 2; i <= n; i ++ )
        {
    
            if ( abs( a[i] - a[i - 1] ) == 1 || abs( a[i] - a[i - 1] ) == 0 ) 
            {
                ans ++;
                //cout << "!  " << a[i] << endl;
                sz = 1;
                st.clear();
                st.insert(a[i]);
                continue;
            }
            if ( sz == 1 )
            {
                sz ++;
                st.insert(a[i]);
                d = abs( a[i] - a[i - 1] );
                continue;
            }
            td = gcdd( d, abs( a[i] - a[i - 1] ) );
            //cout << i << " " << td << endl;
            if ( td != 1 && st.find(a[i]) == st.end() )
            {
                sz ++;
                d = td;
                st.insert(a[i]);
                continue;
            }
            else
            {
                sz = 1;
                ans ++;
                //cout << "!!  " << i << endl;
                st.clear();
                st.insert(a[i]);
                continue;
            }
        }
        cout << ans << endl;
    	return 0;
    }
    

    信息

    ID
    783
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    (无)
    递交数
    83
    已通过
    11
    上传者