이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=1e5+50;
const int mod=1e9+7;
using namespace std;
int n,i,nr[3],f[405][405][405][3],r,g,y,R,G,Y,s;
vector<pair<int,pair<int,int> > >v[3];
char c;
int get(char c)
{
if(c=='R')return 0;
if(c=='G')return 1;
return 2;
}
int cst(pair<int,pair<int,int> >x,pair<int,pair<int,int> >y)
{
return abs(x.fr-y.fr)+abs(x.sc.fr-y.sc.fr)+abs(x.sc.sc-y.sc.sc);
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n;
for(i=1;i<=n;i++)
{
cin>>c;
nr[get(c)]++;
v[get(c)].pb({nr[0],{nr[1],nr[2]}});
}
R=v[0].size(),G=v[1].size(),Y=v[2].size();
if(max(max(R,G),Y)>(n+1)/2)rc(-1);
for(r=0;r<=R;r++)
{
for(g=0;g<=G;g++)
{
for(y=0;y<=Y;y++)
{
s=r+g+y;
if(!s)continue;
f[r][g][y][0]=f[r][g][y][1]=f[r][g][y][2]=inf;
if(r)
{
f[r][g][y][0]=min(f[r-1][g][y][1],f[r-1][g][y][2])+cst(v[0][r-1],{r,{g,y}});
}
if(g)
{
f[r][g][y][1]=min(f[r][g-1][y][0],f[r][g-1][y][2])+cst(v[1][g-1],{r,{g,y}});
}
if(y)
{
f[r][g][y][2]=min(f[r][g][y-1][0],f[r][g][y-1][1])+cst(v[2][y-1],{r,{g,y}});
}
}
}
}
cout<<min(min(f[R][G][Y][0],f[R][G][Y][1]),f[R][G][Y][2])/2<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |