Submission #559902

# Submission time Handle Problem Language Result Execution time Memory
559902 2022-05-10T23:51:50 Z Huy Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
75 / 100
500 ms 755544 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 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 1 ms 1620 KB Output is correct
6 Correct 1 ms 1620 KB Output is correct
7 Correct 1 ms 1620 KB Output is correct
8 Correct 2 ms 1620 KB Output is correct
9 Correct 2 ms 1620 KB Output is correct
10 Correct 2 ms 1620 KB Output is correct
11 Correct 1 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 1 ms 1620 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 1 ms 1620 KB Output is correct
6 Correct 1 ms 1620 KB Output is correct
7 Correct 1 ms 1620 KB Output is correct
8 Correct 2 ms 1620 KB Output is correct
9 Correct 2 ms 1620 KB Output is correct
10 Correct 2 ms 1620 KB Output is correct
11 Correct 1 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 1 ms 1620 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 8 ms 18264 KB Output is correct
19 Correct 9 ms 18260 KB Output is correct
20 Correct 9 ms 18260 KB Output is correct
21 Correct 9 ms 18260 KB Output is correct
22 Correct 8 ms 18260 KB Output is correct
23 Correct 9 ms 18260 KB Output is correct
24 Correct 9 ms 18260 KB Output is correct
25 Correct 10 ms 18132 KB Output is correct
26 Correct 11 ms 18260 KB Output is correct
27 Correct 9 ms 18136 KB Output is correct
28 Correct 10 ms 18260 KB Output is correct
29 Correct 9 ms 18260 KB Output is correct
30 Correct 9 ms 18228 KB Output is correct
31 Correct 9 ms 18260 KB Output is correct
32 Correct 10 ms 18220 KB Output is correct
33 Correct 7 ms 17620 KB Output is correct
34 Correct 8 ms 17620 KB Output is correct
35 Correct 8 ms 17004 KB Output is correct
36 Correct 7 ms 17620 KB Output is correct
37 Correct 8 ms 16468 KB Output is correct
38 Correct 9 ms 18388 KB Output is correct
39 Correct 8 ms 18168 KB Output is correct
40 Correct 8 ms 17620 KB Output is correct
41 Correct 8 ms 18260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 329 ms 755504 KB Output is correct
3 Correct 316 ms 753712 KB Output is correct
4 Correct 324 ms 755432 KB Output is correct
5 Correct 322 ms 755440 KB Output is correct
6 Correct 318 ms 755444 KB Output is correct
7 Correct 340 ms 753656 KB Output is correct
8 Correct 318 ms 753548 KB Output is correct
9 Correct 332 ms 751676 KB Output is correct
10 Correct 311 ms 755540 KB Output is correct
11 Correct 317 ms 755496 KB Output is correct
12 Correct 80 ms 203496 KB Output is correct
13 Correct 177 ms 358468 KB Output is correct
14 Correct 222 ms 518608 KB Output is correct
15 Correct 318 ms 755456 KB Output is correct
16 Correct 330 ms 755504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 852 KB Output is correct
5 Correct 1 ms 1620 KB Output is correct
6 Correct 1 ms 1620 KB Output is correct
7 Correct 1 ms 1620 KB Output is correct
8 Correct 2 ms 1620 KB Output is correct
9 Correct 2 ms 1620 KB Output is correct
10 Correct 2 ms 1620 KB Output is correct
11 Correct 1 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 1 ms 1620 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 8 ms 18264 KB Output is correct
19 Correct 9 ms 18260 KB Output is correct
20 Correct 9 ms 18260 KB Output is correct
21 Correct 9 ms 18260 KB Output is correct
22 Correct 8 ms 18260 KB Output is correct
23 Correct 9 ms 18260 KB Output is correct
24 Correct 9 ms 18260 KB Output is correct
25 Correct 10 ms 18132 KB Output is correct
26 Correct 11 ms 18260 KB Output is correct
27 Correct 9 ms 18136 KB Output is correct
28 Correct 10 ms 18260 KB Output is correct
29 Correct 9 ms 18260 KB Output is correct
30 Correct 9 ms 18228 KB Output is correct
31 Correct 9 ms 18260 KB Output is correct
32 Correct 10 ms 18220 KB Output is correct
33 Correct 7 ms 17620 KB Output is correct
34 Correct 8 ms 17620 KB Output is correct
35 Correct 8 ms 17004 KB Output is correct
36 Correct 7 ms 17620 KB Output is correct
37 Correct 8 ms 16468 KB Output is correct
38 Correct 9 ms 18388 KB Output is correct
39 Correct 8 ms 18168 KB Output is correct
40 Correct 8 ms 17620 KB Output is correct
41 Correct 8 ms 18260 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 329 ms 755504 KB Output is correct
44 Correct 316 ms 753712 KB Output is correct
45 Correct 324 ms 755432 KB Output is correct
46 Correct 322 ms 755440 KB Output is correct
47 Correct 318 ms 755444 KB Output is correct
48 Correct 340 ms 753656 KB Output is correct
49 Correct 318 ms 753548 KB Output is correct
50 Correct 332 ms 751676 KB Output is correct
51 Correct 311 ms 755540 KB Output is correct
52 Correct 317 ms 755496 KB Output is correct
53 Correct 80 ms 203496 KB Output is correct
54 Correct 177 ms 358468 KB Output is correct
55 Correct 222 ms 518608 KB Output is correct
56 Correct 318 ms 755456 KB Output is correct
57 Correct 330 ms 755504 KB Output is correct
58 Execution timed out 598 ms 755544 KB Time limit exceeded
59 Halted 0 ms 0 KB -