QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#306325 | #5434. Binary Substrings | NemanjaSo2005 | WA | 6ms | 33952kb | C++14 | 4.0kb | 2024-01-16 17:20:56 | 2024-01-16 17:20:57 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define int long long
using namespace std;
int N,M,K;
const int maxn=(1<<20)+5;
vector<int> graf[maxn];
int perm[maxn],sleduje[maxn];
bool bio[maxn],uzmi[maxn];
priority_queue<pair<int,int>> PQ;
void getcycle(int x){
if(bio[x])
return;
int gde=x,s=0;
//cout<<"PRATIM "<<gde<<endl;
do {
s++;
bio[gde]=true;
// cout<<gde<<endl;
gde=perm[gde];
} while(gde!=x);
// cout<<"CIKLUS "<<s<<" "<<x<<endl;
PQ.push({s,x});
}
int count_substrings(string x) {
unordered_set<string> substrings;
// Iterate over all substrings
for (int i = 0; i < x.length(); ++i) {
string substring = "";
for (int j = i; j < x.length(); ++j) {
substring.push_back(x[j]);
substrings.insert(substring);
}
}
// Return the count of distinct substrings
return substrings.size();
}
void napravi(int k){
M=1<<(k-1);
for(int i=0;i<(1<<(k));i++){
int a=i>>1;
int b=i&((1<<(k-1))-1);
graf[a].push_back(b);
}
}
vector<int> V,R;
void poperm(int gde){
R.push_back(gde);
if(perm[gde]==0)
return;
int p=perm[gde];
perm[gde]=0;
poperm(p);
}
void euler(int gde){
while(graf[gde].size()){
int ko=graf[gde].back();
graf[gde].pop_back();
euler(ko);
}
V.push_back(gde);
}
void pisivektor(vector<int> V){
for(int i=0;i<V.size();i++)
cout<<V[i]<<" ";
cout<<endl;
}
signed main(){
cin>>N;
if(N==1){
cout<<"0\n";
return 0;
}
if(N==2){
cout<<"01\n";
return 0;
}
if(N==3){
cout<<"010\n";
return 0;
}
if(N==4){
cout<<"1001\n";
return 0;
}
for(int i=0;i<=20;i++){
if((1<<i)+i-1<=N)
K=i;
}
//cout<<"DUZINA JE "<<K<<endl;
napravi(K);
euler(0);
reverse(V.begin(),V.end());
string poc="";
if(((1<<(K))+K-1)!=N){
vector<int> B;
for(int i=1;i<V.size();i++)
B.push_back((V[i-1]<<1) + (V[i]&1));
/*
cout<<"NIZ B"<<endl;
pisivektor(B);*/
B.push_back(B[0]);
for(int i=0;i+1<B.size();i++)
perm[B[i]]=B[i+1]^1;
for(int i=0;i+1<B.size();i++)
sleduje[B[i]]=B[i+1];
/*
cout<<"PERM:"<<endl;
for(int i=0;i<B.size();i++)
cout<<perm[i]<<" ";
cout<<endl;
cout<<"SLEDUJE:"<<endl;
for(int i=0;i<B.size();i++)
cout<<sleduje[i]<<" ";
cout<<endl;*/
for(int i=0;i+1<B.size();i++)
getcycle(B[i]);
int dod=N-((1<<K)+K-1);
// cout<<"DOD JE "<<dod<<endl;
vector<pair<int,int>> odbac;
while(PQ.size()){
if(dod==0)
break;
if(PQ.top().first>dod){
odbac.push_back(PQ.top());
PQ.pop();
}
else{
dod-=PQ.top().first;
uzmi[PQ.top().second]=true;
PQ.pop();
}
}
int poc=0;
if(odbac.size() and dod!=0){
poc=sleduje[odbac[0].second];
uzmi[odbac[0].second]=true;
}
/* cout<<"POCETAK JE "<<poc<<endl;
cout<<"UZIMAM:"<<endl;
for(int i=0;i<B.size();i++)
cout<<uzmi[i]<<" ";
cout<<endl;*/
for(int i=(1<<K);i>=2;i>>=1)
R.push_back((i&poc)>=1);
for(int it=1;it<B.size();it++){
if(uzmi[poc])
poperm(poc);
else
R.push_back(poc);
poc=sleduje[poc];
}/*
cout<<"CEO R je "<<endl;
for(int i=0;i<R.size();i++)
cout<<R[i]<<" ";
cout<<endl;*/
}
else{
//cout<<"OVAKAV"<<endl;
for(int i=1;i<K;i++)
cout<<"0";
for(int i=1;i<V.size();i++)
cout<<(V[i]&1);
cout<<endl;
return 0;
}
string X;
for(int i=0;i<N;i++)
X.push_back('0'+(R[i]&1));
cout<<X<<"\n";
// cout<<count_substrings(X)<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 28352kb
input:
2
output:
01
result:
ok meet maximum 3
Test #2:
score: 0
Accepted
time: 3ms
memory: 28912kb
input:
5
output:
01100
result:
ok meet maximum 12
Test #3:
score: 0
Accepted
time: 6ms
memory: 28828kb
input:
1
output:
0
result:
ok meet maximum 1
Test #4:
score: 0
Accepted
time: 5ms
memory: 29556kb
input:
3
output:
010
result:
ok meet maximum 5
Test #5:
score: 0
Accepted
time: 2ms
memory: 29164kb
input:
4
output:
1001
result:
ok meet maximum 8
Test #6:
score: -100
Wrong Answer
time: 0ms
memory: 33952kb
input:
6
output:
000111
result:
wrong answer not meet maximum 15 < 16