QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#89535#5667. Meeting Placesphtniit#TL 0ms0kbC++113.3kb2023-03-20 14:36:152023-03-20 14:36:19

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:36:19]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2023-03-20 14:36:15]
  • 提交

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 = 500010;

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. 对于复杂的数据结构+贪心/结论/计数题,可以考虑先写暴力,确认想法正确,再逐步优化
*/

vector<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]].push_back(a[i]);
    g[a[i]].push_back(a[i-1]);
  }
  for (int i = L+1; i+1 < n*2; ++i) {
    g[a[i]].push_back(a[i+1]);
    g[a[i+1]].push_back(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;
  };
  if (a[F-1] == a[L+1]) {
    route.emplace_back(a[F-1]);
    //route.emplace_back(a[L+1]);
  } else {
    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] || route[0] == route[1]) {
      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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

100 23 213

output:


result: