QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#432772#4424. Babushka and her pierogirizynvuAC ✓520ms12656kbC++141.9kb2024-06-07 17:12:362024-06-07 17:12:36

Judging History

你现在查看的是最新测评结果

  • [2024-06-07 17:12:36]
  • 评测
  • 测评结果:AC
  • 用时:520ms
  • 内存:12656kb
  • [2024-06-07 17:12:36]
  • 提交

answer

#include<bits/stdc++.h>
using ll = long long;
const int maxn = 2e5 + 10;
int p[maxn], q[maxn], idp[maxn], hs[maxn];
int nxt[maxn], pre[maxn];
bool vis[maxn];
inline void Main() {
   int n, C;
   scanf("%d%d", &n, &C);
   for (int i = 1; i <= n; i++)
      scanf("%d%d", &p[i], &q[i]), hs[i] = p[i];
   std::sort(hs + 1, hs + n + 1);
   for (int i = 1; i <= n; i++)
      p[i] = std::lower_bound(hs + 1, hs + n + 1, p[i]) - hs;
   for (int i = 1; i <= n; i++)
      q[i] = std::lower_bound(hs + 1, hs + n + 1, q[i]) - hs;
   for (int i = 1; i <= n; i++)
      idp[p[i]] = i, nxt[p[i]] = q[i];
   for (int i = 1; i <= n; i++)
      pre[nxt[i]] = i;
   ll ans = 0;
   std::vector<std::pair<int, int> > sp;
   auto Swap = [&](int x, int y) {
      ans += abs(hs[x] - hs[y]) + C;
      sp.emplace_back(idp[x], idp[y]);
      std::swap(idp[x], idp[y]);
      std::swap(nxt[x], nxt[y]), std::swap(pre[nxt[x]], pre[nxt[y]]);
   };
   memset(vis, 0, sizeof(bool) * (n + 1));
   for (int _ = 1; _ <= n; _++) {
      if (vis[_]) continue;
      int mx = 0;
      for (int u = _; ! vis[u]; u = nxt[u])
         mx = std::max(mx, u), vis[u] = 1;
      std::vector<int> P;
      P.push_back(mx);
      while (true) {
         int now = pre[P.back()];
         if (now == mx) {
            for (int i = 1; i < P.size(); i++)
               Swap(P[i - 1], P[i]);
            break;
         }
         std::vector<int> del;
         while (! P.empty() && P.back() < now)
            del.push_back(P.back()), P.pop_back();
         if (del.size()) {
            Swap(now, del.back());
            for (int i = del.size() - 1; i; i--)
               Swap(del[i], del[i - 1]);
         }
         P.push_back(now);
      }
   }
   printf("%lld %zu\n", ans, sp.size());
   for (auto _ : sp)
      printf("%d %d\n", _.first, _.second);
}
int main() {
   int T;
   scanf("%d", &T);
   while (T--) Main();
   return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 459ms
memory: 11572kb

input:

1000
3220 272696332
766498233 728308608
664527309 611122504
769309894 72297979
848647465 356897274
645591201 895123264
165094010 486891491
31233252 552012226
84149600 93558181
569880970 31233252
925517631 900673333
254671525 65782954
360356809 123019566
435505772 128102614
595657911 878072081
138392...

output:

1425165727822 3210
1244 2849
2783 2849
392 334
3193 334
903 334
2266 1410
2266 334
1018 1741
1628 2849
1628 1410
1628 1741
2236 2849
2298 2623
2414 2623
2414 2612
2414 852
1815 117
1366 2030
1366 1760
247 2623
247 117
247 2030
2795 1759
216 1698
1113 1759
1113 1698
2423 2623
2423 1759
2423 1332
122 ...

result:

ok ac (1000 test cases)

Test #2:

score: 0
Accepted
time: 520ms
memory: 11868kb

input:

5
200000 42944121
574814309 67495230
53276728 150326725
864637686 234510550
10036337 414740
506850796 483988482
236014120 843625769
809762843 19603788
507104953 885632626
866590783 119176179
188251555 598788216
652874956 535446529
193164017 235823046
925533029 523948529
50446166 36338992
380571133 6...

output:

43086875784557 199984
182980 6580
175195 108761
187430 6580
187430 108761
187430 151842
8162 100266
97806 156045
82857 100266
82857 156045
130977 193807
130977 6580
130977 100266
130977 156678
58928 71605
103239 158186
11958 131212
11958 90024
95379 158186
95379 131212
95379 125285
147206 136459
167...

result:

ok ac (5 test cases)

Test #3:

score: 0
Accepted
time: 475ms
memory: 12656kb

input:

5
200000 22877235
36648700 36642254
935593015 935591420
912067011 912055984
229315170 229314259
83126790 83123673
949986358 949980907
563637725 563627158
131644066 131631233
202694678 202691574
747915499 747915206
433149695 433125141
466955691 466945175
770435427 770425839
333350582 333349002
794680...

output:

4576424117766 199999
155223 13670
169318 13670
122272 13670
186701 13670
25846 13670
179211 13670
193487 13670
101581 13670
66607 13670
103962 13670
172890 13670
163342 13670
74383 13670
19151 13670
117913 13670
6901 13670
126267 13670
43459 13670
87097 13670
15151 13670
59717 13670
194422 13670
865...

result:

ok ac (5 test cases)

Extra Test:

score: 0
Extra Test Passed