Submission #632663

# Submission time Handle Problem Language Result Execution time Memory
632663 2022-08-20T14:20:17 Z Sam_a17 Parking (CEOI22_parking) C++14
2 / 100
2000 ms 524288 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;

#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif

#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());

#define pb push_back
#define popf pop_front
#define popb pop_back
#define ld long double
#define ll long long

void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
void print(long double t) {cerr << t;}

template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T> void print(T v[],T n) {cerr << "["; for(int i = 0; i < n; i++) {cerr << v[i] << " ";} cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'

// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};

// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();

  if (str != "") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  }
}

// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 3e5 + 10;
long long n, m, d;
deque<int> a[N];
deque<pair<int, int>> color[N];

void solve_() {
  cin >> n >> m;

  vector<int> empty, non_match;
  for(int i = 0; i < m; i++) {
    a[i].resize(2);
    cin >> a[i][0] >> a[i][1];

    if(a[i][0] && a[i][1] && a[i][0] != a[i][1]) {
      color[a[i][0]].push_back({32, i});
      color[a[i][1]].push_back({8, i});
    } else if(a[i][0] && a[i][0] != a[i][1]) {
      color[a[i][0]].push_back({1, i});
    }
 
    if(!a[i][0]) {
      empty.push_back(i);
      a[i].pop_back();
      a[i].pop_back();
    }
  }

  set<pair<int, int>> st;
  for(int i = 1; i <= n; i++) {
    if(color[i].empty()) continue;
    // dbg(color[i][0])
    // dbg(color[i][1])
    st.insert({color[i][0].first + color[i][1].first, i});
  }

  vector<pair<int, int>> answer;
  while(!st.empty()) {
    auto u = *st.begin();
    st.erase(st.begin());
    // dbg(u)

    if(u.first == 2) {
      int curr_color = u.second;
      int ft = color[curr_color][0].second;
      int sc = color[curr_color][1].second;
      answer.push_back({ft + 1, sc + 1});
      
      a[ft].pop_front();    
      empty.push_back(ft + 1);
    } else if(u.first == 9) {
      int curr_color = u.second;
      int ft = color[curr_color][0].second;
      int sc = color[curr_color][1].second;

      if(color[curr_color][0].first < color[curr_color][1].first) {
        swap(ft, sc);
      }

      answer.push_back({ft + 1, sc + 1});
      a[ft].pop_back();

      int need_to_update = a[ft][0];
      int sum = color[need_to_update][0].first + color[need_to_update][1].first;
      
      auto need_to_find = st.find({sum, need_to_update});
      // if(need_to_find != st.end()) {
        st.erase(need_to_find);
      // }

      if(color[need_to_update][0].second == ft) {
        color[need_to_update][0].first = 1;
      } else {
        color[need_to_update][1].first = 1;
      }

      st.insert({color[need_to_update][0].first + color[need_to_update][1].first, need_to_update});
      // dbg(st)
    } else if(u.first == 16) {
      if(empty.empty()) {
        cout << -1 << endl;
        return;
      }
      int curr_color = u.second;
      int ft = color[curr_color][0].second;
      int sc = color[curr_color][1].second;

      auto empty_pos = empty.front();
      empty.pop_back();
      answer.push_back({ft + 1, empty_pos + 1}); 
      answer.push_back({sc + 1, empty_pos + 1});

      {
        a[ft].pop_back();
        int need_to_update = a[ft][0];
        int sum = color[need_to_update][0].first + color[need_to_update][1].first;
        
        auto need_to_find = st.find({sum, need_to_update});
        if(need_to_find != st.end()) {
          st.erase(need_to_find);
        }

        if(color[need_to_update][0].second == ft) {
          color[need_to_update][0].first = 1;
        } else {
          color[need_to_update][1].first = 1;
        }

        st.insert({color[need_to_update][0].first + color[need_to_update][1].first, need_to_update});
      }

      {
        ft = sc;
        a[ft].pop_back();
        int need_to_update = a[ft][0];
        int sum = color[need_to_update][0].first + color[need_to_update][1].first;
        
        auto need_to_find = st.find({sum, need_to_update});
        if(need_to_find != st.end()) {
          st.erase(need_to_find);
        }

        if(color[need_to_update][0].second == ft) {
          color[need_to_update][0].first = 1;
        } else {
          color[need_to_update][1].first = 1;
        }

        st.insert({color[need_to_update][0].first + color[need_to_update][1].first, need_to_update});
      }

    } else if(u.first == 40 || u.first == 33) {
      if(empty.empty()) {
        cout << -1 << endl;
        return;
      }

      int curr_color = u.second;
      int ft = color[curr_color][0].second;
      int sc = color[curr_color][1].second;

      if(color[curr_color][0].first < color[curr_color][1].first) {
        swap(ft, sc);
      }
      
      // dbg(ft)
      // ft takna
      auto empty_pos = empty.back();
      empty.pop_back();
      
      {
        //taki vriny qcum enq empty_pos
        answer.push_back({ft + 1, empty_pos + 1});

        {
          // hanum qcum enq
          a[empty_pos].push_back(a[ft].back());
          // dbg(a[empty_pos])
          a[ft].pop_back();

          int need_to_update = a[empty_pos][0];
          int sum = color[need_to_update][0].first + color[need_to_update][1].first;
          
          auto need_to_find = st.find({sum, need_to_update});
          if(need_to_find != st.end()) {
            st.erase(need_to_find);
          }
          // dbg(color[need_to_update][0])
          // dbg(color[need_to_update][1])


          if(color[need_to_update][0].second == ft) {
            color[need_to_update][0].first = 1;
            color[need_to_update][0].second = empty_pos;
          } else {
            color[need_to_update][1].first = 1;
            color[need_to_update][1].second = empty_pos;
          }

          st.insert({color[need_to_update][0].first + color[need_to_update][1].first, need_to_update});
        }

        int need_to_update = a[ft][0];
        if(color[need_to_update][0].second == ft) {
          color[need_to_update][0].first = 1;
        } else {
          color[need_to_update][1].first = 1;
        }
        st.insert({color[need_to_update][0].first + color[need_to_update][1].first, need_to_update});
      }
    }  else {
      // assert(u.second == 64);
      cout << -1 << endl;
      return;
    }

  }

  cout << sz(answer) << endl;
  for(auto i: answer) {
    cout << i.first << " " << i.second << endl;
  } 
}

int main () {
  setIO("");

  auto solve = [&](int test_case)-> void {
    while(test_case--) {
      solve_();
    }
  };

  int test_cases = 1; // cin >> test_cases;
  solve(test_cases);

  return 0;
}

Compilation message

Main.cpp: In function 'void setIO(std::string)':
Main.cpp:65:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:66:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   66 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 252 ms 404140 KB Output is correct
2 Correct 278 ms 404156 KB Output is correct
3 Correct 268 ms 404196 KB Output is correct
4 Execution timed out 2111 ms 404152 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Partially correct 516 ms 410232 KB Output is partially correct
2 Partially correct 528 ms 411664 KB Output is partially correct
3 Partially correct 431 ms 408836 KB Output is partially correct
4 Partially correct 514 ms 408980 KB Output is partially correct
5 Partially correct 563 ms 411332 KB Output is partially correct
# Verdict Execution time Memory Grader output
1 Incorrect 255 ms 404332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 255 ms 404332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Partially correct 245 ms 404224 KB Output is partially correct
2 Correct 261 ms 404308 KB Output is correct
3 Partially correct 259 ms 404300 KB Output is partially correct
4 Runtime error 539 ms 524288 KB Execution killed with signal 6
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 252 ms 404140 KB Output is correct
2 Correct 278 ms 404156 KB Output is correct
3 Correct 268 ms 404196 KB Output is correct
4 Execution timed out 2111 ms 404152 KB Time limit exceeded
5 Halted 0 ms 0 KB -