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);
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值