Submission #559901

# Submission time Handle Problem Language Result Execution time Memory
559901 2022-05-10T23:51:21 Z Huy Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
75 / 100
500 ms 755604 KB
#include<bits/stdc++.h>
//#define int long long
#define pii pair<int,int>
#define fi first
#define se second
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ldb = long double;
const int N = (int)5e5;
const int maxN = (int)5e5 + 5;
const ll mod = 1e9 + 7;
//const int mod = 998244353;
const ll infty = 1e17;
const ll logn = 18;
const int base = 311;
const int Block_size = 500;
const int ep = 'a';

int cu[] = {0,0,1,-1};
int cv[] = {-1,1,0,0};

int du[] = {-1,-1,+1,1};
int dv[] = {-1,+1,-1,1};

int cled[] = {6,2,5,5,4,5,6,3,7,6};

void InputFile()
{
    freopen(".inp","r",stdin);
    freopen(".out","w",stdout);
    //freopen("test.out","r",stdin);
}

void FastInput()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
}

int n;
int dpr[401][401][401];
int dpg[401][401][401];
int dpy[401][401][401];
vector<int> save_r,save_g,save_y;
string s;

int Cal_R(int cr,int cg,int cy)
{
    int now_pos = save_r[cr-1];
    int moved = 0;
    int k = upper_bound(save_g.begin(),save_g.end(),now_pos) - save_g.begin();
    moved += max(0,cg - k);
    k = upper_bound(save_y.begin(),save_y.end(),now_pos) - save_y.begin();
    moved += max(0,cy - k);
    return now_pos + moved - (cr + cg + cy);
}

int Cal_G(int cr,int cg,int cy)
{
    int now_pos = save_g[cg-1];
    int moved = 0;
    int k = upper_bound(save_r.begin(),save_r.end(),now_pos) - save_r.begin();
    moved += max(0,cr - k);
    k = upper_bound(save_y.begin(),save_y.end(),now_pos) - save_y.begin();
    moved += max(0,cy - k);
    return now_pos + moved - (cr + cg + cy);
}

int Cal_Y(int cr,int cg,int cy)
{
    int now_pos = save_y[cy-1];
    int moved = 0;
    int k = upper_bound(save_r.begin(),save_r.end(),now_pos) - save_r.begin();
    moved += max(0,cr - k);
    k = upper_bound(save_g.begin(),save_g.end(),now_pos) - save_g.begin();
    moved += max(0,cg - k);
    return now_pos + moved - (cr + cg + cy);
}

void Read()
{
   cin >> n >> s;
   s.insert(0,"?");
   //save_r.push_back(mod);
   //save_g.push_back(mod);
   //save_y.push_back(mod);
   for(int i = 1;i <= n;i++)
   {
       if(s[i] == 'R') save_r.push_back(i);
       if(s[i] == 'G') save_g.push_back(i);
       if(s[i] == 'Y') save_y.push_back(i);
   }
   /// init
   for(int i = 1;i <= n;i++)
   {
       for(int cr = 0;cr <= n;cr++)
       {
           for(int cg = 0;cg <= n;cg++)
           {
               dpr[i][cr][cg] = mod;
               dpg[i][cr][cg] = mod;
               dpy[i][cr][cg] = mod;
           }
       }
   }
   dpr[0][0][0] = dpg[0][0][0] = dpy[0][0][0] = 0;

   for(int i = 1;i <= n;i++)
   {
       for(int cr = 0;cr <= (int)save_r.size();cr++)
       {
           for(int cg = 0;cg <= (int)save_g.size();cg++)
           {
               int cy = i - cr - cg;
               if(cy > save_y.size()) continue;
               if(cr + cg > i) break;
               if(cr > 0)
               {
                   dpr[i][cr][cg] = min(dpg[i-1][cr-1][cg],dpy[i-1][cr-1][cg]) + Cal_R(cr,cg,cy);
               }
               if(cg > 0)
               {
                   dpg[i][cr][cg] = min(dpr[i-1][cr][cg-1],dpy[i-1][cr][cg-1]) + Cal_G(cr,cg,cy);
               }
               if(cy > 0)
               {
                   dpy[i][cr][cg] = min(dpr[i-1][cr][cg],dpg[i-1][cr][cg]) + Cal_Y(cr,cg,cy);
               }
           }
       }
   }
   int res;
   res = min(min(dpr[n][(int)save_r.size()][(int)save_g.size()],dpg[n][(int)save_r.size()][(int)save_g.size()]),dpy[n][(int)save_r.size()][(int)save_g.size()]);
   if(res >= (int)1e9) res =-1;
   cout << res;
}

void Solve()
{

}

void Debug()
{
    //Main_sub();
    //Naive();
}


int main()
{
    FastInput();
    //InputFile();
    //int sub_type;
    //cin >> sub_type;
    //Sieve();
    int test;
    //cin >> test;
    test = 1;
    while(test--)
        //for(int prc = 1; prc <= test; prc++)
    {
        Read();
        Solve();
        //Debug();
    }
}







Compilation message

joi2019_ho_t3.cpp:7: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    7 | #pragma GCC optimization ("O3")
      | 
joi2019_ho_t3.cpp:8: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    8 | #pragma GCC optimization ("unroll-loops")
      | 
joi2019_ho_t3.cpp: In function 'void Read()':
joi2019_ho_t3.cpp:121:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |                if(cy > save_y.size()) continue;
      |                   ~~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'void InputFile()':
joi2019_ho_t3.cpp:35:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |     freopen(".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |     freopen(".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 1620 KB Output is correct
6 Correct 1 ms 1608 KB Output is correct
7 Correct 1 ms 1620 KB Output is correct
8 Correct 1 ms 1608 KB Output is correct
9 Correct 1 ms 1620 KB Output is correct
10 Correct 1 ms 1620 KB Output is correct
11 Correct 2 ms 1620 KB Output is correct
12 Correct 1 ms 1620 KB Output is correct
13 Correct 1 ms 1620 KB Output is correct
14 Correct 1 ms 1492 KB Output is correct
15 Correct 1 ms 1620 KB Output is correct
16 Correct 2 ms 1620 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 1620 KB Output is correct
6 Correct 1 ms 1608 KB Output is correct
7 Correct 1 ms 1620 KB Output is correct
8 Correct 1 ms 1608 KB Output is correct
9 Correct 1 ms 1620 KB Output is correct
10 Correct 1 ms 1620 KB Output is correct
11 Correct 2 ms 1620 KB Output is correct
12 Correct 1 ms 1620 KB Output is correct
13 Correct 1 ms 1620 KB Output is correct
14 Correct 1 ms 1492 KB Output is correct
15 Correct 1 ms 1620 KB Output is correct
16 Correct 2 ms 1620 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 10 ms 18260 KB Output is correct
19 Correct 9 ms 18260 KB Output is correct
20 Correct 8 ms 18260 KB Output is correct
21 Correct 8 ms 18260 KB Output is correct
22 Correct 8 ms 18248 KB Output is correct
23 Correct 9 ms 18260 KB Output is correct
24 Correct 8 ms 18260 KB Output is correct
25 Correct 8 ms 18260 KB Output is correct
26 Correct 8 ms 18260 KB Output is correct
27 Correct 8 ms 18228 KB Output is correct
28 Correct 10 ms 18220 KB Output is correct
29 Correct 8 ms 18204 KB Output is correct
30 Correct 9 ms 18260 KB Output is correct
31 Correct 8 ms 18260 KB Output is correct
32 Correct 11 ms 18260 KB Output is correct
33 Correct 9 ms 17620 KB Output is correct
34 Correct 8 ms 17620 KB Output is correct
35 Correct 9 ms 16980 KB Output is correct
36 Correct 8 ms 17620 KB Output is correct
37 Correct 7 ms 16456 KB Output is correct
38 Correct 8 ms 18260 KB Output is correct
39 Correct 9 ms 18260 KB Output is correct
40 Correct 7 ms 17620 KB Output is correct
41 Correct 8 ms 18260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 310 ms 755532 KB Output is correct
3 Correct 342 ms 753624 KB Output is correct
4 Correct 314 ms 755568 KB Output is correct
5 Correct 332 ms 755532 KB Output is correct
6 Correct 318 ms 755532 KB Output is correct
7 Correct 314 ms 753664 KB Output is correct
8 Correct 325 ms 753656 KB Output is correct
9 Correct 314 ms 751872 KB Output is correct
10 Correct 326 ms 755484 KB Output is correct
11 Correct 320 ms 755496 KB Output is correct
12 Correct 79 ms 203436 KB Output is correct
13 Correct 155 ms 358476 KB Output is correct
14 Correct 214 ms 518624 KB Output is correct
15 Correct 316 ms 755604 KB Output is correct
16 Correct 327 ms 755440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 1620 KB Output is correct
6 Correct 1 ms 1608 KB Output is correct
7 Correct 1 ms 1620 KB Output is correct
8 Correct 1 ms 1608 KB Output is correct
9 Correct 1 ms 1620 KB Output is correct
10 Correct 1 ms 1620 KB Output is correct
11 Correct 2 ms 1620 KB Output is correct
12 Correct 1 ms 1620 KB Output is correct
13 Correct 1 ms 1620 KB Output is correct
14 Correct 1 ms 1492 KB Output is correct
15 Correct 1 ms 1620 KB Output is correct
16 Correct 2 ms 1620 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 10 ms 18260 KB Output is correct
19 Correct 9 ms 18260 KB Output is correct
20 Correct 8 ms 18260 KB Output is correct
21 Correct 8 ms 18260 KB Output is correct
22 Correct 8 ms 18248 KB Output is correct
23 Correct 9 ms 18260 KB Output is correct
24 Correct 8 ms 18260 KB Output is correct
25 Correct 8 ms 18260 KB Output is correct
26 Correct 8 ms 18260 KB Output is correct
27 Correct 8 ms 18228 KB Output is correct
28 Correct 10 ms 18220 KB Output is correct
29 Correct 8 ms 18204 KB Output is correct
30 Correct 9 ms 18260 KB Output is correct
31 Correct 8 ms 18260 KB Output is correct
32 Correct 11 ms 18260 KB Output is correct
33 Correct 9 ms 17620 KB Output is correct
34 Correct 8 ms 17620 KB Output is correct
35 Correct 9 ms 16980 KB Output is correct
36 Correct 8 ms 17620 KB Output is correct
37 Correct 7 ms 16456 KB Output is correct
38 Correct 8 ms 18260 KB Output is correct
39 Correct 9 ms 18260 KB Output is correct
40 Correct 7 ms 17620 KB Output is correct
41 Correct 8 ms 18260 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 310 ms 755532 KB Output is correct
44 Correct 342 ms 753624 KB Output is correct
45 Correct 314 ms 755568 KB Output is correct
46 Correct 332 ms 755532 KB Output is correct
47 Correct 318 ms 755532 KB Output is correct
48 Correct 314 ms 753664 KB Output is correct
49 Correct 325 ms 753656 KB Output is correct
50 Correct 314 ms 751872 KB Output is correct
51 Correct 326 ms 755484 KB Output is correct
52 Correct 320 ms 755496 KB Output is correct
53 Correct 79 ms 203436 KB Output is correct
54 Correct 155 ms 358476 KB Output is correct
55 Correct 214 ms 518624 KB Output is correct
56 Correct 316 ms 755604 KB Output is correct
57 Correct 327 ms 755440 KB Output is correct
58 Execution timed out 595 ms 755532 KB Time limit exceeded
59 Halted 0 ms 0 KB -