제출 #930422

#제출 시각아이디문제언어결과실행 시간메모리
930422boris_mihovEvent Hopping 2 (JOI21_event2)C++17
0 / 100
3053 ms5736 KiB
#include <algorithm> #include <iostream> #include <numeric> #include <cassert> #include <cstring> #include <vector> #include <queue> #include <stack> #include <set> typedef long long llong; const int MAXN = 100000 + 10; const int INF = 2e9; int n, k; int l[MAXN]; int r[MAXN]; int perm[MAXN]; std::vector <int> sol; std::vector <int> ans; std::vector <int> remaining; bool intersecting(int x, int y) { return std::max(l[x], l[y]) < std::min(r[x], r[y]); } std::pair <int,int> canAdd(int idx) { int cntLeft = sol.size(); for (const int &i : ans) { if (intersecting(idx, i)) { return {-1, -1}; } } int maxPopped = 0; int minPopped = INF; for (const int &i : sol) { if (intersecting(idx, i)) { maxPopped = std::max(maxPopped, r[i]); minPopped = std::min(minPopped, l[i]); cntLeft--; } } int allowedTo = INF; int allowedFrom = 0; for (const int &i : sol) { if (l[i] >= maxPopped) { allowedTo = std::min(allowedTo, l[i]); } if (r[i] <= minPopped) { allowedFrom = std::min(allowedFrom, r[i]); } } int coolIdx = 0; int coolIdx2 = 0; for (const int &i : remaining) { if (r[i] <= allowedTo && l[i] >= r[idx]) { // std::cout << coolUIDx if (coolIdx == 0 || (r[i] < r[coolIdx]) || (r[i] == r[coolIdx] && l[i] > l[coolIdx])) { coolIdx = i; } } if (l[i] >= allowedFrom && r[i] <= l[idx]) { if (coolIdx2 == 0 || (r[i] < r[coolIdx2]) || (r[i] == r[coolIdx2] && l[i] > l[coolIdx2])) { coolIdx2 = i; } } } if ((coolIdx != 0) + (coolIdx2 != 0) - (coolIdx != 0 && coolIdx == coolIdx2) + cntLeft + ans.size() + 1 >= k) { return {coolIdx, coolIdx2}; } else { return {-1, -1}; } } void removeIntersecting(int idx) { std::vector <int> sol2; for (const int &i : sol) { if (!intersecting(idx, i)) { sol2.push_back(i); } } sol = sol2; std::vector <int> rem2; for (const int &i : remaining) { if (!intersecting(idx, i)) { rem2.push_back(i); } } remaining = rem2; } void solve() { std::vector <std::pair <int, std::pair <int,bool>>> v; for (int i = 1 ; i <= n ; ++i) { v.push_back({l[i], {i, false}}); v.push_back({r[i], {i, true}}); } int cnt = 0; int last = 0; std::sort(v.begin(), v.end()); for (int i = 0 ; i < 2 * n ; ++i) { cnt += (v[i].first > last); last = v[i].first; if (v[i].second.second) r[v[i].second.first] = cnt; else l[v[i].second.first] = cnt; } // std::cout << n << ' ' << k << '\n'; // for (int i = 1 ; i <= n ; ++i) // { // std::cout << l[i] << ' ' << r[i] << '\n'; // } std::iota(perm + 1, perm + 1 + n, 1); std::sort(perm + 1, perm + 1 + n, [&](int x, int y) { return r[x] < r[y] || (r[x] == r[y] && l[x] > l[y]); }); for (int i = 1 ; i <= n ; ++i) { if (sol.empty() || !intersecting(sol.back(), perm[i])) { sol.push_back(perm[i]); } else { remaining.push_back(perm[i]); } } if (sol.size() < k) { std::cout << -1 << '\n'; return; } // std::cout << "sol is\n"; // for (const int &idx : sol) // { // std::cout << idx << ' ' << l[idx] << ' ' << r[idx] << '\n'; // } for (int i = 1 ; i <= n && ans.size() < k ; ++i) { auto [res, second] = canAdd(i); // std::cout << "res: " << res << if (res != -1) { ans.push_back(i); removeIntersecting(i); if (res != 0) { // std::cout << " bonus: " << i << ' ' << res << '\n'; sol.push_back(res); for (int &i : remaining) { if (i == res) { std::swap(i, remaining.back()); remaining.pop_back(); break; } } } if (second != 0) { // std::cout << " bonus: " << i << ' ' << res << '\n'; sol.push_back(second); for (int &i : remaining) { if (i == second) { std::swap(i, remaining.back()); remaining.pop_back(); break; } } } } } for (const int &idx : ans) { std::cout << idx << '\n'; } } void input() { std::cin >> n >> k; for (int i = 1 ; i <= n ; ++i) { std::cin >> l[i] >> r[i]; // l[i]++; } } void fastIOI() { std::ios_base :: sync_with_stdio(0); std::cout.tie(nullptr); std::cin.tie(nullptr); } int main() { fastIOI(); input(); solve(); return 0; }

컴파일 시 표준 에러 (stderr) 메시지

event2.cpp: In function 'std::pair<int, int> canAdd(int)':
event2.cpp:89:110: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   89 |     if  ((coolIdx != 0) + (coolIdx2 != 0) - (coolIdx != 0 && coolIdx == coolIdx2) + cntLeft + ans.size() + 1 >= k)
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
event2.cpp: In function 'void solve()':
event2.cpp:167:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  167 |     if (sol.size() < k)
      |         ~~~~~~~~~~~^~~
event2.cpp:179:43: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  179 |     for (int i = 1 ; i <= n && ans.size() < k ; ++i)
      |                                ~~~~~~~~~~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...