Submission #57785244


Source Code Expand

#include<stdio.h>
#include<vector>
using namespace std;
const int N=200005;

#define ll __int128

ll exgcd(ll a,ll b,ll &x,ll &y){
	if(b==0){
		x=1,y=0;
		return a;
	}
	
	ll d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}

ll gcd(ll x,ll y){
	if(y) return gcd(y,x%y);
	else return x;
}

struct equ{
	ll r,m;
};

equ merge(equ X,equ Y){
	if(!X.m) return Y;
	if(!Y.m) return X;
	
	ll a=X.m,b=Y.m,c=Y.r-X.r,x,y;
	ll d=exgcd(a,b,x,y);
	
	if(c%d) return (equ){0,0};
	
	a/=d,b/=d,c/=d;
	x=((x*c)%b+b)%b;
	
	Y.m=X.m/gcd(X.m,Y.m)*Y.m;
	Y.r=((x*X.m%Y.m+X.r)%Y.m+Y.m)%Y.m;
	return Y;
}

int n;
int p[N],a[N],b[N];
vector<int> e[N],g;
equ now,tmp;
bool vis[N];

void dfs(int x){
	g.push_back(x);
	vis[x]=true;
	
	for(int v:e[x])
	if(!vis[v]) dfs(v);
}

signed main(){
	int i,j,k;
	scanf("%d",&n);
	
	for(i=1;i<=n;i++){
		scanf("%d",&p[i]);
		e[i].push_back(p[i]);
	}for(i=1;i<=n;i++)
		scanf("%d",&a[i]);
	
	for(i=1;i<=n;i++) if(!vis[i]){
		g.clear();
		dfs(i);
		k=-1;
		
		for(j=0;j<g.size();j++)
		if(merge(now,(equ){(ll)j,(ll)g.size()}).m
		&&(k<0||a[g[j]]<a[g[k]])) k=j;
		
		if(k<0) return 114;
		
		now=merge(now,(equ){(ll)k,(ll)g.size()});
		
		for(j=0;j<g.size();j++)
			b[g[j]]=a[g[(j+k)%g.size()]];
	}
	
	for(i=1;i<=n;i++)
		printf("%d ",b[i]);
	
	puts("");
	return 0;
}

Submission Info

Submission Time
Task G - Lexicographically Smallest Permutation
User chrhaaeon
Language C++ 20 (gcc 12.2)
Score 0
Code Size 1356 Byte
Status WA
Exec Time 138 ms
Memory 32760 KiB

Compile Error

Main.cpp: In function ‘int main()’:
Main.cpp:74:26: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   74 |                 for(j=0;j<g.size();j++)
      |                         ~^~~~~~~~~
Main.cpp:82:26: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector<int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
   82 |                 for(j=0;j<g.size();j++)
      |                         ~^~~~~~~~~
Main.cpp:61:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   61 |         scanf("%d",&n);
      |         ~~~~~^~~~~~~~~
Main.cpp:64:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   64 |                 scanf("%d",&p[i]);
      |                 ~~~~~^~~~~~~~~~~~
Main.cpp:67:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   67 |                 scanf("%d",&a[i]);
      |                 ~~~~~^~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 575
Status
AC × 3
AC × 72
WA × 19
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt, 01_random_55.txt, 01_random_56.txt, 01_random_57.txt, 01_random_58.txt, 01_random_59.txt, 01_random_60.txt, 01_random_61.txt, 01_random_62.txt, 01_random_63.txt, 01_random_64.txt, 01_random_65.txt, 01_random_66.txt, 01_random_67.txt, 01_random_68.txt, 01_random_69.txt, 01_random_70.txt, 01_random_71.txt, 01_random_72.txt, 01_random_73.txt, 01_random_74.txt, 01_random_75.txt, 01_random_76.txt, 01_random_77.txt, 01_random_78.txt, 01_random_79.txt, 02_small_80.txt, 02_small_81.txt, 02_small_82.txt, 02_small_83.txt, 02_small_84.txt, 02_small_85.txt, 02_small_86.txt, 02_small_87.txt, 02_small_88.txt, 02_small_89.txt, 02_small_90.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 2 ms 3204 KiB
00_sample_01.txt AC 2 ms 3104 KiB
00_sample_02.txt AC 2 ms 3128 KiB
01_random_03.txt AC 124 ms 23400 KiB
01_random_04.txt AC 109 ms 30036 KiB
01_random_05.txt AC 112 ms 26852 KiB
01_random_06.txt AC 107 ms 25256 KiB
01_random_07.txt AC 103 ms 29120 KiB
01_random_08.txt AC 100 ms 32760 KiB
01_random_09.txt AC 115 ms 26924 KiB
01_random_10.txt AC 116 ms 24616 KiB
01_random_11.txt AC 38 ms 10248 KiB
01_random_12.txt AC 64 ms 15684 KiB
01_random_13.txt AC 28 ms 10780 KiB
01_random_14.txt AC 3 ms 3512 KiB
01_random_15.txt AC 80 ms 23688 KiB
01_random_16.txt AC 50 ms 14976 KiB
01_random_17.txt AC 133 ms 16844 KiB
01_random_18.txt WA 133 ms 16880 KiB
01_random_19.txt AC 133 ms 16688 KiB
01_random_20.txt AC 129 ms 16836 KiB
01_random_21.txt AC 138 ms 17012 KiB
01_random_22.txt AC 137 ms 17228 KiB
01_random_23.txt AC 126 ms 17976 KiB
01_random_24.txt AC 94 ms 17980 KiB
01_random_25.txt AC 114 ms 23732 KiB
01_random_26.txt AC 100 ms 21584 KiB
01_random_27.txt AC 21 ms 6760 KiB
01_random_28.txt AC 19 ms 6340 KiB
01_random_29.txt WA 53 ms 11472 KiB
01_random_30.txt WA 76 ms 13804 KiB
01_random_31.txt WA 107 ms 18624 KiB
01_random_32.txt AC 103 ms 19084 KiB
01_random_33.txt AC 72 ms 26392 KiB
01_random_34.txt AC 75 ms 27320 KiB
01_random_35.txt AC 105 ms 23764 KiB
01_random_36.txt AC 104 ms 24732 KiB
01_random_37.txt AC 125 ms 25344 KiB
01_random_38.txt AC 126 ms 29044 KiB
01_random_39.txt AC 113 ms 23844 KiB
01_random_40.txt AC 105 ms 26680 KiB
01_random_41.txt AC 101 ms 28352 KiB
01_random_42.txt AC 106 ms 27076 KiB
01_random_43.txt AC 96 ms 31728 KiB
01_random_44.txt WA 127 ms 16820 KiB
01_random_45.txt WA 118 ms 16916 KiB
01_random_46.txt WA 123 ms 16720 KiB
01_random_47.txt AC 121 ms 16964 KiB
01_random_48.txt AC 122 ms 16864 KiB
01_random_49.txt AC 133 ms 17248 KiB
01_random_50.txt AC 130 ms 18276 KiB
01_random_51.txt AC 102 ms 19252 KiB
01_random_52.txt AC 61 ms 17328 KiB
01_random_53.txt AC 109 ms 24324 KiB
01_random_54.txt AC 20 ms 6820 KiB
01_random_55.txt WA 49 ms 10196 KiB
01_random_56.txt WA 82 ms 13228 KiB
01_random_57.txt AC 95 ms 18288 KiB
01_random_58.txt AC 106 ms 21152 KiB
01_random_59.txt AC 3 ms 3680 KiB
01_random_60.txt WA 80 ms 16640 KiB
01_random_61.txt AC 79 ms 16864 KiB
01_random_62.txt WA 80 ms 16976 KiB
01_random_63.txt WA 80 ms 16696 KiB
01_random_64.txt WA 81 ms 17016 KiB
01_random_65.txt AC 77 ms 16952 KiB
01_random_66.txt AC 80 ms 17652 KiB
01_random_67.txt AC 70 ms 19464 KiB
01_random_68.txt AC 56 ms 20564 KiB
01_random_69.txt AC 39 ms 23548 KiB
01_random_70.txt WA 125 ms 16776 KiB
01_random_71.txt WA 85 ms 16796 KiB
01_random_72.txt WA 124 ms 16944 KiB
01_random_73.txt WA 79 ms 16640 KiB
01_random_74.txt WA 79 ms 16712 KiB
01_random_75.txt AC 124 ms 16772 KiB
01_random_76.txt AC 85 ms 16620 KiB
01_random_77.txt WA 132 ms 16620 KiB
01_random_78.txt AC 80 ms 16840 KiB
01_random_79.txt AC 79 ms 16812 KiB
02_small_80.txt AC 2 ms 3116 KiB
02_small_81.txt AC 2 ms 3300 KiB
02_small_82.txt AC 2 ms 3176 KiB
02_small_83.txt AC 1 ms 3308 KiB
02_small_84.txt AC 2 ms 3356 KiB
02_small_85.txt AC 1 ms 3312 KiB
02_small_86.txt AC 2 ms 3256 KiB
02_small_87.txt AC 1 ms 3100 KiB
02_small_88.txt AC 1 ms 3200 KiB
02_small_89.txt AC 2 ms 3312 KiB
02_small_90.txt AC 2 ms 3124 KiB