QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#352554 | #4424. Babushka and her pierogi | ucup-team1209# | AC ✓ | 531ms | 10192kb | C++20 | 2.0kb | 2024-03-13 12:36:14 | 2024-03-13 12:36:14 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,x,y) for (int i=(x);i>=(y);i--)
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
template<typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template<typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
void file() {
#ifdef zqj
freopen("a.in","r",stdin);
#endif
}
typedef long long ll;
#define sz 202020
int n; ll C;
int q[sz],p[sz];
int pos[sz],pre[sz],nxt[sz];
vector<pii>aa;
ll tot;
void Swap(int x,int y) {
tot+=abs(p[x]-p[y]);
int nx=nxt[x],ny=nxt[y];
nxt[x]=ny,nxt[y]=nx;
pre[nx]=y,pre[ny]=x;
swap(pos[x],pos[y]);
aa.push_back({pos[x],pos[y]});
}
void work() {
cin>>n>>C;
ll ans=0;
rep(i,1,n) cin>>q[i]>>p[i],ans+=abs(q[i]-p[i]);
ans/=2;
vector<int>V(q+1,q+n+1);
sort(V.begin(),V.end());
rep(i,1,n) {
int x=lower_bound(V.begin(),V.end(),q[i])-V.begin()+1;
int y=lower_bound(V.begin(),V.end(),p[i])-V.begin()+1;
nxt[x]=y,pre[y]=x,pos[x]=i;
}
sort(p+1,p+n+1);
drep(i,n,1) while (nxt[i]!=i) {
// rep(j,1,n) assert(pre[nxt[j]]==j);
if (pre[i]==i) continue;
if (pre[pre[i]]==i) {
Swap(i,pre[i]);
continue;
}
int x=pre[i],y=nxt[i];
if (y<pre[i]) {
Swap(x,i);
continue;
}
while (233) {
x=pre[x],y=nxt[y];
if (y<pre[i]) {
Swap(pre[i],pre[y]);
break;
}
if (x>pre[i]) {
Swap(x,pre[i]);
break;
}
}
}
rep(i,1,n) assert(nxt[i]==i);
assert(tot==ans);
ans+=1ll*C*aa.size();
cout<<ans<<' '<<aa.size()<<'\n';
for (auto [x,y]:aa) cout<<x<<' '<<y<<'\n';
aa.clear(); tot=0;
}
int main() {
file();
ios::sync_with_stdio(false),cin.tie(0);
int T; cin>>T;
while (T--) work();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 475ms
memory: 10192kb
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 2616 308 270 3064 1220 704 704 692 2736 1847 736 1930 1930 2215 510 2400 2400 1082 584 1517 330 2327 197 330 1790 892 180 1790 718 2610 606 1460 2017 723 594 2017 245 2715 2715 270 170 2343 2773 2578 2578 60 2142 1095 764 2854 1454 3025 2609 1454 1454 3114 1860 1771 2485 1860 1860...
result:
ok ac (1000 test cases)
Test #2:
score: 0
Accepted
time: 531ms
memory: 9936kb
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 111370 197943 138351 193807 193807 54506 101256 52147 155777 109517 39985 199311 199311 109048 96416 173518 96416 98032 67822 96416 187136 96416 76924 118088 158884 76924 65279 171566 25127 171566 149102 126922 100871 126922 57396 141120 171025 156412 191791 29126 136638 81214 ...
result:
ok ac (5 test cases)
Test #3:
score: 0
Accepted
time: 491ms
memory: 9924kb
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