Submission #68605673


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=(a);i<(n);i++)
#define per(i,a,n) for (int i=(n)-1;i>=(a);i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}()); 
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=5100;
int dp[N][N];
int n,k,p[N],vis[N],ans;
VI cc[N];
int solve(VI a) {
	int m=SZ(a);
	rep(i,0,m) rep(j,0,m) dp[i][j]=0;
	rep(i,0,m) a[i]%=n;
	rep(i,0,m) cc[a[i]].pb(i);
	rep(len,0,m) rep(j,0,m) {
		int i=(j+m-len)%m;
		if (len==0) {
			dp[j][i]=0;
			continue;
		}
		dp[j][i]=dp[(j+m-1)%m][i];
		if (i<j) {
			for (auto c:cc[a[j]]) {
				if (c>=i&&c<j)
					dp[j][i]=max(dp[j][i],dp[c][i]+dp[j][c==m-1?0:c+1]+1);
			}
		} else {
			for (auto c:cc[a[j]]) {
				if (c<j||c>=i)
					dp[j][i]=max(dp[j][i],dp[c][i]+dp[j][c==m-1?0:c+1]+1);
			}			
		}
	}
	int ans=0;
	rep(i,0,m) rep(j,i+1,m) if (a[i]==a[j]) ans=max(ans,1+dp[(j+m-1)%m][i]
		+dp[(i+m-1)%m][j]);
	rep(i,0,m) cc[a[i]].clear();
	return ans;
}
int main() {
	scanf("%d%d",&n,&k);
	rep(i,1,n*k+1) scanf("%d",&p[i]);
	rep(i,1,n*k+1) if (!vis[i]) {
		VI pat;
		int u=i;
		while (!vis[u]) {
			pat.pb(u);
			vis[u]=1;
			u=p[u];
		}
		ans+=solve(pat);
	}
	printf("%d\n",ans);
}

Submission Info

Submission Time
Task B - Sort Permutation
User apiad
Language C++ 20 (gcc 12.2)
Score 800
Code Size 1708 Byte
Status AC
Exec Time 1596 ms
Memory 103700 KiB

Compile Error

Main.cpp: In function ‘int main()’:
Main.cpp:59:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   59 |         scanf("%d%d",&n,&k);
      |         ~~~~~^~~~~~~~~~~~~~
Main.cpp:60:29: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   60 |         rep(i,1,n*k+1) scanf("%d",&p[i]);
      |                        ~~~~~^~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 800 / 800
Status
AC × 3
AC × 53
Set Name Test Cases
Sample 01-sample-01.txt, 01-sample-02.txt, 01-sample-03.txt
All 01-sample-01.txt, 01-sample-02.txt, 01-sample-03.txt, 02-small-01.txt, 02-small-02.txt, 02-small-03.txt, 02-small-04.txt, 02-small-05.txt, 02-small-06.txt, 02-small-07.txt, 02-small-08.txt, 02-small-09.txt, 02-small-10.txt, 02-small-11.txt, 02-small-12.txt, 02-small-13.txt, 02-small-14.txt, 02-small-15.txt, 02-small-16.txt, 03-random-01.txt, 03-random-02.txt, 03-random-03.txt, 03-random-04.txt, 03-random-05.txt, 04-large-01.txt, 04-large-02.txt, 04-large-03.txt, 04-large-04.txt, 04-large-05.txt, 05-id-01.txt, 05-id-02.txt, 05-id-03.txt, 05-id-04.txt, 06-large-cycle-01.txt, 06-large-cycle-02.txt, 06-large-cycle-03.txt, 06-large-cycle-04.txt, 06-large-cycle-05.txt, 07-large-cycle-2-01.txt, 07-large-cycle-2-02.txt, 07-large-cycle-2-03.txt, 07-large-cycle-2-04.txt, 07-large-cycle-2-05.txt, 08-divide-01.txt, 08-divide-02.txt, 08-divide-03.txt, 08-divide-04.txt, 08-divide-05.txt, 09-large-ans-01.txt, 09-large-ans-02.txt, 09-large-ans-03.txt, 09-large-ans-04.txt, 09-large-ans-05.txt
Case Name Status Exec Time Memory
01-sample-01.txt AC 1 ms 3988 KiB
01-sample-02.txt AC 1 ms 3612 KiB
01-sample-03.txt AC 1 ms 3936 KiB
02-small-01.txt AC 1 ms 3784 KiB
02-small-02.txt AC 1 ms 3972 KiB
02-small-03.txt AC 1 ms 3860 KiB
02-small-04.txt AC 1 ms 3748 KiB
02-small-05.txt AC 1 ms 3824 KiB
02-small-06.txt AC 1 ms 3732 KiB
02-small-07.txt AC 1 ms 3876 KiB
02-small-08.txt AC 1 ms 3908 KiB
02-small-09.txt AC 1 ms 3884 KiB
02-small-10.txt AC 1 ms 3896 KiB
02-small-11.txt AC 1 ms 3808 KiB
02-small-12.txt AC 1 ms 3968 KiB
02-small-13.txt AC 1 ms 3868 KiB
02-small-14.txt AC 1 ms 3808 KiB
02-small-15.txt AC 1 ms 3876 KiB
02-small-16.txt AC 1 ms 3768 KiB
03-random-01.txt AC 75 ms 23556 KiB
03-random-02.txt AC 189 ms 30256 KiB
03-random-03.txt AC 1 ms 4112 KiB
03-random-04.txt AC 4 ms 6320 KiB
03-random-05.txt AC 28 ms 13444 KiB
04-large-01.txt AC 379 ms 45984 KiB
04-large-02.txt AC 80 ms 21552 KiB
04-large-03.txt AC 197 ms 30036 KiB
04-large-04.txt AC 329 ms 48776 KiB
04-large-05.txt AC 194 ms 29624 KiB
05-id-01.txt AC 2 ms 3776 KiB
05-id-02.txt AC 1 ms 3704 KiB
05-id-03.txt AC 1 ms 3796 KiB
05-id-04.txt AC 2 ms 3924 KiB
06-large-cycle-01.txt AC 1596 ms 103700 KiB
06-large-cycle-02.txt AC 1152 ms 93520 KiB
06-large-cycle-03.txt AC 1468 ms 99852 KiB
06-large-cycle-04.txt AC 549 ms 64856 KiB
06-large-cycle-05.txt AC 1596 ms 102244 KiB
07-large-cycle-2-01.txt AC 205 ms 30172 KiB
07-large-cycle-2-02.txt AC 921 ms 85024 KiB
07-large-cycle-2-03.txt AC 1343 ms 95504 KiB
07-large-cycle-2-04.txt AC 93 ms 22336 KiB
07-large-cycle-2-05.txt AC 1026 ms 87596 KiB
08-divide-01.txt AC 2 ms 4232 KiB
08-divide-02.txt AC 7 ms 6004 KiB
08-divide-03.txt AC 2 ms 4308 KiB
08-divide-04.txt AC 3 ms 4132 KiB
08-divide-05.txt AC 2 ms 4304 KiB
09-large-ans-01.txt AC 316 ms 55876 KiB
09-large-ans-02.txt AC 95 ms 17216 KiB
09-large-ans-03.txt AC 458 ms 72988 KiB
09-large-ans-04.txt AC 144 ms 35764 KiB
09-large-ans-05.txt AC 86 ms 21668 KiB