--- 这里是 cjiaw 的小窝(●'◡'●) ---

正在玩命加载中......

AT_abc438_C - 1D puyopuyo

题目链接:C - 1D puyopuyo


题目大意:

给一个长度为n的数组,每次删除连续的4个相同的数字,

求:若干次删除后数组最小的长度


思路:

用栈维护整个数组

当存在有连续且相同的四个数字时,将这四个数字出栈即可


代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<bitset>
#include<tuple>
#include<array>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/numeric>
#define inf 72340172838076673
#define int long long
#define endl '\n'
#define F first
#define S second
#define mst(a,x) memset(a,x,sizeof (a))
#define gmap __gnu_pbds::gp_hash_table
#define power __gnu_cxx::power
using namespace std;
typedef pair<int, int> pii;

const int N = 200086, mod = 998244353;

int n, m;
int a[N];
int st[N];

void solve() {


    cin >> n;
    int top = 0;
    int res = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        st[++top] = a[i];
        if (top >= 4) {
            if (st[top] == st[top - 1] && st[top - 1] == st[top - 2] && st[top - 2] == st[top - 3]) {
                res += 4;
                top -= 4;
            }
        }
    }
    
    cout << n - res << endl;

}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    
    int T = 1;
// cin >> T;
    while (T--) solve();
    
    return 0;
}

 

posted @ 2026-01-01 17:04  wwjjw  阅读(12)  评论(0)    收藏  举报