link

current: 10 / 13

????A. Maximum Element In A Stack

????B. Rolling The Polygon

????C. Caesar Cipher

????D. Take Your Seat

E. 2-3-4 Tree

模拟题

// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize(4)
#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>
#include <unordered_set>
// #include<bits/extc++.h>
// using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define fi first
#define se second
#define debug(x) cerr<<#x << " := " << x << endl;
#define bug cerr<<"-----------------------"<<endl;
#define FOR(a, b, c) for(int a = b; a <= c; ++ a)

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;

template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}

/**********showtime************/
            const int maxn = 5009;
            int tot;
            struct node{
                int cnt;
                int val[3];
                int son[4];
            } tree[maxn *2];

            int newnode() {
                ++tot;
                tree[tot].cnt = 0;
                for(int i=0; i<3; i++) tree[tot].val[i] = 0;
                for(int i=0; i<4; i++) tree[tot].son[i] = 0;
                return tot;
            }

            void insert(int p,  int val) {

                int id = tree[p].cnt;

                for(int i = 0; i < tree[p].cnt; i++) {
                    if(val < tree[p].val[i]) {
                        id = i;
                        break;
                    }
                }
                int flag = 1;
                for(int i=0; i <= tree[p].cnt; i++) if(tree[p].son[i]) flag = 0;
                if(flag) {
                    int cur = tree[p].cnt;
                    while(cur > id) tree[p].val[cur] = tree[p].val[cur-1], cur--;
                    tree[p].val[id] = val;
                    tree[p].cnt++;
                    return;
                }
                if(tree[p].son[id] == 0) tree[p].son[id] = newnode();
                int np =  tree[p].son[id];
                if(tree[np].cnt == 3) {
                    int cur = tree[p].cnt;
                    while(cur > id) tree[p].val[cur] = tree[p].val[cur-1], cur--;
                    tree[p].val[id] = tree[np].val[1];

                    tree[p].cnt++;

                    int lp = newnode();
                    tree[np].cnt = 1;
                    tree[lp].cnt = 1;
                    tree[lp].val[0] = tree[np].val[2];
                    tree[lp].son[0] = tree[np].son[2];
                    tree[lp].son[1] = tree[np].son[3];

                    cur = tree[p].cnt;
                    while(cur > id + 1) tree[p].son[cur] = tree[p].son[cur-1], cur--;
                    tree[p].son[id+1] = lp;
                    tree[p].son[id] = np;
                    if(val > tree[np].val[1]) np = lp;
                }
                insert(np,val);
            }


            void display(int p) {
                if(tree[p].cnt == 0) return;
                for(int i=0; i<tree[p].cnt; i++) {
                    printf("%d ", tree[p].val[i]);
                }
                puts("");
                for(int i=0; i<=tree[p].cnt; i++) {
                    display(tree[p].son[i]);
                }
            }

int main(){
            int T;  scanf("%d", &T);
            int cas = 0;
            while(T--) {
                tot = 0;
                int rt = newnode();
                int n;
                scanf("%d", &n);
                for(int i=1; i<=n; i++) {
                    int x;  scanf("%d", &x);
                    if(tree[rt].cnt == 3) {
                        int newle = rt;
                        rt = newnode();
                        int newri = newnode();
                        tree[rt].cnt = 1; tree[rt].val[0] = tree[newle].val[1]; tree[rt].son[0] = newle; tree[rt].son[1] = newri;
                        tree[newri].cnt = 1; tree[newri].val[0] = tree[newle].val[2];
                        tree[newri].son[0] = tree[newle].son[2]; tree[newri].son[1] = tree[newle].son[3];
                        tree[newle].cnt =1;
                    }
                    insert(rt,  x);
                }
                printf("Case #%d:\n", ++cas);
                    display(rt);
            }

            return 0;
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-02
相关资源
相似解决方案