fork(1) download
  1. /*-------------icalFikr's template v2.1--------------*/
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <fstream>
  6. #include <string>
  7. #include <cstring>
  8. #include <vector>
  9. #include <list>
  10. #include <map>
  11. #include <bitset>
  12. #include <stack>
  13. #include <queue>
  14. #include <deque>
  15. #include <set>
  16. #include <cstdlib>
  17. #include <cmath>
  18. #include <complex>
  19. #include <algorithm>
  20. #include <iomanip>
  21. #include <cstddef>
  22. #include <cassert>
  23. #include <cctype>
  24. #include <utility>
  25. #include <ctime>
  26.  
  27. #ifndef GEDEBUG
  28. #define DEBUG printf(__VA_ARGS__)
  29. #define DEBUGS puts("gukguk")
  30. #define TIME() printf("\nFinished in %.3lf s\n",clock()/(double)CLOCKS_PER_SEC)
  31. #else
  32. #define DEBUG
  33. #define DEBUGS
  34. #define TIME()
  35. #endif
  36.  
  37. #ifdef __WIN32__
  38. char getchar_unlocked() { return getchar(); }
  39. #endif
  40.  
  41. /* Abbreviations */
  42. #define puf push_front
  43. #define pof pop_front()
  44. #define pub push_back
  45. #define pob pop_back()
  46. #define fs first
  47. #define sc second
  48. #define mp make_pair
  49. #define FOR(i,a,b) for(int i=int(a),_b=int(b);i<=_b;i++)
  50. #define FORINC(i,a,b,inc) for(int i=int(a),_b=int(b);i<=_b;i+=inc)
  51. #define FORLL(i,a,b) for(LL i=LL(a),_b=LL(b);i<=_b;i++)
  52. #define FORD(i,a,b) for(int i=int(a),_b=int(b);i>=_b;i--)
  53. #define FOREACH(i,a) for (__typeof(a.begin()) i=a.begin();i!=a.end();i++)
  54. #define OPEN freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
  55. #define CLOSE fclose(stdin); fclose(stdout);
  56. #define ALL(v) (v).begin(),(v).end()
  57. #define sz size()
  58. #define reset(x,N) memset(x,N,sizeof(x))
  59. #define nl puts("")
  60. #define gc getchar_unlocked()
  61.  
  62. using namespace std;
  63. typedef double db;
  64. typedef long long LL;
  65. typedef pair <int,int> ii;
  66. typedef pair <LL,LL> pll;
  67. typedef vector <ii> vii;
  68. typedef vector <int> vint;
  69.  
  70. const db EPS = 0.0000001;
  71. const db PI = acos(-1);
  72. const LL INFLL = 0x7FFFFFFFFFFFFFFF;
  73. const int INF = 0x7FFFFFFF;
  74.  
  75. template<typename T>
  76. inline void next(T &num) {
  77. char c; num=0;
  78. do { c=gc; }
  79. while (c!=EOF && c==' ' && c=='\n' && c=='\t');
  80. int sign=(c=='-' ? -1 : 1);
  81. if (c!='-') num+=(c-'0');
  82. while ( (c=gc)!=EOF && c!='\n' && c!='\t' && c!=' ') {
  83. num*=10; num+=(c-'0');
  84. }
  85. num*=sign;
  86. }
  87.  
  88. inline string getstr() {
  89. string str; char k;
  90. while( (k=gc)==' ' || k=='\n' ) {
  91. k=gc;
  92. if ( k==' ' || k=='\n' ) continue;
  93. else break;
  94. }
  95. str.pub(k);
  96. while((k=gc)!=EOF &&k!='\n' && k!='\t'
  97. && k!='\v' && k!='\0' && k!=' ')
  98. str.pub(k);
  99. return str;
  100. }
  101.  
  102. /*-------------end of template-------------*/
  103. char c[400];
  104. int st[6][6], cnt[6][6], h, v, n;
  105.  
  106. int _hash(char x) {
  107. switch (x) {
  108. case 'R' : return 0;
  109. case 'G' : return 1;
  110. case 'B' : return 2;
  111. case 'Y' : return 3;
  112. case 'W' : return 4;
  113. }
  114. }
  115.  
  116. int main() {
  117. //ios_base::sync_with_stdio(0);
  118. scanf("%d\n",&n);
  119. gets(c);
  120. if (!c[0]) gets(c);
  121.  
  122. for(int i = 0, m = strlen(c); i < m; i += 3 )
  123. st[_hash(c[i])][c[i+1]-'1'] = 2;
  124.  
  125. FOR(i,0,4) {
  126. int meme = 0;
  127. FOR(j,0,4)
  128. if (st[i][j]) meme = 1;
  129. v += meme;
  130. }
  131.  
  132. FOR(j,0,4) {
  133. int meme = 0;
  134. FOR(i,0,4)
  135. if (st[i][j]) meme = 1;
  136. h += meme;
  137. }
  138.  
  139. int ans = INF;
  140. FOR(msk, 0, (1<<10)-1) {
  141. memcpy(cnt,st,sizeof st);
  142.  
  143. if (v > 1) {
  144. FOR(i,0,4)
  145. if ((1<<(i+5)) & msk)
  146. FOR(j,0,4)
  147. cnt[i][j]--;
  148. }
  149.  
  150. if (h > 1) {
  151. FOR(j,0,4)
  152. if ((1<<j) & msk)
  153. FOR(i,0,4)
  154. cnt[i][j]--;
  155. }
  156.  
  157. int banjir = 0;
  158. FOR(i,0,4)
  159. FOR(j,0,4)
  160. banjir += (cnt[i][j] > 1);
  161.  
  162. if (banjir > 1) continue;
  163.  
  164. bool stat = 1;
  165.  
  166. for(int i = 0; i < 5; ++i) {
  167. if (!stat) break;
  168. banjir = 0;
  169. for(int j = 0; j < 5; ++j)
  170. if (cnt[i][j] == 1)
  171. banjir++;
  172. stat = (banjir < 2);
  173. }
  174.  
  175. for(int j = 0; j < 5; ++j) {
  176. if (!stat) break;
  177. banjir = 0;
  178. for(int i = 0; i < 5; ++i)
  179. if (cnt[i][j] == 1)
  180. banjir++;
  181. stat = (banjir < 2);
  182. }
  183.  
  184. //cout << msk / 32 << ' ' << msk % 32 << endl;
  185. if (stat)
  186. ans = min(ans, __builtin_popcount(msk));
  187. }
  188.  
  189. printf("%d\n",ans);
  190. return 0;
  191. }
Success #stdin #stdout 0s 3300KB
stdin
5
B1 Y1 W1 R2 R1
stdout
4