Submission #1346504

#TimeUsernameProblemLanguageResultExecution timeMemory
1346504bakhtiyarnTable Tennis (info1cup20_tabletennis)C++20
24 / 100
3100 ms342256 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int N = 3e5+5;
int a[N];
int n, k; 
unordered_map<int, int> pos;

vector<int> build(int dif){
  vector<int> pre(n+k+5);
  for(int i=1; i<=n+k; i++){
    pre[i] = 1;
    if(pos[a[i]-dif]) pre[i] = pre[pos[a[i]-dif]] + 1;
  }
  
  vector<array<int, 2>> premx(n+k+5);
  for(int i=1; i<=n+k; i++){
    premx[i] = premx[i-1];
    if(premx[i][0] < pre[i]) premx[i] = {pre[i], i};
  }
  
  
  vector<int> suf(n+k+5);
  for(int i=n+k; i>=1; i--){
    suf[i] = 1;
    if(pos[a[i]+dif]) suf[i] = suf[pos[a[i]+dif]] + 1;
  }
  
  vector<array<int, 2>> sufmx(n+k+5);
  for(int i=n+k; i>=1; i--){
    sufmx[i] = sufmx[i+1];
    if(sufmx[i][0] < suf[i]) sufmx[i] = {suf[i], i};
  }
  
  int idx = -1;
  for(int i=1; i<n+k; i++){
    if(min(premx[i][0], sufmx[i+1][0])*2 < n) continue;
    idx = i;
  }
  if(idx == -1) return {};
  
  vector<int> ans;
  
  int i = premx[idx][1];
  while(i >= 1){
    ans.push_back(a[i]);
    if(pos[a[i]-dif]) i = pos[a[i]-dif];
    else break;
  }
  
  reverse(ans.begin(), ans.end());
  while(ans.size() > n/2) ans.pop_back();
  
  i = sufmx[idx+1][1];
  while(i <= n+k){
    ans.push_back(a[i]);
    if(pos[a[i]+dif]) i = pos[a[i]+dif];
    else break;
  }
  
  while(ans.size() > n) ans.pop_back();
  return ans;
}

// for(int i=1; i<=n; i++) 
void solve(){
  cin >> n >> k;
  for(int i=1; i<=n+k; i++) cin >> a[i];
  for(int i=1; i<=n+k; i++) pos[a[i]] = i;
  
  for(int l=1; l<=2+k; l++){
    for(int r=l+1; r<=2+k; r++){
      if(max(l, r) > n+k) break;
      int dif = a[r] - a[l];
      
      vector<int> ans = build(dif);
      if(ans.size() == n) {
        for(int i: ans) cout << i << " ";
        return;
      }
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...