Submission #1163227

#TimeUsernameProblemLanguageResultExecution timeMemory
1163227hainam2k9Growing Vegetable is Fun 3 (JOI19_ho_t3)C++20
100 / 100
113 ms200936 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,rs=1e9,cost[3][3][405][405];
char c;
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;
        if(c=='R') pos[0].pb(i);
        else if(c=='G') pos[1].pb(i);
        else pos[2].pb(i);
    }
    for(int i = 0; i<3; ++i)
        for(int j = 0; j<3; ++j){
            if(i==j) continue;
            for(int ptr1 = 1; ptr1<=sz(pos[i]); ++ptr1)
                for(int ptr2 = 1; ptr2<=sz(pos[j]); ++ptr2){
                    if(ptr2>0) cost[i][j][ptr1][ptr2]=cost[i][j][ptr1][ptr2-1];
                    cost[i][j][ptr1][ptr2]+=pos[i][ptr1-1]<pos[j][ptr2-1];
                }
        }
    int dp[n+5][sz(pos[0])+5][sz(pos[1])+5][3];
    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[0][1][j][k]-cost[0][2][j][i-j-k]);
                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[1][0][k][j]-cost[1][2][k][i-j-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[2][0][i-j-k][j]-cost[2][1][i-j-k][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...