Submission #795579

# Submission time Handle Problem Language Result Execution time Memory
795579 2023-07-27T11:26:49 Z SlavicG Minerals (JOI19_minerals) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
#include "minerals.h"
using namespace std;
 
#define all(v) v.begin(), v.end()
#define sz(a) (int)a.size()
#define pb push_back
#define rall(v) v.rbegin(),v.rend()
 
mt19937 rng(69);
void Solve(int n) {
    vector<int> p1, p2;
    int ans = 0, prevans = -1;
    vector<int> bulanea(2 * n);
    iota(bulanea.begin(), bulanea.end(), 1);
    shuffle(bulanea.begin(), bulanea.end(), rng);
    for(int i: bulanea) {
        ans = Query(i);
        if(ans == prevans) {
            p2.pb(i);
        } else {
            p1.pb(i);
        }
        prevans = ans;
    }
    assert(sz(p1) == n && sz(p2) == n);
    vector<int> pr(n, 0);
    int IN = 1;
    /*
    vector<bool> bagat(n, 1);
    for(int bit = 0; (1 << bit) < n; ++bit) {
        for(int i = 0; i < n; ++i) {
            if(i >> bit & 1) {
                if(!bagat[i]) prevans = Query(p1[i]);
                bagat[i] = 1;
            } else {
                if(bagat[i]) prevans = Query(p1[i]);
                bagat[i] = 0;
            }
        }   
        for(int i = 0; i < n; ++i) {
            int ans = Query(p2[i]);
            if(ans == prevans) {
                pr[i] += (1 << bit);
            }
            prevans = ans;
        }
        IN ^= 1;
    }
    */
    vector<int> l(n, 0), r(n, n - 1);
    int F = 0;
    int j = n - 1;
    for(int runs = 0; runs < 17; ++runs) {
        vector<pair<int, int>> b;
        vector<int> ids[n + 1];
        for(int i = 0; i < n; ++i) {
            if(l[i] <= r[i]) {
                int mid = l[i] + r[i] >> 1;
                b.push_back({mid, i});
                ids[mid].push_back(i);
            }
        }
        if(b.empty()) break;
        sort(all(b));
        int cost1 = 0, cost2 = 0;
        {
            map<int, int> vis;
            int k = j;
            while(k > b[0].first) {
                vis[k] = true;
                ++cost1;
                --k;
                vis[k] = true;
            }
            if(!vis[b.back().first]) {
                while(k < b.back().first) {
                    ++k;
                    ++cost1;
                }
            }
        }
        {
            int k = j;
            map<int, int> vis;
            while(k < b.back().first) {
                vis[k] = true;
                ++cost2;
                ++k;
                vis[k] = true;
            }
            if(!vis[b[0].first]) {
                while(k > b[0].first) {
                    --k;
                    ++cost2;
                }
            }
        }
        if(cost1 <= cost2) {
            map<int, int> vis;
            while(j > b[0].first) {
                if(!vis[j]) {
                    for(auto x: ids[j]) {
                        int ans = query(p2[x]);
                        if(ans == prevans) {
                            pr[x.second] = mid;
                            r[x.second] = mid - 1;
                        } else {
                            l[x.second] = mid + 1;
                        }
                        prevans = ans;
                    }
                    vis[j] = true;
                }
                prevans = Query(p1[j]);
                --j;
                if(!vis[j]) {
                    for(auto x: ids[j]) {
                        int ans = query(p2[x]);
                        if(ans == prevans) {
                            pr[x.second] = mid;
                            r[x.second] = mid - 1;
                        } else {
                            l[x.second] = mid + 1;
                        }
                        prevans = ans;
                    }
                    vis[j] = true;
                }
            }
            if(!vis[b.back().first]) {
                while(j < b.back().first) {
                    if(!vis[j]) {
                        for(auto x: ids[j]) {
                            int ans = query(p2[x]);
                            if(ans == prevans) {
                                pr[x.second] = mid;
                                r[x.second] = mid - 1;
                            } else {
                                l[x.second] = mid + 1;
                            }
                            prevans = ans;
                        }
                        vis[j] = true;
                    }
                    ++j;
                    prevans = Query(p1[j]);
                    if(!vis[j]) {
                        for(auto x: ids[j]) {
                            int ans = query(p2[x]);
                            if(ans == prevans) {
                                pr[x.second] = mid;
                                r[x.second] = mid - 1;
                            } else {
                                l[x.second] = mid + 1;
                            }
                            prevans = ans;
                        }
                        vis[j] = true;
                    }
                }
            }
        } else {
            map<int, int> vis;
            while(j < b.back().first) {
                if(!vis[j]) {
                    for(auto x: ids[j]) {
                        int ans = query(p2[x]);
                        if(ans == prevans) {
                            pr[x] = mid;
                            r[x] = mid - 1;
                        } else {
                            l[x] = mid + 1;
                        }
                        prevans = ans;
                    }
                    vis[j] = true;
                }
                ++j;
                prevans = Query(p1[j]);
                if(!vis[j]) {
                    for(auto x: ids[j]) {
                        int ans = query(p2[x]);
                        if(ans == prevans) {
                            pr[x] = mid;
                            r[x] = mid - 1;
                        } else {
                            l[x] = mid + 1;
                        }
                        prevans = ans;
                    }
                    vis[j] = true;
                }
            }
            if(!vis[b[0].first]) {
                while(j > b[0].first) {
                    if(!vis[j]) {
                        for(auto x: ids[j]) {
                            int ans = query(p2[x]);
                            if(ans == prevans) {
                                pr[x] = mid;
                                r[x] = mid - 1;
                            } else {
                                l[x] = mid + 1;
                            }
                            prevans = ans;
                        }
                        vis[j] = true;
                    }
                    prevans = Query(p1[j]);
                    --j;
                    if(!vis[j]) {
                        for(auto x: ids[j]) {
                            int ans = query(p2[x]);
                            if(ans == prevans) {
                                pr[x] = mid;
                                r[x] = mid - 1;
                            } else {
                                l[x] = mid + 1;
                            }
                            prevans = ans;
                        }
                        vis[j] = true;
                    }
                }
            }
        }
    }
    for(int i = 0; i < n; ++i) {
        Answer(p2[i], p1[pr[i]]);
    }
}
/*
4
1 5
2 6
3 4
7 8
*/

Compilation message

minerals.cpp: In function 'void Solve(int)':
minerals.cpp:59:32: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   59 |                 int mid = l[i] + r[i] >> 1;
minerals.cpp:104:35: error: 'query' was not declared in this scope; did you mean 'Query'?
  104 |                         int ans = query(p2[x]);
      |                                   ^~~~~
      |                                   Query
minerals.cpp:106:34: error: request for member 'second' in 'x', which is of non-class type 'int'
  106 |                             pr[x.second] = mid;
      |                                  ^~~~~~
minerals.cpp:106:44: error: 'mid' was not declared in this scope
  106 |                             pr[x.second] = mid;
      |                                            ^~~
minerals.cpp:107:33: error: request for member 'second' in 'x', which is of non-class type 'int'
  107 |                             r[x.second] = mid - 1;
      |                                 ^~~~~~
minerals.cpp:109:33: error: request for member 'second' in 'x', which is of non-class type 'int'
  109 |                             l[x.second] = mid + 1;
      |                                 ^~~~~~
minerals.cpp:109:43: error: 'mid' was not declared in this scope
  109 |                             l[x.second] = mid + 1;
      |                                           ^~~
minerals.cpp:119:35: error: 'query' was not declared in this scope; did you mean 'Query'?
  119 |                         int ans = query(p2[x]);
      |                                   ^~~~~
      |                                   Query
minerals.cpp:121:34: error: request for member 'second' in 'x', which is of non-class type 'int'
  121 |                             pr[x.second] = mid;
      |                                  ^~~~~~
minerals.cpp:121:44: error: 'mid' was not declared in this scope
  121 |                             pr[x.second] = mid;
      |                                            ^~~
minerals.cpp:122:33: error: request for member 'second' in 'x', which is of non-class type 'int'
  122 |                             r[x.second] = mid - 1;
      |                                 ^~~~~~
minerals.cpp:124:33: error: request for member 'second' in 'x', which is of non-class type 'int'
  124 |                             l[x.second] = mid + 1;
      |                                 ^~~~~~
minerals.cpp:124:43: error: 'mid' was not declared in this scope
  124 |                             l[x.second] = mid + 1;
      |                                           ^~~
minerals.cpp:135:39: error: 'query' was not declared in this scope; did you mean 'Query'?
  135 |                             int ans = query(p2[x]);
      |                                       ^~~~~
      |                                       Query
minerals.cpp:137:38: error: request for member 'second' in 'x', which is of non-class type 'int'
  137 |                                 pr[x.second] = mid;
      |                                      ^~~~~~
minerals.cpp:137:48: error: 'mid' was not declared in this scope
  137 |                                 pr[x.second] = mid;
      |                                                ^~~
minerals.cpp:138:37: error: request for member 'second' in 'x', which is of non-class type 'int'
  138 |                                 r[x.second] = mid - 1;
      |                                     ^~~~~~
minerals.cpp:140:37: error: request for member 'second' in 'x', which is of non-class type 'int'
  140 |                                 l[x.second] = mid + 1;
      |                                     ^~~~~~
minerals.cpp:140:47: error: 'mid' was not declared in this scope
  140 |                                 l[x.second] = mid + 1;
      |                                               ^~~
minerals.cpp:150:39: error: 'query' was not declared in this scope; did you mean 'Query'?
  150 |                             int ans = query(p2[x]);
      |                                       ^~~~~
      |                                       Query
minerals.cpp:152:38: error: request for member 'second' in 'x', which is of non-class type 'int'
  152 |                                 pr[x.second] = mid;
      |                                      ^~~~~~
minerals.cpp:152:48: error: 'mid' was not declared in this scope
  152 |                                 pr[x.second] = mid;
      |                                                ^~~
minerals.cpp:153:37: error: request for member 'second' in 'x', which is of non-class type 'int'
  153 |                                 r[x.second] = mid - 1;
      |                                     ^~~~~~
minerals.cpp:155:37: error: request for member 'second' in 'x', which is of non-class type 'int'
  155 |                                 l[x.second] = mid + 1;
      |                                     ^~~~~~
minerals.cpp:155:47: error: 'mid' was not declared in this scope
  155 |                                 l[x.second] = mid + 1;
      |                                               ^~~
minerals.cpp:168:35: error: 'query' was not declared in this scope; did you mean 'Query'?
  168 |                         int ans = query(p2[x]);
      |                                   ^~~~~
      |                                   Query
minerals.cpp:170:37: error: 'mid' was not declared in this scope
  170 |                             pr[x] = mid;
      |                                     ^~~
minerals.cpp:173:36: error: 'mid' was not declared in this scope
  173 |                             l[x] = mid + 1;
      |                                    ^~~
minerals.cpp:183:35: error: 'query' was not declared in this scope; did you mean 'Query'?
  183 |                         int ans = query(p2[x]);
      |                                   ^~~~~
      |                                   Query
minerals.cpp:185:37: error: 'mid' was not declared in this scope
  185 |                             pr[x] = mid;
      |                                     ^~~
minerals.cpp:188:36: error: 'mid' was not declared in this scope
  188 |                             l[x] = mid + 1;
      |                                    ^~~
minerals.cpp:199:39: error: 'query' was not declared in this scope; did you mean 'Query'?
  199 |                             int ans = query(p2[x]);
      |                                       ^~~~~
      |                                       Query
minerals.cpp:201:41: error: 'mid' was not declared in this scope
  201 |                                 pr[x] = mid;
      |                                         ^~~
minerals.cpp:204:40: error: 'mid' was not declared in this scope
  204 |                                 l[x] = mid + 1;
      |                                        ^~~
minerals.cpp:214:39: error: 'query' was not declared in this scope; did you mean 'Query'?
  214 |                             int ans = query(p2[x]);
      |                                       ^~~~~
      |                                       Query
minerals.cpp:216:41: error: 'mid' was not declared in this scope
  216 |                                 pr[x] = mid;
      |                                         ^~~
minerals.cpp:219:40: error: 'mid' was not declared in this scope
  219 |                                 l[x] = mid + 1;
      |                                        ^~~
minerals.cpp:28:9: warning: unused variable 'IN' [-Wunused-variable]
   28 |     int IN = 1;
      |         ^~
minerals.cpp:52:9: warning: unused variable 'F' [-Wunused-variable]
   52 |     int F = 0;
      |         ^