QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#121215#4424. Babushka and her pierogizlxFTHAC ✓507ms14596kbC++141.4kb2023-07-07 19:31:532023-07-07 19:31:55

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-07 19:31:55]
  • 评测
  • 测评结果:AC
  • 用时:507ms
  • 内存:14596kb
  • [2023-07-07 19:31:53]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

void solve() {
  int n;
  cin >> n;
  i64 c;
  cin >> c;
  vector<i64> a(n), b(n), d(n), ra(n), rb(n);
  i64 ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    ans += abs(a[i] - b[i]);
    d[i] = b[i];
  }
  ans /= 2;
  sort(d.begin(), d.end());
  for (int i = 0; i < n; i++) {
    a[i] = lower_bound(d.begin(), d.end(), a[i]) - d.begin();
    b[i] = lower_bound(d.begin(), d.end(), b[i]) - d.begin();
    ra[a[i]] = i;
    rb[b[i]] = i;
  }
  vector<pair<int, int>> op;
  auto work = [&](int u, int v) {
    ans += c;
    ra[a[u]] = v;
    ra[a[v]] = u;
    swap(a[u], a[v]);
    op.push_back({u, v});
  };
  for (int cur = 0; cur < n; cur++) {
    int u = rb[cur];
    while (b[u] != a[u]) {
      auto ok = [&](int v) {
        return a[u] <= b[v] && a[u] > a[v];
      };
      int v1 = u, v2 = u;
      while (1) {
        v1 = rb[a[v1]];
        v2 = ra[b[v2]];
        if (ok(v1)) {
          work(u, v1);
          break;
        }
        if (ok(v2)) {
          work(u, v2);
          break;
        }
      }
    }
  }
  cout << ans << " " << op.size() << "\n";
  for (auto &[x, y] : op) {
    cout << x + 1 << " " << y + 1 << "\n";
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 456ms
memory: 14596kb

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
866 2743
866 2835
3026 1533
3026 907
3026 854
3026 733
2507 1361
2801 1624
2801 164
567 1819
567 1367
2189 1389
2189 1814
881 451
881 37
2329 2412
2329 1319
2329 2760
2243 1110
2243 843
1225 355
1225 1177
1225 3077
1225 621
1225 2382
2605 732
2605 1290
2605 1446
2605 1427
3073 716...

result:

ok ac (1000 test cases)

Test #2:

score: 0
Accepted
time: 507ms
memory: 14464kb

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
176783 177024
176783 167123
29816 151066
149462 59325
149462 168834
47437 19518
47437 121747
77177 164375
77177 82430
77177 32697
77177 71557
77177 129942
55856 54145
491 193283
491 45134
491 27057
491 46967
9721 142843
54648 141633
195822 51638
195822 121873
93825 172619
93825...

result:

ok ac (5 test cases)

Test #3:

score: 0
Accepted
time: 495ms
memory: 14424kb

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