Submission #559900

# Submission time Handle Problem Language Result Execution time Memory
559900 2022-05-10T23:49:18 Z Huy Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
75 / 100
500 ms 755648 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);
               }
           }
       }
   }
   ll 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 968 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 1608 KB Output is correct
8 Correct 1 ms 1620 KB Output is correct
9 Correct 1 ms 1604 KB Output is correct
10 Correct 1 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 1480 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 1484 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 968 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 1608 KB Output is correct
8 Correct 1 ms 1620 KB Output is correct
9 Correct 1 ms 1604 KB Output is correct
10 Correct 1 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 1480 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 1484 KB Output is correct
18 Correct 8 ms 18248 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 11 ms 18248 KB Output is correct
23 Correct 9 ms 18168 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 9 ms 18260 KB Output is correct
28 Correct 8 ms 18260 KB Output is correct
29 Correct 8 ms 18260 KB Output is correct
30 Correct 8 ms 18260 KB Output is correct
31 Correct 8 ms 18260 KB Output is correct
32 Correct 8 ms 18260 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 16980 KB Output is correct
36 Correct 7 ms 17620 KB Output is correct
37 Correct 8 ms 16460 KB Output is correct
38 Correct 10 ms 18248 KB Output is correct
39 Correct 8 ms 18260 KB Output is correct
40 Correct 8 ms 17620 KB Output is correct
41 Correct 9 ms 18248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 448 ms 755548 KB Output is correct
3 Correct 387 ms 753572 KB Output is correct
4 Correct 363 ms 755564 KB Output is correct
5 Correct 379 ms 755560 KB Output is correct
6 Correct 330 ms 755612 KB Output is correct
7 Correct 333 ms 753620 KB Output is correct
8 Correct 309 ms 753660 KB Output is correct
9 Correct 328 ms 751824 KB Output is correct
10 Correct 311 ms 755564 KB Output is correct
11 Correct 318 ms 755428 KB Output is correct
12 Correct 86 ms 203380 KB Output is correct
13 Correct 153 ms 358348 KB Output is correct
14 Correct 210 ms 518552 KB Output is correct
15 Correct 312 ms 755648 KB Output is correct
16 Correct 324 ms 755508 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 968 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 1608 KB Output is correct
8 Correct 1 ms 1620 KB Output is correct
9 Correct 1 ms 1604 KB Output is correct
10 Correct 1 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 1480 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 1484 KB Output is correct
18 Correct 8 ms 18248 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 11 ms 18248 KB Output is correct
23 Correct 9 ms 18168 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 9 ms 18260 KB Output is correct
28 Correct 8 ms 18260 KB Output is correct
29 Correct 8 ms 18260 KB Output is correct
30 Correct 8 ms 18260 KB Output is correct
31 Correct 8 ms 18260 KB Output is correct
32 Correct 8 ms 18260 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 16980 KB Output is correct
36 Correct 7 ms 17620 KB Output is correct
37 Correct 8 ms 16460 KB Output is correct
38 Correct 10 ms 18248 KB Output is correct
39 Correct 8 ms 18260 KB Output is correct
40 Correct 8 ms 17620 KB Output is correct
41 Correct 9 ms 18248 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 448 ms 755548 KB Output is correct
44 Correct 387 ms 753572 KB Output is correct
45 Correct 363 ms 755564 KB Output is correct
46 Correct 379 ms 755560 KB Output is correct
47 Correct 330 ms 755612 KB Output is correct
48 Correct 333 ms 753620 KB Output is correct
49 Correct 309 ms 753660 KB Output is correct
50 Correct 328 ms 751824 KB Output is correct
51 Correct 311 ms 755564 KB Output is correct
52 Correct 318 ms 755428 KB Output is correct
53 Correct 86 ms 203380 KB Output is correct
54 Correct 153 ms 358348 KB Output is correct
55 Correct 210 ms 518552 KB Output is correct
56 Correct 312 ms 755648 KB Output is correct
57 Correct 324 ms 755508 KB Output is correct
58 Execution timed out 562 ms 755580 KB Time limit exceeded
59 Halted 0 ms 0 KB -