Submission #772546

#TimeUsernameProblemLanguageResultExecution timeMemory
772546kingfran1907Teleporters (IOI08_teleporters)C++14
100 / 100
879 ms64052 KiB
#include <bits/stdc++.h> using namespace std; typedef long long llint; const int maxn = 25e5+10; const int inf = 0x3f3f3f3f; int n, m; int l[maxn], r[maxn]; vector< int > v; vector< pair<int, int> > vs; int nex[maxn]; bool bio[maxn]; priority_queue< int > cycles; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d%d", l+i, r+i); l[n] = -1; r[n] = inf; n++; for (int i = 0; i < n; i++) { v.push_back(l[i]); v.push_back(r[i]); } sort(v.begin(), v.end()); for (int i = 0; i < n; i++) { l[i] = lower_bound(v.begin(), v.end(), l[i]) - v.begin(); r[i] = lower_bound(v.begin(), v.end(), r[i]) - v.begin(); vs.push_back({l[i], i}); vs.push_back({r[i], i}); } sort(vs.begin(), vs.end()); for (int i = 0; i < v.size() - 1; i++) { int ind = vs[i + 1].second; int rs = l[ind] + r[ind] - vs[i + 1].first; nex[i] = rs; } int sol = 0; for (int i = 0; i < v.size() - 1; i++) { //printf("trying: %d\n", i); if (bio[i]) continue; int cnt = 0; int ptr = i; //printf("cycle: "); while (!bio[ptr]) { //printf("%d ", ptr); bio[ptr] = true; ptr = nex[ptr]; cnt++; } //printf("\n"); if (i > 0) cycles.push(cnt); else sol = cnt - 1; } while (m && !cycles.empty()) { //printf("debug: %d\n", cycles.top()); sol += cycles.top() + 2; cycles.pop(); m--; } //printf("%d - %d\n", m, cycles.size()); m = m -= min(m, (int)cycles.size()); sol += 2 * (m - m % 2) + m % 2; printf("%d\n", sol); return 0; }

Compilation message (stderr)

teleporters.cpp: In function 'int main()':
teleporters.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |  for (int i = 0; i < v.size() - 1; i++) {
      |                  ~~^~~~~~~~~~~~~~
teleporters.cpp:45:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |  for (int i = 0; i < v.size() - 1; i++) {
      |                  ~~^~~~~~~~~~~~~~
teleporters.cpp:70:4: warning: operation on 'm' may be undefined [-Wsequence-point]
   70 |  m = m -= min(m, (int)cycles.size());
      |  ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
teleporters.cpp:18:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
teleporters.cpp:20:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |   scanf("%d%d", l+i, r+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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...