답안 #841085

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
841085 2023-09-01T08:51:52 Z MrBrionix 가장 긴 여행 (IOI23_longesttrip) C++17
70 / 100
33 ms 460 KB
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;

bool are_connected(std::vector<int> A, std::vector<int> B);

void add(vector<int> &a,vector<int> &b){
    reverse(a.begin(),a.end());
    for(auto i : b)a.push_back(i);
}

bool query(vector<int> &a,vector<int> &b,int l1,int r1,int l2,int r2){
    vector<int> tmp1,tmp2;
    for(int i=l1;i<=r1;i++)tmp1.push_back(a[i]);
    for(int i=l2;i<=r2;i++)tmp2.push_back(b[i]);
    
    return are_connected(tmp1,tmp2);
}

bool comp(vector<int> &a,vector<int> &b){
    return a.size()<b.size();
}

mt19937 rng((uint64_t) chrono::duration_cast<chrono::
    nanoseconds>(chrono::high_resolution_clock::now()
        .time_since_epoch()).count());

vector<int> longest_trip(int N, int D)
{
    srand(time(NULL));
    deque<vector<int>> q;
    
    
    for(int i=0;i<N;i++){
        q.push_back({i});
    }
    shuffle(q.begin(),q.end(),rng);
    
    int cont=0;
    
    while(q.size()>3){
        
        //shuffle(q.begin(),q.end(),rng);
        sort(q.begin(),q.end(),comp);
        shuffle(q.begin(),q.begin()+4,rng);
        auto a = q.front();
        q.pop_front();
        auto b = q.front();
        q.pop_front();
        auto c = q.front();
        q.pop_front();
        auto d = q.front();
        q.pop_front();
        
        
        if(!are_connected({*a.begin(),*b.begin()},{*c.begin(),*d.begin()})){
            add(a,b);
            add(c,d);
            q.push_back(a);
            q.push_back(c);
            continue;
        }
        
        if(!are_connected({*a.begin()},{*c.begin(),*d.begin()})){
            add(c,d);
            q.push_back(a);
            q.push_back(b);
            q.push_back(c);
            continue;
        }
        
        if(are_connected({*a.begin()},{*c.begin()})){
            add(a,c);
            q.push_back(a);
            q.push_back(b);
            q.push_back(d);
            continue;
        }
        
        add(a,d);
        q.push_back(a);
        q.push_back(b);
        q.push_back(c);
    }
    
    while(q.size()>2){
        
        //shuffle(q.begin(),q.end(),rng);
        sort(q.begin(),q.end(),comp);
        shuffle(q.begin(),q.begin()+3,rng);
        auto a = q.front();
        q.pop_front();
        auto b = q.front();
        q.pop_front();
        auto c = q.front();
        q.pop_front();
        
        //if(rng()%2)reverse(a.begin(),a.end());
        //if(rng()%2)reverse(b.begin(),b.end());
        //if(rng()%2)reverse(c.begin(),c.end());
        
        if(are_connected({*a.begin()},{*b.begin()})){
            cont++;
        }else if(are_connected({*a.begin()},{*c.begin()})){
            swap(b,c);
            cont+=2;
        }else{
            cont+=2;
            swap(a,c);
        }
        
        add(a,b);
        q.push_back(a);
        q.push_front(c);
    }
    
    
    //assert(cont<410);
    auto a = q.front();
    q.pop_front();
    auto b = q.front();
    q.pop_front();
    
    if(!are_connected(a,b)){
        if(a.size()>b.size())return a;
        else return b;
    }else{
        int l1 = *a.begin(), r1 = a.back();
        int l2 = *b.begin(), r2 = b.back();
        
        if(are_connected({l1},{l2})){
            add(a,b);
            return a;
        }
        if(are_connected({l1},{r2})){
            reverse(b.begin(),b.end());
            add(a,b);
            return a;
        }
        if(are_connected({l2},{r1})){
            reverse(a.begin(),a.end());
            add(a,b);
            return a;
        }
        
        
        int low = -1, up = a.size()-1;
        
        while(up-low>1){
            int mid = (up+low)/2;
            
            if(query(a,b,0,mid,0,b.size()-1)){
                up=mid;
            }else{
                low=mid;
            }
        }
        
        int ind = up;
        
        low = -1, up = b.size()-1;
        while(up-low>1){
            int mid = (up+low)/2;
            
            if(query(a,b,ind,ind,0,mid)){
                up=mid;
            }else{
                low=mid;
            }
        }
        
        
        for(int i=0;i<ind;i++){
            a.push_back(*a.begin());
            a.erase(a.begin());
        }
        
        for(int i=0;i<up;i++){
            b.push_back(*b.begin());
            b.erase(b.begin());
        }
        
        add(a,b);
        return a;
    }
}

/*
  1
  5 1
  1
  1 1
  0 0 1
  0 0 0 1
  
  static inline constexpr int maxNumberOfCalls = 32640;
  static inline constexpr int maxTotalNumberOfCalls = 150000;
  static inline constexpr int maxTotalNumberOfLandmarksInCalls = 1500000;
  static int call_counter = 0;
  static int total_call_counter = 0;
  static int landmark_counter = 0;
  
  static int C, N, D;
  static std::vector<std::vector<int>> U;
  static std::vector<bool> present;
  
  static inline void protocol_violation(std::string message)
  {
  printf("Protocol Violation: %s\n", message.c_str());
  exit(0);
  }
  
  bool are_connected(std::vector<int> A, std::vector<int> B)
  {
  ++call_counter;
  ++total_call_counter;
  if (call_counter > maxNumberOfCalls || total_call_counter > maxTotalNumberOfCalls)
  {
  protocol_violation("too many calls");
  }
  
  int nA = A.size(), nB = B.size();
  landmark_counter += nA + nB;
  if (landmark_counter > maxTotalNumberOfLandmarksInCalls)
  {
  protocol_violation("too many elements");
  }
  
  if (nA == 0 || nB == 0)
  {
  protocol_violation("invalid array");
  }
  for (int i = 0; i < nA; ++i)
  {
  if (A[i] < 0 || N <= A[i])
  {
  protocol_violation("invalid array");
  }
  if (present[A[i]])
  {
  protocol_violation("invalid array");
  }
  present[A[i]] = true;
  }
  for (int i = 0; i < nA; ++i)
  {
  present[A[i]] = false;
  }
  for (int i = 0; i < nB; ++i)
  {
  if (B[i] < 0 || N <= B[i])
  {
  protocol_violation("invalid array");
  }
  if (present[B[i]])
  {
  protocol_violation("invalid array");
  }
  present[B[i]] = true;
  }
  for (int i = 0; i < nB; ++i)
  {
  present[B[i]] = false;
  }
  
  for (int i = 0; i < nA; ++i)
  {
  for (int j = 0; j < nB; ++j)
  {
  if (A[i] == B[j])
  {
  protocol_violation("non-disjoint arrays");
  }
  }
  }
  
  for (int i = 0; i < nA; ++i)
  {
  for (int j = 0; j < nB; ++j)
  {
  if (U[std::max(A[i], B[j])][std::min(A[i], B[j])] == 1)
  {
  return true;
  }
  }
  }
  
  return false;
  }
  
  
  int main()
  {
  assert(1 == scanf("%d", &C));
  int maximumCalls = 0;
  for (int c = 0; c < C; ++c)
  {
  call_counter = 0;
  assert(2 == scanf("%d %d", &N, &D));
  
  present.assign(N, false);
  U.resize(N);
  for (int i = 1; i < N; ++i)
  {
  U[i].resize(i);
  for (int j = 0; j < i; ++j)
  {
  assert(1 == scanf("%d", &U[i][j]));
  }
  }
  
  for (int i = 2; i < N; ++i)
  {
  for (int j = 1; j < i; ++j)
  {
  for (int k = 0; k < j; ++k)
  {
  if (U[i][j] + U[i][k] + U[j][k] < D)
  {
  printf("Insufficient Density\n");
  exit(0);
  }
  }
  }
  }
  
  std::vector<int> t = longest_trip(N, D);
  int l = t.size();
  printf("%d\n", l);
  for (int i = 0; i < l; ++i)
  {
  printf(i == 0 ? "%d" : " %d", t[i]);
  }
  printf("\n");
  printf("%d chiamate\n", call_counter);
  
  maximumCalls = std::max(maximumCalls, call_counter);
  call_counter = 0;
  }
  printf("%d max chiamate\n", maximumCalls);
  
  return 0;
  }
 */
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 7 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 208 KB Output is correct
2 Correct 24 ms 208 KB Output is correct
3 Correct 28 ms 308 KB Output is correct
4 Correct 14 ms 332 KB Output is correct
5 Correct 33 ms 208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 208 KB Output is correct
2 Correct 18 ms 208 KB Output is correct
3 Correct 30 ms 332 KB Output is correct
4 Correct 21 ms 324 KB Output is correct
5 Correct 33 ms 208 KB Output is correct
6 Correct 12 ms 208 KB Output is correct
7 Correct 18 ms 208 KB Output is correct
8 Correct 16 ms 208 KB Output is correct
9 Correct 26 ms 208 KB Output is correct
10 Correct 24 ms 320 KB Output is correct
11 Correct 26 ms 332 KB Output is correct
12 Correct 21 ms 460 KB Output is correct
13 Correct 29 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 208 KB Output is correct
2 Correct 12 ms 208 KB Output is correct
3 Correct 26 ms 300 KB Output is correct
4 Correct 22 ms 312 KB Output is correct
5 Correct 24 ms 336 KB Output is correct
6 Correct 14 ms 208 KB Output is correct
7 Correct 19 ms 208 KB Output is correct
8 Correct 22 ms 208 KB Output is correct
9 Correct 22 ms 336 KB Output is correct
10 Correct 27 ms 316 KB Output is correct
11 Correct 26 ms 320 KB Output is correct
12 Correct 17 ms 328 KB Output is correct
13 Correct 32 ms 316 KB Output is correct
14 Correct 12 ms 208 KB Output is correct
15 Correct 12 ms 208 KB Output is correct
16 Correct 26 ms 208 KB Output is correct
17 Correct 21 ms 208 KB Output is correct
18 Correct 20 ms 328 KB Output is correct
19 Correct 25 ms 332 KB Output is correct
20 Correct 29 ms 308 KB Output is correct
21 Correct 32 ms 332 KB Output is correct
22 Correct 27 ms 316 KB Output is correct
23 Correct 30 ms 336 KB Output is correct
24 Correct 32 ms 328 KB Output is correct
25 Correct 16 ms 208 KB Output is correct
26 Correct 16 ms 208 KB Output is correct
27 Correct 16 ms 208 KB Output is correct
28 Correct 16 ms 208 KB Output is correct
29 Correct 21 ms 208 KB Output is correct
30 Correct 21 ms 208 KB Output is correct
31 Correct 17 ms 332 KB Output is correct
32 Correct 23 ms 208 KB Output is correct
33 Correct 20 ms 316 KB Output is correct
34 Correct 22 ms 208 KB Output is correct
35 Correct 26 ms 328 KB Output is correct
36 Correct 24 ms 320 KB Output is correct
37 Correct 22 ms 316 KB Output is correct
38 Correct 24 ms 320 KB Output is correct
39 Correct 26 ms 316 KB Output is correct
40 Correct 21 ms 320 KB Output is correct
41 Correct 21 ms 324 KB Output is correct
42 Correct 24 ms 316 KB Output is correct
43 Correct 19 ms 208 KB Output is correct
44 Correct 20 ms 328 KB Output is correct
45 Correct 17 ms 208 KB Output is correct
46 Correct 22 ms 208 KB Output is correct
47 Correct 20 ms 208 KB Output is correct
48 Correct 20 ms 208 KB Output is correct
49 Correct 26 ms 208 KB Output is correct
50 Correct 20 ms 208 KB Output is correct
51 Correct 23 ms 360 KB Output is correct
52 Correct 25 ms 208 KB Output is correct
53 Correct 22 ms 324 KB Output is correct
54 Correct 20 ms 208 KB Output is correct
55 Correct 20 ms 208 KB Output is correct
56 Correct 30 ms 336 KB Output is correct
57 Correct 29 ms 336 KB Output is correct
58 Correct 32 ms 320 KB Output is correct
59 Correct 24 ms 308 KB Output is correct
60 Correct 19 ms 336 KB Output is correct
61 Correct 19 ms 208 KB Output is correct
62 Correct 24 ms 332 KB Output is correct
63 Correct 27 ms 332 KB Output is correct
64 Correct 24 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 208 KB Output is correct
2 Correct 16 ms 208 KB Output is correct
3 Correct 28 ms 208 KB Output is correct
4 Correct 31 ms 260 KB Output is correct
5 Partially correct 26 ms 320 KB Output is partially correct
6 Correct 13 ms 208 KB Output is correct
7 Correct 19 ms 208 KB Output is correct
8 Correct 21 ms 208 KB Output is correct
9 Correct 29 ms 296 KB Output is correct
10 Partially correct 32 ms 208 KB Output is partially correct
11 Partially correct 25 ms 316 KB Output is partially correct
12 Partially correct 20 ms 344 KB Output is partially correct
13 Partially correct 30 ms 324 KB Output is partially correct
14 Correct 12 ms 208 KB Output is correct
15 Correct 12 ms 208 KB Output is correct
16 Correct 19 ms 208 KB Output is correct
17 Correct 26 ms 208 KB Output is correct
18 Correct 26 ms 312 KB Output is correct
19 Correct 20 ms 208 KB Output is correct
20 Correct 31 ms 308 KB Output is correct
21 Correct 16 ms 208 KB Output is correct
22 Correct 9 ms 256 KB Output is correct
23 Correct 17 ms 208 KB Output is correct
24 Correct 17 ms 208 KB Output is correct
25 Correct 19 ms 208 KB Output is correct
26 Correct 19 ms 208 KB Output is correct
27 Correct 17 ms 208 KB Output is correct
28 Correct 19 ms 208 KB Output is correct
29 Correct 16 ms 320 KB Output is correct
30 Correct 22 ms 208 KB Output is correct
31 Correct 27 ms 312 KB Output is correct
32 Correct 21 ms 208 KB Output is correct
33 Correct 17 ms 332 KB Output is correct
34 Correct 19 ms 208 KB Output is correct
35 Correct 19 ms 208 KB Output is correct
36 Correct 17 ms 208 KB Output is correct
37 Correct 21 ms 332 KB Output is correct
38 Correct 19 ms 208 KB Output is correct
39 Correct 18 ms 208 KB Output is correct
40 Correct 24 ms 228 KB Output is correct
41 Correct 21 ms 308 KB Output is correct
42 Correct 29 ms 300 KB Output is correct
43 Partially correct 24 ms 316 KB Output is partially correct
44 Partially correct 32 ms 208 KB Output is partially correct
45 Partially correct 20 ms 328 KB Output is partially correct
46 Partially correct 26 ms 336 KB Output is partially correct
47 Partially correct 29 ms 436 KB Output is partially correct
48 Partially correct 23 ms 320 KB Output is partially correct
49 Partially correct 29 ms 208 KB Output is partially correct
50 Partially correct 26 ms 208 KB Output is partially correct
51 Partially correct 24 ms 312 KB Output is partially correct
52 Partially correct 27 ms 344 KB Output is partially correct
53 Partially correct 19 ms 208 KB Output is partially correct
54 Partially correct 20 ms 208 KB Output is partially correct
55 Partially correct 24 ms 208 KB Output is partially correct
56 Partially correct 28 ms 328 KB Output is partially correct
57 Partially correct 21 ms 316 KB Output is partially correct
58 Partially correct 26 ms 316 KB Output is partially correct
59 Partially correct 26 ms 320 KB Output is partially correct
60 Partially correct 24 ms 208 KB Output is partially correct
61 Partially correct 24 ms 208 KB Output is partially correct
62 Partially correct 24 ms 328 KB Output is partially correct
63 Partially correct 21 ms 332 KB Output is partially correct
64 Partially correct 30 ms 336 KB Output is partially correct
65 Partially correct 22 ms 336 KB Output is partially correct
66 Partially correct 26 ms 328 KB Output is partially correct
67 Partially correct 24 ms 316 KB Output is partially correct
68 Partially correct 27 ms 324 KB Output is partially correct
69 Partially correct 21 ms 336 KB Output is partially correct
70 Partially correct 26 ms 316 KB Output is partially correct
71 Partially correct 30 ms 320 KB Output is partially correct
72 Partially correct 23 ms 344 KB Output is partially correct
73 Partially correct 21 ms 208 KB Output is partially correct
74 Partially correct 20 ms 324 KB Output is partially correct
75 Partially correct 26 ms 320 KB Output is partially correct
76 Partially correct 25 ms 336 KB Output is partially correct
77 Partially correct 24 ms 328 KB Output is partially correct
78 Partially correct 23 ms 332 KB Output is partially correct
79 Partially correct 24 ms 336 KB Output is partially correct
80 Partially correct 25 ms 316 KB Output is partially correct
81 Partially correct 23 ms 312 KB Output is partially correct
82 Partially correct 26 ms 336 KB Output is partially correct