Submission #1163218

#TimeUsernameProblemLanguageResultExecution timeMemory
1163218hainam2k9Growing Vegetable is Fun 3 (JOI19_ho_t3)C++20
0 / 100
2 ms4200 KiB
#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 1e5+5;
const string NAME = "";
int n,dp[65][65][65][3],rs=1e9,cost[405][405];
char c[405];
vector<int> pos[3];
int main()
{
    tt;
    if(fopen((NAME + ".INP").c_str(), "r")) fo;
    cin >> n;
    for(int i = 1; i<=n; ++i){
        cin >> c[i];
        if(c[i]=='R') pos[0].pb(i);
        else if(c[i]=='G') pos[1].pb(i);
        else pos[2].pb(i);
    }
    for(int i = 1; i<=n; ++i){
        int cnt=0;
        for(int j = 1; j<i; ++j)
            cnt+=c[i]!=c[j];
        for(int j = i+1; j<=n; ++j)
            if(c[i]!=c[j]) ++cnt, cost[i][cnt]=cost[i][cnt-1]+1;
    }
    memset(dp,0x3f,sizeof(dp));
    if(!pos[0].empty()) dp[1][1][0][0]=pos[0][0]-1;
    if(!pos[1].empty()) dp[1][0][1][1]=pos[1][0]-1;
    if(!pos[2].empty()) dp[1][0][0][2]=pos[2][0]-1;
    for(int i = 2; i<=n; ++i)
        for(int j = 0; j<=min(i,sz(pos[0])); ++j)
            for(int k = 0; k<=min(i-j,sz(pos[1])); ++k){
                if(i-j-k>sz(pos[2])) continue;
                if(j>0) dp[i][j][k][0]=min(dp[i-1][j-1][k][1],dp[i-1][j-1][k][2])+abs(i-pos[0][j-1]-cost[pos[0][j-1]][i-j]);
                if(k>0) dp[i][j][k][1]=min(dp[i-1][j][k-1][0],dp[i-1][j][k-1][2])+abs(i-pos[1][k-1]-cost[pos[1][k-1]][i-k]);
                if(i-j-k>0) dp[i][j][k][2]=min(dp[i-1][j][k][0],dp[i-1][j][k][1])+abs(i-pos[2][i-j-k-1]-cost[pos[2][i-j-k-1]][j+k]);
            }
    for(int i=sz(pos[0]), j=sz(pos[1]), last=0; last<3; ++last)
        rs=min(rs,dp[n][i][j][last]);
    if(rs==1e9) rs=-1;
    cout << rs;
}

Compilation message (stderr)

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:28:45: note: in expansion of macro 'fo'
   28 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
joi2019_ho_t3.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
      |                                                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:28:45: note: in expansion of macro 'fo'
   28 |     if(fopen((NAME + ".INP").c_str(), "r")) fo;
      |                                             ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...