QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#424924#4424. Babushka and her pierogiffffycAC ✓127ms29868kbC++143.0kb2024-05-29 19:55:462024-05-29 19:55:48

Judging History

你现在查看的是最新测评结果

  • [2024-05-29 19:55:48]
  • 评测
  • 测评结果:AC
  • 用时:127ms
  • 内存:29868kb
  • [2024-05-29 19:55:46]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
namespace IO{//by cyffff
	int len=0;
	char ibuf[(1<<21)+1],*iS,*iT,out[(1<<25)+1];
	#if ONLINE_JUDGE
	#define gh() (iS==iT?iT=(iS=ibuf)+fread(ibuf,1,(1<<21)+1,stdin),(iS==iT?EOF:*iS++):*iS++)
 	#else
	#define gh() getchar()
	#endif
	#define reg register
	inline int read(){
		reg char ch=gh();
		reg int x=0;
		reg char t=0;
		while(ch<'0'||ch>'9')   t|=ch=='-',ch=gh();
		while(ch>='0'&&ch<='9') x=x*10+(ch^48),ch=gh();
		return t?-x:x;
	}
	inline void putc(char ch){
		out[len++]=ch;
	}
	template<class T>
	inline void write(T x){
		if(x<0)putc('-'),x=-x;
		if(x>9)write(x/10);
		out[len++]=x%10+48;
	}
	inline void flush(){
		fwrite(out,1,len,stdout);
		len=0;
	}
	inline char getc(){
		char ch=gh();
		while(ch<'A'||ch>'Z') ch=gh();
		return ch;
	}
}
using IO::read;
using IO::write;
using IO::flush;
using IO::getc;
using IO::putc;
#define vi vector<int>
#define pii pair<int,int>
#define mpr make_pair
#define fir first 
#define sec second
const int N=2e5+10;
int T,n,c,p[N],q[N],v[N],vis[N];
template<class P,class Q>
class HashTable{
	static const int mod=5e5+9,L=mod+10;
	int head[L],nxt[N],siz;
	P val[N];
	Q sav[N],z;
    public:
	inline void clear(){
		for(int i=1;i<=siz;i++){
			head[val[i]%mod]=0,nxt[i]=val[i]=0;
			Q tmp=z;
			swap(sav[i],tmp);
		}
		siz=0;
	}
	inline void insert(P y){
		val[++siz]=y;
		nxt[siz]=head[y%mod];
		head[y%mod]=siz;
	}
	inline Q &operator[](const P y){
		int x=y%mod;
		for(int i=head[x];i;i=nxt[i])
			if(val[i]==y)
				return sav[i];
		insert(y);
		return sav[siz];
	}
	inline bool count(const P y){
		int x=y%mod;
		for(int i=head[x];i;i=nxt[i])
			if(val[i]==y)
				return 1;
		return 0;
	}
};
HashTable<int,int>mp;
int stk[N],top;
vector<pii>ans;
inline void add(int x,int y){
	ans.push_back(mpr(x,y));
}
inline void solve(vi vec){
	int mx=0,mxp=0;
	for(int i=0;i<vec.size();i++)
		if(v[vec[i]]>mx)
			mx=v[vec[i]],mxp=i;
	vi cir;
	for(int i=mxp,j=0;j<vec.size();i=(i+1)%vec.size(),j++)
		cir.push_back(vec[i]);
	top=0;
	stk[++top]=cir[0];
	for(int i=1;i<cir.size();i++){
		int tmp=top;
		while(top&&v[stk[top]]<v[cir[i]]) top--;
		if(top<tmp){
			add(stk[top],stk[tmp]);
			for(int j=top+1;j<tmp;j++)
				add(stk[j],stk[tmp]);
		}
		stk[++top]=cir[i];
	}
	for(int i=1;i<top;i++)
		add(stk[i],stk[top]);
}
int main(){
	//freopen("swap.in","r",stdin);
	//freopen("swap.out","w",stdout);
	T=read();
	while(T--){
		n=read(),c=read();
		ll s=0;
		mp.clear(),ans.clear();
		for(int i=1;i<=n;i++){
			p[i]=read(),q[i]=read();
			s+=abs(p[i]-q[i]);
			mp[q[i]]=i,v[i]=q[i],q[i]=i;
		}
		for(int i=1;i<=n;i++)
			p[i]=mp[p[i]],vis[i]=0;
		for(int i=1;i<=n;i++){
			if(vis[i]) continue;
			vector<int>cir;
			for(int j=i;!vis[j];j=p[j])
				cir.push_back(j),vis[j]=1;
			solve(cir);
		}
		write(s/2+1ll*ans.size()*c),putc(' '),write(ans.size()),putc('\n');
		for(auto tmp:ans)
			write(tmp.fir),putc(' '),write(tmp.sec),putc('\n');
	}
	flush();
}

详细

Test #1:

score: 100
Accepted
time: 103ms
memory: 26144kb

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

result:

ok ac (1000 test cases)

Test #2:

score: 0
Accepted
time: 127ms
memory: 28524kb

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

result:

ok ac (5 test cases)

Test #3:

score: 0
Accepted
time: 123ms
memory: 29868kb

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