QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#419661 | #4424. Babushka and her pierogi | DaiRuiChen007 | AC ✓ | 413ms | 11064kb | C++14 | 1.1kb | 2024-05-24 08:25:31 | 2024-05-24 08:25:32 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int MAXN=2e5+5;
int p[MAXN];
array <int,3> a[MAXN];
bool vis[MAXN];
void solve() {
int n,cnt=0; long long ans=0,C;
scanf("%d%lld",&n,&C);
for(int i=1;i<=n;++i) {
scanf("%d%d",&a[i][1],&a[i][0]);
ans+=abs(a[i][0]-a[i][1]),a[i][2]=i;
}
sort(a+1,a+n+1),ans/=2;
for(int i=1;i<=n;++i) p[i]=lower_bound(a+1,a+n+1,array<int,3>{a[i][1],0,0})-a;
vector <array<int,2>> wys;
auto opr=[&](int x,int y) { swap(p[x],p[y]),wys.push_back({a[x][2],a[y][2]}); };
fill(vis+1,vis+n+1,0);
for(int i=1;i<=n;++i) if(!vis[i]) {
++cnt;
int x=i;
for(int j=i;!vis[j];j=p[j]) vis[j]=true,x=max(x,j);
int u=x; vector <int> q;
while(p[u]!=x) {
if(p[u]>u) {
q.erase(upper_bound(q.begin(),q.end(),p[u],greater<int>()),q.end());
int v=q.back();
opr(v,u);
while(p[u]!=u) opr(u,p[u]);
u=p[v];
} else q.push_back(u),u=p[u];
}
while(p[u]!=u) opr(u,p[u]);
}
printf("%lld %d\n",ans+1ll*C*(n-cnt),n-cnt);
for(auto z:wys) printf("%d %d\n",z[0],z[1]);
}
signed main() {
int T; scanf("%d",&T);
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 385ms
memory: 10156kb
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 2849 1244 2849 2783 334 392 334 3193 334 903 1410 2266 2266 334 1741 1018 2849 1628 1628 1410 1628 1741 2849 2236 2623 2298 2623 2414 2414 2612 2414 852 117 1815 2030 1366 1366 1760 2623 247 247 117 247 2030 1759 2795 1698 216 1759 1113 1113 1698 2623 2423 2423 1759 2423 1332 2715...
result:
ok ac (1000 test cases)
Test #2:
score: 0
Accepted
time: 413ms
memory: 10308kb
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 6580 182980 108761 175195 6580 187430 187430 108761 187430 151842 100266 8162 156045 97806 100266 82857 82857 156045 193807 130977 130977 6580 130977 100266 130977 156678 71605 58928 158186 103239 131212 11958 11958 90024 158186 95379 95379 131212 95379 125285 136459 147206 192...
result:
ok ac (5 test cases)
Test #3:
score: 0
Accepted
time: 325ms
memory: 11064kb
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 13670 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 136...
result:
ok ac (5 test cases)
Extra Test:
score: 0
Extra Test Passed