QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#226738 | #4424. Babushka and her pierogi | 275307894a | AC ✓ | 466ms | 20964kb | C++14 | 1.6kb | 2023-10-26 15:05:35 | 2023-10-26 15:05:36 |
Judging History
answer
#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;using LL=__int128;
const int N=2e5+5,M=(1<<16)+5,K=(1<<25)+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(time(0));
int n,C,x;
struct Node{int A,B,id;}A[N];
int nxt[N],pre[N];
void Solve(){
int i,j;scanf("%d%d",&n,&C);
vector<pii> ans;ll sum=0;
queue<int> q;
auto Do=[&](int x,int y){
swap(nxt[x],nxt[y]);swap(A[x].A,A[y].A);
pre[nxt[x]]=x;pre[nxt[y]]=y;
ans.emplace_back(A[x].id,A[y].id);sum+=abs(A[y].A-A[x].A)+C;
};
auto chk=[&](int x,int y){
return A[y].B>=A[x].A&&A[y].A<=A[x].A;
};
unordered_map<int,int> f;
for(i=1;i<=n;i++) scanf("%d%d",&A[i].A,&A[i].B),A[i].id=i;
sort(A+1,A+n+1,[](Node x,Node y){return x.B<y.B;});
for(i=1;i<=n;i++) f[A[i].B]=i;
for(i=1;i<=n;i++) nxt[i]=f[A[i].A],pre[nxt[i]]=i;
for(i=1;i<=n;i++) while(nxt[i]^i){
int l=i,r=i;//cerr<<A[i].A<<' '<<A[i].B<<'\n';
while(1){
l=pre[l];r=nxt[r];
if(chk(i,l)) {Do(i,l);break;}
if(chk(i,r)) {Do(i,r);break;}
}
}
printf("%lld %d\n",sum,ans.size());
for(auto i:ans) printf("%d %d\n",i.fi,i.se);
}
int main(){
int t=1;
scanf("%d",&t);
while(t--) Solve();
cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 410ms
memory: 20720kb
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 2835 3026 1533 3026 907 3026 854 3026 733 2507 1361 2801 1624 2801 164 567 1819 567 1367 2189 1389 2189 1814 881 37 2329 2760 2243 843 1225 355 1225 1177 1225 3077 1225 621 1225 2382 2605 732 2605 1290 2605 1446 2605 1427 3073 1559 3176 642 3176 502 1969 346 1969 2122 1749 262...
result:
ok ac (1000 test cases)
Test #2:
score: 0
Accepted
time: 466ms
memory: 20952kb
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 167123 29816 151066 149462 156960 149462 199103 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 121873 93825 172619 93825 3986 93825 ...
result:
ok ac (5 test cases)
Test #3:
score: 0
Accepted
time: 432ms
memory: 20964kb
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