QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#432881#4424. Babushka and her pierogi11d10xyAC ✓2969ms27588kbC++141.1kb2024-06-07 19:36:262024-06-07 19:36:26

Judging History

This is the latest submission verdict.

  • [2024-06-07 19:36:26]
  • Judged
  • Verdict: AC
  • Time: 2969ms
  • Memory: 27588kb
  • [2024-06-07 19:36:26]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
using i64=long long;
void solve(){
   int n,C;cin>>n>>C;
   vector<pair<int,int>>opt;
   vector<int>p(n),q(n);
   map<int,int>ip,iq;
   i64 ans=0;
   for(int i=0;i<n;i++){
      scanf("%d%d",&p[i],&q[i]);
      ip[p[i]]=i,iq[q[i]]=i;
   }
   auto swaper=[&](int x,int y){
      x=ip[x],y=ip[y],swap(ip[p[x]],ip[p[y]]),swap(p[x],p[y]),
      ans+=C+abs(p[x]-p[y]),opt.emplace_back(x+1,y+1);
   };
   auto pre=[&](int x){return p[iq[x]];};
   auto nex=[&](int x){return q[ip[x]];};
   auto sortcyc_=[&](int lst){
      for(int u;(u=pre(lst))!=lst;swaper(u,lst),lst=u);
   };
   auto sortcyc=[&](int x){
      int mx=x;
      for(int i=nex(x);i!=x;i=nex(i))mx=max(mx,i);
      int p=mx;
      for(int v;(v=pre(p))!=mx;p=v)if(v>p){
         for(;v>nex(p);p=nex(p));
         swaper(v,p),sortcyc_(p);
      }
      sortcyc_(mx);
   };
   for(i64 x:p)if(pre(x)!=x)sortcyc(x);
   printf("%lld %d\n",ans,(int)opt.size());
   for(auto[x,y]:opt)printf("%d %d\n",x,y);
}
int main(){
   int T;for(cin>>T;T--;)solve();
   return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2206ms
memory: 27208kb

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
1481 1460
1873 1203
1341 1460
1203 1341
2477 1341
2132 1853
966 2132
1316 1460
1853 1316
1582 1316
1089 2161
864 1631
2161 864
2825 2465
3119 822
446 822
977 2465
822 977
3060 751
1138 751
2682 751
344 751
2431 344
2149 1631
2465 2149
751 2149
93 738
2684 93
513 738
2981 147
2227 ...

result:

ok ac (1000 test cases)

Test #2:

score: 0
Accepted
time: 2969ms
memory: 27588kb

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
24121 197943
125560 159724
107435 159724
179717 107435
168543 159724
100035 56921
33755 2742
125703 56921
2742 125703
184204 166146
173719 166146
129321 197943
159724 129321
56921 129321
166146 129321
110151 129321
89116 129321
144733 180128
32681 50982
74584 32681
187243 32681...

result:

ok ac (5 test cases)

Test #3:

score: 0
Accepted
time: 2142ms
memory: 27384kb

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