SRM 387(1-250pt)

时间:2023-03-09 02:43:07
SRM 387(1-250pt)

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

 // BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "MarblesRegroupingEasy.cpp"
#include <sstream>
#include <stdexcept>
#include <functional>
#include <iomanip>
#include <numeric>
#include <fstream>
#include <cctype>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define clr0(x) memset(x, 0, sizeof(x))
#define clr1(x) memset(x, -1, sizeof(x))
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(t) t.begin(),t.end()
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<a<<" "
#define tst1(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int inf = / ; class MarblesRegroupingEasy
{
public:
bool vis[];
int minMoves(vector <string> v){
clr0 (vis);
int cnt = ;
for (int i = ; i < sz(v); ++ i){
string s = v[i];
int tmp = , idx = ;
for (int j = ; j < sz(s); ++ j)
if (s[j] != '') ++ tmp, idx = j;
if (!tmp) continue;
if (tmp == && !vis[idx]){
vis[idx] = ; continue;
}
++ cnt;
}
if (cnt) -- cnt;
return cnt;
} // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); if ((Case == -) || (Case == )) test_case_4(); }
private:
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(); }
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 << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { string Arr0[] = {"",
""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, minMoves(Arg0)); }
void test_case_1() { string Arr0[] = {"",
"",
""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, minMoves(Arg0)); }
void test_case_2() { string Arr0[] = {"",
"",
"",
""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, minMoves(Arg0)); }
void test_case_3() { string Arr0[] = {"",
"",
"",
"",
"",
""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, minMoves(Arg0)); }
void test_case_4() { string Arr0[] = {"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); int Arg1 = ; verify_case(, Arg1, minMoves(Arg0)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
MarblesRegroupingEasy ___test;
___test.run_test(-);
return ;
}
// END CUT HERE