QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#417967 | #4424. Babushka and her pierogi | Bronya | AC ✓ | 380ms | 11484kb | C++14 | 2.0kb | 2024-05-23 07:20:08 | 2024-05-23 07:20:10 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int n,C;
int p[200005];
int val[200005];
// unordered_map<int,int>id;
bool pd[200005];
int last[200005];
vector<pair<int,int> >lans;
int read(){
int x=0;
char c=getchar();
while(!isdigit(c))c=getchar();
while(isdigit(c))x=(x<<3)+(x<<1)+(c-'0'),c=getchar();
return x;
}
int id1[200005],id2[200005];
bool cmp(int x,int y){
return p[x]<p[y];
}
bool cmpp(int x,int y){
return val[x]<val[y];
}
void Solve(){
// scanf("%d%d",&n,&C);
n=read();C=read();
lans.clear();
for(int i=1;i<=n;i++){
// scanf("%d%d",&p[i],&val[i]);
p[i]=read();val[i]=read();
pd[i]=false;
id1[i]=id2[i]=i;
}
sort(id1+1,id1+1+n,cmp);
sort(id2+1,id2+1+n,cmpp);
for(int i=1;i<=n;i++)p[id1[i]]=id2[i];
long long sum=0,ans=1ll*C*n;
for(int i=1;i<=n;i++)sum+=abs(val[i]-val[p[i]]);
for(int i=1;i<=n;i++){
if(!pd[i]){
int x=i,y=i,sav;
while(!pd[x]){
if(val[x]<val[y])y=x;
pd[x]=true;
x=p[x];
}
x=p[y];sav=y;
stack<int>st;ans-=C;
st.push(y);
while(x!=y){
if(val[x]>val[y])st.push(x);
else {
int fi=0;vector<int>dl;
while(!st.empty()&&val[st.top()]>val[x]){
fi=st.top();dl.push_back(fi);
st.pop();
}
lans.push_back({st.top(),y});
for(int j=dl.size()-1;j>=1;j--)lans.push_back({dl[j],y});
if(x==sav)break;
st.push(x);
}
y=x;x=p[x];
}
}
}
printf("%lld %d\n",sum/2+ans,(int)lans.size());
for(auto [x,y]:lans)printf("%d %d\n",x,y);
}
int main(){
int T;
T=read();
while(T--)Solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 320ms
memory: 9716kb
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 940 1956 520 1956 940 2853 2074 2853 2076 1552 2121 1552 2076 2441 985 2441 940 1268 2076 1268 2675 1720 193 2248 940 1625 2675 1625 193 1625 2516 305 2516 2402 1986 2402 2272 2567 2272 2691 1748 2691 1217 2510 940 2636 2516 2636 2272 2636 1217 2636 578 2509 578 1409 2306 1372 578...
result:
ok ac (1000 test cases)
Test #2:
score: 0
Accepted
time: 380ms
memory: 9260kb
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 9721 175284 133798 175284 3642 161990 190482 188021 55945 94776 148909 94776 3642 75919 190482 75919 55945 75919 3642 3950 110105 3950 127873 129255 52219 132836 158091 132836 12647 73105 9721 105530 3642 105530 127873 105530 52219 105530 12647 105530 128025 50506 128025 151822...
result:
ok ac (5 test cases)
Test #3:
score: 0
Accepted
time: 357ms
memory: 11484kb
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