foj 2260 Card Game 模擬 或區間最值 福州大學第十四屆程序設計競賽H題

題目鏈接:

http://acm.fzu.edu.cn/problem.php?pid=2260

題意:

直接看題目,說的很清楚,或者我複製一遍題面好了
有如下取牌遊戲:

  1. 桌面上有n張卡牌從左到右排成一行,每張卡牌上有一個數字;

  2. 遊戲按輪次進行,每一輪中取掉所有比左邊數值小的卡牌;

  3. 當無牌可取的時候則遊戲結束。

比如初始卡牌爲{5, 6, 3, 7, 4, 1, 2},共需2輪取牌。取牌過程如下(小括號爲每輪取掉的牌):

{5, 6, 3, 7, 4, 1, 2}

==> {5, 6, (3), 7, (4), (1), 2}

==> {5, 6, 7, 2}

==> {5, 6, 7, (2)}

==> {5, 6, 7}

現按順序給定初始的卡牌數字,請求出遊戲結束時取牌的總輪次,並輸出結束時桌上剩餘的卡牌序列。

模擬

現在感覺最正確的思路應該就是Claris老師(%%%)的模擬了,這應該比較好想把(隊友質問我爲什麼比賽的時候沒有想到,其實自己寫了一發暴力模擬,t了之後就拿它來對拍自己的其他思路了……因爲用的stl的鏈表,所以沒想手寫鏈表可以有很大的優化,真的太蠢了QAQ

模擬當然就是用鏈表裸的模擬了,把當前要刪除的節點放在隊列裏,刪的同時看被刪的節點的後一個節點下一輪會不會被刪,被刪的話就放進下一輪要刪除的隊列裏,每次循環的時候交換隊列就好了

複雜度O(n)

代碼:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define PB push_back

typedef long long LL;
typedef pair<int, int> P;
const int MAXN = 1e6 + 5;
const int INF = 0x3f3f3f3f;

struct Node {
  int pre, nxt, val;
};

int n;
int num[MAXN], tot;
int q[2][MAXN], la, lb;
int *qa, *qb;
bool used[MAXN], changed;
Node nodes[MAXN];

void Erase(int x) {
  if (used[x]) return ;
  used[x] = true;
  changed = true;
  int nxt = nodes[x].nxt;
  nodes[nodes[x].pre].nxt = nodes[x].nxt;
  nodes[nodes[x].nxt].pre = nodes[x].pre;
  if (nodes[nodes[nxt].pre].val > nodes[nxt].val) qb[lb++] = nxt;
}

int main() {
  while (~scanf("%d", &n)) {
    for (int i = 0; i < n; ++i) {
      scanf("%d", &nodes[i].val);
      used[i] = false;
      nodes[i].pre = i - 1;
      nodes[i].nxt = i + 1;
    }
    nodes[0].pre = 0;
    nodes[n - 1].nxt = n - 1;
    int mx = nodes[0].val;
    tot = 0;
    used[0] = true;
    qa = q[0];
    qb = q[1];
    la = lb = 0;
    for (int i = 1; i < n; ++i) {
      if (mx <= nodes[i].val) {
        num[tot++] = mx;
        mx = nodes[i].val;
        used[i] = true;
      }
      if (nodes[i - 1].val > nodes[i].val) {
        qa[la++] = i;
      }
    }
    num[tot++] = mx;
    changed = true;
    int ans = -1;
    while (changed) {
      ++ans;
      changed = false;
      lb = 0;
      for (int i = 0; i < la; ++i) Erase(qa[i]);
      swap(qa, qb);
      swap(la, lb);
    }
    printf("%d\n%d", ans, num[0]);
    for (int i = 1; i < tot; ++i) printf(" %d", num[i]);
    putchar(10);
  }
}

區間最值

不難看出,如果一個數要消失,那它一定是撞到前一個比它大的數才消失,如果它與前一個比它大的數之間有其它數,那麼這些數消失之前,它都不會消失。

用數組f[]標記每個元素什麼時候消失,如果一個元素的前一個元素就是那個比它大的數,那它毫無疑問第一次操作的時候就會消失。如果一個元素和比它大的那個元素之間相隔了一些元素,那麼它消失的時間就是這些元素消失時間最大值加1 。所以接下來的問題就是如何處理這些區間最值。

= =,很扯的是,自己測了很多次,每次直接往前走到那個比它大的元素順便記錄區間最值的方法是最快的,複雜度O(n2) ,只要900多ms就能過,可能是數據的問題把。O(nlogn) 的算法中,一般的線段樹會T,zkw線段樹則1400ms就能過,樹狀數組的話,由於題目的特殊性,採用O(nlogn)update()方法就能過,O(nlog2n)update()則得卡一卡。

如果有人可以告訴我爲什麼O(n2) 的算法可以這麼快,不勝感激。

代碼:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

#define PB push_back
#define lowbit(x) (x & (-x))
#define MS(x, y) memset(x, y, sizeof(x))

template<class T1, class T2> inline void gmax(T1& a, T2 b) { if (a < b) a = b; }

typedef long long LL;
typedef pair<int, int> P;
const int MAXN = 1e6 + 5;
const LL INF = 100000000000000000LL;

int n;
int a[MAXN], f[MAXN], pre[MAXN];
P stk[MAXN];
int tail;
int num[MAXN], tot;
int segTree[2100000], BASE;
// 採用了zkw線段樹
void update(int x) {
  x += BASE;
  segTree[x] = f[x - BASE];
  for (x >>= 1; x; x >>= 1) segTree[x] = max(segTree[x << 1], segTree[x << 1 | 1]);
}

int query(int l, int r) {
  int ret = 0;
  l += BASE - 1; r += BASE + 1;
  for (; l ^ r ^ 1; l >>= 1, r >>= 1) {
    if (~ l & 1) gmax(ret, segTree[l ^ 1]);
    if (r & 1) gmax(ret, segTree[r ^ 1]);
  }
  return ret;
}

int main() {
  while (~scanf("%d", &n)) {
    for (BASE = 1; BASE <= n + 1; BASE <<= 1);
    for (int i = 0; i <= BASE << 1; ++i) segTree[i] = 0;
    int ans = 0, mx = 0, tot = 0, x;
    tail = 0;
    for (int i = 1; i <= n; ++i) scanf("%d", a + i);
    // 用單調棧記錄每個數前面比它大的數的位置
    stk[tail++] = P(a[1], 1);
    for (int i = 2; i <= n; ++i) {
      while (tail > 0 && stk[tail - 1].first <= a[i]) --tail;
      pre[i] = stk[tail - 1].second;
      stk[tail++] = P(a[i], i);
    }
    for (int i = 1; i <= n; ++i) {
      if (a[i] >= mx) {
        num[tot++] = mx;
        mx = a[i];
        f[i] = 0;
      } else {
        if (a[i - 1] > a[i]) {
          f[i] = 1;
        } else {
          f[i] = query(pre[i] + 1, i - 1) + 1;
        }
      }
      gmax(ans, f[i]);
      update(i);
    }
    num[tot++] = mx;
    printf("%d\n%d", ans, num[1]);
    for (int i = 2; i < tot; ++i) printf(" %d", num[i]);
    putchar(10);
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章