Submission #816699

# Submission time Handle Problem Language Result Execution time Memory
816699 2023-08-09T06:17:21 Z vjudge1 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
100 / 100
167 ms 97804 KB
#include<bits/stdc++.h>
 
#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC optimize("unroll-loops")
 
using namespace std;
template<class A, class B>
ostream& operator<<(ostream& os, const pair<A, B> &p) {
  os << '(' << p.first << ',' << p.second << ')';
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &v) {
  os << '{';
  bool osu = 1;
  for(auto &i : v) { if(!osu) os << ','; os << i; osu = 0; }
  os << '}';
  return os;
}
template<class T>
ostream& operator<<(ostream& os, const deque<T> &v) {
  os << '{';
  bool osu = 1;
  for(auto &i : v) { if(!osu) os << ','; os << i; osu = 0; }
  os << '}';
  return os;
}
template<class T,long long unsigned int size>
ostream& operator<<(ostream& os, const array<T,size> &arr) {
  os << '{';
  bool osu = 1;
  for(auto &i : arr) { if(!osu) os << ','; os << i; osu = 0; }
  os << '}';
  return os;
}
 
#define ll long long
#define ull unsigned long long
#define lll __int128
#define rep(i,n,N) for(int i=(n);i<=(N);++i)
#define For(i,n,N) for(int i=(n);i< (N);++i)
#define rap(i,n,N) for(int i=(n);i>=(N);--i)
#define rip(i,n,N,V) for(int i=(n);i<=(N);i+=V)
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
//#define le left
//#define ri right
#define fi first
#define se second
#define ff fi.fi
#define fs fi.se
#define sf se.fi
#define ss se.se
#define lc (id<<1)
#define rc ((id<<1)|1)
#define debug(x) cout << #x << " -> " << (x) << endl
#define out(x,y) cout << ">> " << (x) << " " << (y) << endl
#define ou3(x,y,z) cout << ">> " << (x) << " " << (y) << " " << (z) << endl
#define mems(x,y) memset(&x,y,sizeof x)
#define memsv(x,y) memset(&x[0], y, sizeof(x[0]) * x.size())
#define popcount __builtin_popcountll
#define clz(x) (1<<(31-__builtin_clz(x)))
#define clzll(x) (1LL<<(63-__builtin_clzll(x)))
#define all(x) x.begin(),x.end()
#define rsort(x) sort(x), reverse(x)
#define revsort(a,b) sort(a,b), reverse(a,b)
#define PQ(T) priority_queue<T>
#define PQr(T) priority_queue<T, vector<T>, greater<T>>
#define permute(x) while(next_permutation(all(x)))
#define distinct(x) x.erase(unique(all(x)),x.end())
#define vi vector<int>
#define vvi vector<vi>
#define vii vector<pii>
#define vll vector<ll>
#define vlll vector<pll>
#define vvll vector<vll>
#define ari(x) array<int,x>
#define arll(x) array<ll,x>
#define ard(x) array<double,x>
#define pii pair<int,int> 
#define pll pair<ll,ll>
#define plll pair<ll,pll>
#define pllll pair<pll,pll>
#define piii pair<int,pii>
#define piiii pair<pii,pii>
#define psi pair<string,int>
#define endl '\n'
const int MAX = 3e3+5;
const int MOD = 1000000007;
const ll MOD2 = 2010405347;
const ll INF = 2e18;
const int dr[]={1,-1,0,0,1,1,-1,-1};
const int dc[]={0,0,-1,1,1,-1,1,-1};
const double pi = acos(-1);
const double EPS = 1e-9;
const int block = 500;
inline ll pw(ll x,ll y,ll md=MOD){ll ret=1;x%=md;while(y){if(y&1)ret=ret*x%md;x=x*x%md,y>>=1;}return ret;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n,cv[128],ans,dp[202][202][202][3],cs[3][3][202][202];
string s;
vi v[3];

int f(int a,int b,int c,int ls){
	if(a+b+c==n)return 0;
	int &ret = dp[a][b][c][ls];
	if(ret!=-1)return ret;
	ret = MOD;
	ari(3) z = {a,b,c};
	rep(i,0,2)if(i!=ls && z[i]!=v[i].size())ret = min(ret, f(a+(i==0), b+(i==1), c+(i==2), i) + cs[i][0][z[i]][a] + cs[i][1][z[i]][b] + cs[i][2][z[i]][c]);
	return ret;
}

int main(){
//	cout<<fixed<<setprecision(10);
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt","w",stdout);
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cv['R'] = 0;
	cv['G'] = 1;
	cv['Y'] = 2;
	cin>>n>>s;
	For(i,0,n)v[cv[s[i]]].pb(i);
	rep(i,0,2)if(v[i].size()>200)return cout<<"-1\n",0;
	ans = MOD;
	int nw;
	rep(i,0,2)rep(j,0,2)if(i!=j){
		For(a,0,v[j].size()){
			nw = a;
			For(b,0,v[i].size()){
				while(nw<v[j].size() && v[j][nw]<v[i][b])++nw;
				cs[i][j][b][a] = nw-a;
//				debug(vi({i,j,b,a,nw-a}));
			}
		}
	}
	mems(dp, -1);
	rep(i,0,2)if(v[i].size())ans = min(ans, f(i==0, i==1, i==2, i) + cs[i][0][0][0] + cs[i][1][0][0] + cs[i][2][0][0]);
	if(ans==MOD)ans = -1;
	cout<<ans<<endl;
	return 0;
}

Compilation message

joi2019_ho_t3.cpp: In function 'int f(int, int, int, int)':
joi2019_ho_t3.cpp:114:28: warning: comparison of integer expressions of different signedness: 'std::array<int, 3>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  114 |  rep(i,0,2)if(i!=ls && z[i]!=v[i].size())ret = min(ret, f(a+(i==0), b+(i==1), c+(i==2), i) + cs[i][0][z[i]][a] + cs[i][1][z[i]][b] + cs[i][2][z[i]][c]);
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:127:21: warning: array subscript has type 'char' [-Wchar-subscripts]
  127 |  For(i,0,n)v[cv[s[i]]].pb(i);
      |                     ^
joi2019_ho_t3.cpp:42:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 | #define For(i,n,N) for(int i=(n);i< (N);++i)
      |                                   ^
joi2019_ho_t3.cpp:132:3: note: in expansion of macro 'For'
  132 |   For(a,0,v[j].size()){
      |   ^~~
joi2019_ho_t3.cpp:42:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 | #define For(i,n,N) for(int i=(n);i< (N);++i)
      |                                   ^
joi2019_ho_t3.cpp:134:4: note: in expansion of macro 'For'
  134 |    For(b,0,v[i].size()){
      |    ^~~
joi2019_ho_t3.cpp:135:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  135 |     while(nw<v[j].size() && v[j][nw]<v[i][b])++nw;
      |           ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 33 ms 97108 KB Output is correct
2 Correct 34 ms 97056 KB Output is correct
3 Correct 33 ms 97044 KB Output is correct
4 Correct 35 ms 97120 KB Output is correct
5 Correct 34 ms 97012 KB Output is correct
6 Correct 35 ms 97040 KB Output is correct
7 Correct 33 ms 97124 KB Output is correct
8 Correct 33 ms 97036 KB Output is correct
9 Correct 37 ms 97120 KB Output is correct
10 Correct 33 ms 97100 KB Output is correct
11 Correct 34 ms 97020 KB Output is correct
12 Correct 39 ms 97108 KB Output is correct
13 Correct 34 ms 97056 KB Output is correct
14 Correct 34 ms 97016 KB Output is correct
15 Correct 38 ms 97020 KB Output is correct
16 Correct 34 ms 97124 KB Output is correct
17 Correct 31 ms 97104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 97108 KB Output is correct
2 Correct 34 ms 97056 KB Output is correct
3 Correct 33 ms 97044 KB Output is correct
4 Correct 35 ms 97120 KB Output is correct
5 Correct 34 ms 97012 KB Output is correct
6 Correct 35 ms 97040 KB Output is correct
7 Correct 33 ms 97124 KB Output is correct
8 Correct 33 ms 97036 KB Output is correct
9 Correct 37 ms 97120 KB Output is correct
10 Correct 33 ms 97100 KB Output is correct
11 Correct 34 ms 97020 KB Output is correct
12 Correct 39 ms 97108 KB Output is correct
13 Correct 34 ms 97056 KB Output is correct
14 Correct 34 ms 97016 KB Output is correct
15 Correct 38 ms 97020 KB Output is correct
16 Correct 34 ms 97124 KB Output is correct
17 Correct 31 ms 97104 KB Output is correct
18 Correct 30 ms 97168 KB Output is correct
19 Correct 31 ms 97196 KB Output is correct
20 Correct 34 ms 97108 KB Output is correct
21 Correct 29 ms 97092 KB Output is correct
22 Correct 29 ms 97128 KB Output is correct
23 Correct 30 ms 97204 KB Output is correct
24 Correct 37 ms 97080 KB Output is correct
25 Correct 34 ms 97108 KB Output is correct
26 Correct 30 ms 97152 KB Output is correct
27 Correct 37 ms 97144 KB Output is correct
28 Correct 34 ms 97096 KB Output is correct
29 Correct 32 ms 97168 KB Output is correct
30 Correct 33 ms 97092 KB Output is correct
31 Correct 35 ms 97108 KB Output is correct
32 Correct 44 ms 97180 KB Output is correct
33 Correct 36 ms 97208 KB Output is correct
34 Correct 32 ms 97100 KB Output is correct
35 Correct 35 ms 97096 KB Output is correct
36 Correct 34 ms 97108 KB Output is correct
37 Correct 40 ms 97084 KB Output is correct
38 Correct 33 ms 97192 KB Output is correct
39 Correct 34 ms 97092 KB Output is correct
40 Correct 35 ms 97100 KB Output is correct
41 Correct 36 ms 97084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 97024 KB Output is correct
2 Correct 36 ms 97432 KB Output is correct
3 Correct 33 ms 97372 KB Output is correct
4 Correct 33 ms 97480 KB Output is correct
5 Correct 37 ms 97400 KB Output is correct
6 Correct 35 ms 97384 KB Output is correct
7 Correct 32 ms 97452 KB Output is correct
8 Correct 33 ms 97380 KB Output is correct
9 Correct 33 ms 97364 KB Output is correct
10 Correct 33 ms 97420 KB Output is correct
11 Correct 32 ms 97356 KB Output is correct
12 Correct 33 ms 97192 KB Output is correct
13 Correct 32 ms 97244 KB Output is correct
14 Correct 34 ms 97400 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 97108 KB Output is correct
2 Correct 34 ms 97056 KB Output is correct
3 Correct 33 ms 97044 KB Output is correct
4 Correct 35 ms 97120 KB Output is correct
5 Correct 34 ms 97012 KB Output is correct
6 Correct 35 ms 97040 KB Output is correct
7 Correct 33 ms 97124 KB Output is correct
8 Correct 33 ms 97036 KB Output is correct
9 Correct 37 ms 97120 KB Output is correct
10 Correct 33 ms 97100 KB Output is correct
11 Correct 34 ms 97020 KB Output is correct
12 Correct 39 ms 97108 KB Output is correct
13 Correct 34 ms 97056 KB Output is correct
14 Correct 34 ms 97016 KB Output is correct
15 Correct 38 ms 97020 KB Output is correct
16 Correct 34 ms 97124 KB Output is correct
17 Correct 31 ms 97104 KB Output is correct
18 Correct 30 ms 97168 KB Output is correct
19 Correct 31 ms 97196 KB Output is correct
20 Correct 34 ms 97108 KB Output is correct
21 Correct 29 ms 97092 KB Output is correct
22 Correct 29 ms 97128 KB Output is correct
23 Correct 30 ms 97204 KB Output is correct
24 Correct 37 ms 97080 KB Output is correct
25 Correct 34 ms 97108 KB Output is correct
26 Correct 30 ms 97152 KB Output is correct
27 Correct 37 ms 97144 KB Output is correct
28 Correct 34 ms 97096 KB Output is correct
29 Correct 32 ms 97168 KB Output is correct
30 Correct 33 ms 97092 KB Output is correct
31 Correct 35 ms 97108 KB Output is correct
32 Correct 44 ms 97180 KB Output is correct
33 Correct 36 ms 97208 KB Output is correct
34 Correct 32 ms 97100 KB Output is correct
35 Correct 35 ms 97096 KB Output is correct
36 Correct 34 ms 97108 KB Output is correct
37 Correct 40 ms 97084 KB Output is correct
38 Correct 33 ms 97192 KB Output is correct
39 Correct 34 ms 97092 KB Output is correct
40 Correct 35 ms 97100 KB Output is correct
41 Correct 36 ms 97084 KB Output is correct
42 Correct 31 ms 97024 KB Output is correct
43 Correct 36 ms 97432 KB Output is correct
44 Correct 33 ms 97372 KB Output is correct
45 Correct 33 ms 97480 KB Output is correct
46 Correct 37 ms 97400 KB Output is correct
47 Correct 35 ms 97384 KB Output is correct
48 Correct 32 ms 97452 KB Output is correct
49 Correct 33 ms 97380 KB Output is correct
50 Correct 33 ms 97364 KB Output is correct
51 Correct 33 ms 97420 KB Output is correct
52 Correct 32 ms 97356 KB Output is correct
53 Correct 33 ms 97192 KB Output is correct
54 Correct 32 ms 97244 KB Output is correct
55 Correct 34 ms 97400 KB Output is correct
56 Correct 0 ms 212 KB Output is correct
57 Correct 1 ms 212 KB Output is correct
58 Correct 124 ms 97796 KB Output is correct
59 Correct 120 ms 97800 KB Output is correct
60 Correct 126 ms 97796 KB Output is correct
61 Correct 167 ms 97804 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 41 ms 97760 KB Output is correct
64 Correct 58 ms 97784 KB Output is correct
65 Correct 73 ms 97796 KB Output is correct
66 Correct 144 ms 97796 KB Output is correct
67 Correct 122 ms 97688 KB Output is correct
68 Correct 131 ms 97792 KB Output is correct
69 Correct 131 ms 97800 KB Output is correct
70 Correct 130 ms 97796 KB Output is correct
71 Correct 120 ms 97792 KB Output is correct
72 Correct 1 ms 340 KB Output is correct
73 Correct 0 ms 340 KB Output is correct