QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#181350#6352. SPPPSPSS.ucup-team859#WA 0ms3816kbC++143.4kb2023-09-16 17:59:082023-09-16 17:59:09

Judging History

你现在查看的是最新测评结果

  • [2023-09-16 17:59:09]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3816kb
  • [2023-09-16 17:59:08]
  • 提交

answer

#include <bits/stdc++.h>

#define lsb(x) (x & (-x))

using ull = unsigned long long;
using ll = long long;

using namespace std;

string special_case(int n, vector<int> &a, char ch) {
  string s;
  vector<int> sl(n + 1, 0), sr(n + 1, 0);

  int l = 0, r = n + 1;
  int pl = 0, pr = n + 1;

  int last = 0;
  for (int i = 1; i <= n; ++i) {
    s.push_back(ch);
    if (ch == 'S') {
      ch = 'P';
      int rem = 2 - (i == 1);
      while (r - 1 >= n - i + 1 && l < r - 1) {
        --r;
        --rem;
        sr[a[r]] = 1;
      }

      if (last == 0 && rem > 0)
        last = rem;
      else if (last > 0)
        rem = last + 2;

      while (rem > 0 && pr > 0) {
        if (sl[pr - 1] == 1)
          --rem;
        --pr;
      }

      if (l >= pr)
        break;
    } else {
      ch = 'S';

      int rem = 2 - (i == 1);
      while (l + 1 <= i && l + 1 < r) {
        ++l;
        --rem;
        sl[a[l]] = 1;
      }

      if (last == 0 && rem > 0)
        last = rem;
      else if (last > 0)
        rem = last + 2;

      while (rem > 0 && pl <= n) {
        if (sr[pl + 1] == 1)
          --rem;
        ++pl;
      }

      if (pl >= r)
        break;
    }
  }

  return s;
}

void solve() {
  int n;
  cin >> n;

  vector<int> a(n + 1, 0), dp(n + 1, 0), p(n + 1, 0);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    p[a[i]] = i;
  }

  bool ok = true;
  for (int i = 1; i < n && ok; ++i) {
    if (a[i] > a[i + 1])
      ok = false;
  }

  if (ok) {
    cout << ".";
    return;
  }

  dp[n] = 1;
  for (int i = n - 1; i >= 1; --i) {
    if (a[i] == a[i + 1] - 1)
      dp[i] = dp[i + 1] + 1;
    else
      dp[i] = 1;
  }

  int mn = n + 1, mx = 1;
  // case 1 -> pref x x + 1 ... x + k suf
  int ans = n, ls = n, lp = 0;

  for (int i = 1; i < n; ++i) {
    mx = max(mx, a[i]);
    mn = min(mn, a[i]);

    if (mx - mn + 1 == i && mn == 1) {
      if (a[i + 1] == i + 1) {
        int dr = i + dp[i + 1];
        int x = max(i, n - dr);
        if (i == n - dr)
          ++x;
        if (x < ans) {
          ans = x;
          lp = i;
          ls = n - dr;
          if (lp == ls)
            ++lp;
        }
      }
    }
  }

  // case 2 -> pref suf 
  int last = 0;
  for (int i = 1; i < n; ++i) {
    while (p[last + 1] <= i)
      ++last;

    int x = max(i, n - last);
    if (i == n - last)
      ++x;

    if (x < ans) {
      ans = x;
      lp = i;
      ls = n - last;
      if (lp == ls)
        ++lp;
    }
  }

  // case 3 -> suf pref
  last = n + 1;
  for (int i = n; i > 1; --i) {
    while (p[last - 1] >= i)
      --last;

    int x = max(n - i + 1, last - 1);

    if (last - 1 == n - i + 1)
      ++x;

    if (x < ans) {
      ans = x;
      lp = last - 1;
      ls = n - i + 1;
      if (lp == ls)
        ++lp;
    }
  }

  auto s1 = special_case(n, a, 'P');
  auto s2 = special_case(n, a, 'S');

  if (s1.size() > s2.size())
    s1 = s2;

  if (s1.size() < ans) {
    s1.push_back('.');
    cout << s1 << endl;
    return;
  }

  for (int i = 1; i <= ans; ++i) {
    if (i == lp)
      cout << 'P';
    else
      cout << 'S';
  }

  cout << ".";

}

int main() {
#ifdef HOME
  ifstream cin("input.in");
  ofstream cout("output.out");
#endif
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  
  int t = 1;
  //cin >> t;
  while (t--)
    solve();

  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3492kb

input:

3
1 2 3

output:

.

result:

ok OK 0 operations

Test #2:

score: 0
Accepted
time: 0ms
memory: 3628kb

input:

2
2 1

output:

SS.

result:

ok OK 2 operations

Test #3:

score: 0
Accepted
time: 0ms
memory: 3816kb

input:

9
3 2 4 1 5 6 7 9 8

output:

SSSP.

result:

ok OK 4 operations

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 3532kb

input:

10
2 9 5 7 10 6 3 1 8 4

output:

SSSSSSSP.

result:

wrong answer the permutation is not sorted