이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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]);
}
int canAdd(int idx)
{
int cntLeft = sol.size();
for (const int &i : ans)
{
if (intersecting(idx, i))
{
return -1;
}
}
int maxPopped = 0;
for (const int &i : sol)
{
if (intersecting(idx, i))
{
maxPopped = std::max(maxPopped, r[i]);
cntLeft--;
}
}
int coolIdx = 0;
for (const int &i : remaining)
{
if (l[i] >= r[idx] && l[i] <= maxPopped)
{
coolIdx = idx;
break;
}
}
// std::cout << "cool idx is: " << coolIdx << '\n';
if ((coolIdx > 0) + cntLeft + ans.size() + 1 >= k)
{
return coolIdx;
} else
{
return -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::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;
}
for (int i = 1 ; i <= n && ans.size() < k ; ++i)
{
int res = 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;
// }
// }
// }
}
}
while (ans.size() < k);
assert(ans.size() == k);
while (ans.size() > k) ans.pop_back();
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 'int canAdd(int)':
event2.cpp:61:51: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
61 | if ((coolIdx > 0) + cntLeft + ans.size() + 1 >= k)
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
event2.cpp: In function 'void solve()':
event2.cpp:114:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
114 | if (sol.size() < k)
| ~~~~~~~~~~~^~~
event2.cpp:120:43: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
120 | for (int i = 1 ; i <= n && ans.size() < k ; ++i)
| ~~~~~~~~~~~^~~
event2.cpp:145:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
145 | while (ans.size() < k);
| ~~~~~~~~~~~^~~
In file included from /usr/include/c++/10/cassert:44,
from event2.cpp:4:
event2.cpp:146:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
146 | assert(ans.size() == k);
| ~~~~~~~~~~~^~~~
event2.cpp:147:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
147 | while (ans.size() > k) ans.pop_back();
| ~~~~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |