/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Time Exceeded ≥1100ms ≥596.0 KiB
#3 Time Exceeded ≥1100ms ≥584.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;
const long long M=2e5+10,MOD=998244353;
typedef long long ll;
#define debug(x) cout<<x<<endl

vector<string>all;
vector<int>prime;
vector<int>pp(3000,0);
void precal(){
	string a="abc";
	all.push_back(a);
	while(next_permutation(a.begin(),a.end())){
		all.push_back(a);
	}
	prime.push_back(0);
	pp[0]=1;
	for(int i=2;i<=2000;i++){
		int x=1;
		for(int j=2;j*j<=i;j++){
			if(i%j==0){
				x=0;
			}
		}
		if(x)prime.push_back(i);
		if(x)pp[i]=1;
	}
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
    cin>>t;
    precal();
    while(t--){
    	int n;
    	cin>>n;
    	string s;
    	cin>>s;
    	vector<int>fre(3,0);
    	for(auto i:s){
    		fre[i-'a']++;
    	}
    	int mx=MOD;
    	for(auto it:all){
    		string p=it;
    		for(auto start:prime){
    			if(start>n)break;
    			for(auto end:prime){
    				if(start+end>n)break;
    				for(auto L:prime){
    				if(start+L+end>n)break;
    				if(start+L+end!=n)continue;
    				int baki=L;
    				int a=fre[p[0]-'a'];
    				int b=fre[p[1]-'a'];
    				int c=fre[p[2]-'a'];
    				int pos=0,neg=0;
    				if(a>start)pos+=a-start;
    				if(a<start)neg+=start-a;
    				if(b>end)pos+=b-end;
    				if(b<end)neg+=end-b;
    				if(c>baki)pos+=c-baki;
    				if(c<baki)neg+=baki-c;
    				mx=min(mx,min(pos,neg));
    				}
    				
    			}
    		}
    	}
    	if(mx==MOD)mx=-1;
    	cout<<mx<<"\n";
    	
    	
    	
    	}
    	
    	
    	
    	
      
    return 0;
 
}

Information

Submit By
Type
Submission
Problem
P1158 Yet another Beautiful String
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 08:26:10
Judged At
2025-01-02 08:26:10
Judged By
Score
0
Total Time
≥1100ms
Peak Memory
≥596.0 KiB