DIV1 300pt
题意:有m种颜色的球若干个放在n个盒子里。每次操作可从一个盒子里拿出任意个球(不必同色),放进另一个盒子。要求终态为:1、最多有一个盒子里面装有不同色的球,该盒子成为joker box,也可以没有joker box;2、除了joker box,其他盒子要么为空,要么都为同色球;3、对于每种颜色的球,除了在joker box里的球,其余都在同一个盒子里,或者该颜色所有球都在joker box。
给定初态时n个盒子里含有每种颜色的球各多少个,问转移到终态最少需要操作多少次。
解法:贪心。有一种操作是,首先选出一个joker box,然后将其他盒子里的所有球全部拿到joker box里面,这样操作次数为n-1,每个盒子操作一次。而对于任意一个盒子,要使操作次数为0,有两种可能:1、初态时它为空;2、初态时它仅含一种颜色t的球,且之前没有盒子初态仅含颜色t的球。如果不满足则两个条件,直接将盒子拿空。
tag:greedy, good
1 // BEGIN CUT HERE 2 /* 3 * Author: plum rain 4 * score : 5 */ 6 /* 7 8 */ 9 // END CUT HERE 10 #line 11 "MarblesRegroupingEasy.cpp" 11 #include <sstream> 12 #include <stdexcept> 13 #include <functional> 14 #include <iomanip> 15 #include <numeric> 16 #include <fstream> 17 #include <cctype> 18 #include <iostream> 19 #include <cstdio> 20 #include <vector> 21 #include <cstring> 22 #include <cmath> 23 #include <algorithm> 24 #include <cstdlib> 25 #include <set> 26 #include <queue> 27 #include <bitset> 28 #include <list> 29 #include <string> 30 #include <utility> 31 #include <map> 32 #include <ctime> 33 #include <stack> 34 35 using namespace std; 36 37 #define clr0(x) memset(x, 0, sizeof(x)) 38 #define clr1(x) memset(x, -1, sizeof(x)) 39 #define pb push_back 40 #define sz(v) ((int)(v).size()) 41 #define all(t) t.begin(),t.end() 42 #define zero(x) (((x)>0?(x):-(x))<eps) 43 #define out(x) cout<<#x<<":"<<(x)<<endl 44 #define tst(a) cout<<a<<" " 45 #define tst1(a) cout<<#a<<endl 46 #define CINBEQUICKER std::ios::sync_with_stdio(false) 47 48 typedef vector<int> vi; 49 typedef vector<string> vs; 50 typedef vector<double> vd; 51 typedef pair<int, int> pii; 52 typedef long long int64; 53 54 const double eps = 1e-8; 55 const double PI = atan(1.0)*4; 56 const int inf = 2139062143 / 2; 57 58 class MarblesRegroupingEasy 59 { 60 public: 61 bool vis[100]; 62 int minMoves(vector <string> v){ 63 clr0 (vis); 64 int cnt = 0; 65 for (int i = 0; i < sz(v); ++ i){ 66 string s = v[i]; 67 int tmp = 0, idx = 0; 68 for (int j = 0; j < sz(s); ++ j) 69 if (s[j] != '0') ++ tmp, idx = j; 70 if (!tmp) continue; 71 if (tmp == 1 && !vis[idx]){ 72 vis[idx] = 1; continue; 73 } 74 ++ cnt; 75 } 76 if (cnt) -- cnt; 77 return cnt; 78 } 79 80 // BEGIN CUT HERE 81 public: 82 void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); } 83 private: 84 template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '"' << *iter << "","; os << " }"; return os.str(); } 85 void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << " Expected: "" << Expected << '"' << endl; cerr << " Received: "" << Received << '"' << endl; } } 86 void test_case_0() { string Arr0[] = {"20", 87 "11"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; verify_case(0, Arg1, minMoves(Arg0)); } 88 void test_case_1() { string Arr0[] = {"11", 89 "11", 90 "10"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(1, Arg1, minMoves(Arg0)); } 91 void test_case_2() { string Arr0[] = {"10", 92 "10", 93 "01", 94 "01"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(2, Arg1, minMoves(Arg0)); } 95 void test_case_3() { string Arr0[] = {"11", 96 "11", 97 "11", 98 "10", 99 "10", 100 "01"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; verify_case(3, Arg1, minMoves(Arg0)); } 101 void test_case_4() { string Arr0[] = {"020008000070", 102 "000004000000", 103 "060000600000", 104 "006000000362", 105 "000720000000", 106 "000040000000", 107 "004009003000", 108 "000800000000", 109 "020030003000", 110 "000500200000", 111 "000000300000"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; verify_case(4, Arg1, minMoves(Arg0)); } 112 113 // END CUT HERE 114 115 }; 116 117 // BEGIN CUT HERE 118 int main() 119 { 120 // freopen( "a.out" , "w" , stdout ); 121 MarblesRegroupingEasy ___test; 122 ___test.run_test(-1); 123 return 0; 124 } 125 // END CUT HERE