QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#89538#5665. AA Country and King Dreamoonphtniit#RE 10ms35356kbC++113.1kb2023-03-20 14:51:382023-03-20 14:51:39

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-20 14:51:39]
  • 评测
  • 测评结果:RE
  • 用时:10ms
  • 内存:35356kb
  • [2023-03-20 14:51:38]
  • 提交

answer

#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast","-funroll-loops")
//#pragma GCC target("sse4.1","sse4.2","ssse3","sse3","sse2","sse","avx2","avx","popcnt","tune=native")

using namespace std;

typedef long double ldb;
typedef long long i64;
typedef unsigned long long u64;
typedef unsigned int u32;
typedef pair<int, int> pii;

// priority_queue<int, vector<int>, greater<int>> minq;
// ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// fflush(stdout);

const int inf = 1000000007;
const i64 prm = 998244353;
const i64 inf2 = ((i64)inf) * inf;
const i64 bone = 1;
const int maxn = 600010;

inline int read(){
  int x=0,f=0; char ch=getchar();
  while(!isdigit(ch)) f|=(ch==45),ch=getchar();
  while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
  return f?-x:x;
}

/*
   1. 请再三确认题意
   2. 对于复杂的数据结构+贪心/结论/计数题,可以考虑先写暴力,确认想法正确,再逐步优化
*/

set<int> g[maxn];
void once() {
  int n = read();
  static int a[maxn];
  for (int i = 1; i <= n; ++i) {
    g[i].clear();
  }
  for (int i = 1; i < n*2; ++i) {
    a[i] = read();
  }
  if (n == 1) {
    puts("1");
    return;
  }

  a[1] = a[n*2-1] = 1;
  int F = 0, L = 0;
  for (int i = 1; i < n*2; ++i) if (a[i] == 0) {
    L = i;
  }
  for (int i = n*2-1; i > 0; --i) if (a[i] == 0) {
    F = i;
  }
  for (int i = 2; i < F; ++i) {
    g[a[i-1]].insert(a[i]);
    g[a[i]].insert(a[i-1]);
  }
  for (int i = L+1; i+1 < n*2; ++i) {
    g[a[i]].insert(a[i+1]);
    g[a[i+1]].insert(a[i]);
  }

  vector<int> route;
  std::function<bool(int, int)> dfs = [&](int u, int fa) {
    route.push_back(u);
    if (u == a[L+1]) {
      return true;
    }
    for (auto v: g[u]) if (v != fa) {
      if (dfs(v, u)) {
        return true;
      }
    }
    route.pop_back();
    return false;
  };
  dfs(a[F-1], 0);

  static int vis[maxn];
  for (int i = 1; i <= n; ++i) vis[i] = 0;
  for (int i = 1; i < n*2; ++i) {
    vis[a[i]] = 1;
  }
  vector<int> vt;
  for (int i = 1; i <= n; ++i) if (!vis[i]) {
    vt.emplace_back(i);
  }

  vector<int> ans;
  if (vt.size() > 0) {
    for (int i = 0; i < route.size(); ++i) if (i + 1 == route.size() || route[i+1] > vt[0]) {
      for (int j = 0; j <= i; ++j) {
        ans.emplace_back(route[j]);
      }
      if (route[i] < vt[0]) {
        for (auto e: vt) {
          ans.emplace_back(e);
          ans.emplace_back(route[i]);
        }
      } else {
        ans.emplace_back(vt[0]);
        for (int j = 1; j < vt.size(); ++j) {
          ans.emplace_back(vt[j]);
          ans.emplace_back(vt[0]);
        }
        ans.emplace_back(route[i]);
      }
      for (int j = i+1; j < route.size(); ++j) {
        ans.emplace_back(route[j]);
      }
      break;
    }
  } else {
    ans = route;
  }
  assert(ans.size() == L+1 - (F-1) + 1);
  for (int k = 0, i = F-1; k < ans.size(); ++k, ++i) {
    a[i] = ans[k];
  }
  for (int i = 1; i < n*2; ++i) {
    printf("%d ", a[i]);
  }
  puts("");
}

int main() {
  int tes = 1;
  tes = read();
  while (tes--) {
    once();
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 10ms
memory: 35356kb

input:

9
5
1 2 3 2 0 2 1 5 1
5
1 2 3 0 0 2 1 5 1
5
1 2 0 0 0 2 1 5 1
5
1 2 0 0 0 0 1 5 1
5
1 0 0 0 0 0 1 5 1
5
1 0 0 0 0 0 0 5 1
5
1 0 0 0 0 0 0 0 1
5
1 0 0 0 0 0 0 0 0
5
0 0 0 0 0 0 0 0 0

output:

1 2 3 2 4 2 1 5 1 
1 2 3 2 4 2 1 5 1 
1 2 3 2 4 2 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 

result:

ok 9 lines

Test #2:

score: -100
Dangerous Syscalls

input:

28668
2
0 2 1
2
0 0 1
2
0 0 0
2
1 0 1
2
1 0 0
2
1 2 0
3
0 2 1 3 1
3
0 0 1 3 1
3
0 0 0 3 1
3
0 0 0 0 1
3
0 0 0 0 0
3
1 0 1 3 1
3
1 0 0 3 1
3
1 0 0 0 1
3
1 0 0 0 0
3
1 2 0 3 1
3
1 2 0 0 1
3
1 2 0 0 0
3
1 2 1 0 1
3
1 2 1 0 0
3
1 2 1 3 0
3
0 2 3 2 1
3
0 0 3 2 1
3
0 0 0 2 1
3
1 0 3 2 1
3
1 0 0 2 1
3
1 2 ...

output:


result: