250 Problem Statement
Farmer John recently found out about a popular online farming game.There are n types of plants in the game. The types are numbered 0 through n-1. At the beginning of the game, Farmer John is given one seed of each plant type.There is a single plot of land. At any time the plot can only contain at most one plant. Whenever the plot is empty, Farmer John can plant one of the seeds. Once a seed of type i is planted, it takes time[i] seconds until it grows into a fully developed plant. When that happens, Farmer John will harvest the plant and the plot will become empty again. Planting a seed and harvesting a plant happens instanteously.
Farmer John also has budget coins. He can spend these coins to make his plants grow faster. For each i, Farmer John may pay cost[i] coins to reduce time[i] by 1. Farmer John may pay for the same plant multiple times, each time decreasing its growing time by 1. Obviously, the growing time cannot be reduced below 0.
You are given the vector <int>s time and cost with n elements each, and the int budget. Determine and return the minimum amount of time in which Farmer John can grow a single plant of each type.
Definition
Class:
FarmvilleDiv2
Method:
minTime
Parameters:
vector <int>, vector <int>, int
Returns:
int
Method signature:
int minTime(vector <int> time, vector <int> cost, int budget)
(be sure your method is public)
题目类型:贪心
算法分析:先将cost值低的时间消去,注意每次消去时要将当前值减去!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
// BEGIN CUT HERE // END CUT HERE #line 5 "FarmvilleDiv2.cpp" #include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <iostream> #include <sstream> #include <map> #include <set> #include <queue> #include <stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define ULL unsigned long long #define REP(i,n) for(i=0;i<(n);++i) #define FOR(i,l,h) for(i=(l);i<=(h);++i) #define FORD(i,h,l) for(i=(h);i>=(l);--i) typedef vector<int> VI; typedef vector<string> VS; typedef vector<double> VD; //typedef long long LL; typedef pair<int,int> PII; struct node { int x, y; }; bool cmp (node a, node b) {return a.y < b.y;} node aa[66]; class FarmvilleDiv2 { public: int minTime(vector <int> time, vector <int> cost, int budget) { int n = time.size (); for (int i = 0; i < n; i++) aa[i].x = time[i]; for (int i = 0; i < n; i++) aa[i].y = cost[i]; sort (aa, aa + n, cmp); int res = 0; bool is_first = true; for (int i = 0; i < n; ) { if (i < n && budget >= aa[i].x * aa[i].y) { while (i < n && budget >= aa[i].x * aa[i].y) { budget -= aa[i].x * aa[i].y; i++; } } else if (i < n) { if (is_first) { res += aa[i].x - (budget / aa[i].y); budget = budget - (budget / aa[i].y) * aa[i].y; is_first = false; } else { res += aa[i].x; } i++; } } return res; } // BEGIN CUT HERE public: 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(); if ((Case == -1) || (Case == 5)) test_case_5(); } 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() { int Arr0[] = {100}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 26; int Arg3 = 74; verify_case(0, Arg3, minTime(Arg0, Arg1, Arg2)); } void test_case_1() { int Arr0[] = {100}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 101; int Arg3 = 0; verify_case(1, Arg3, minTime(Arg0, Arg1, Arg2)); } void test_case_2() { int Arr0[] = {2,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 3; int Arg3 = 0; verify_case(2, Arg3, minTime(Arg0, Arg1, Arg2)); } void test_case_3() { int Arr0[] = {1,2,3,4,5}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {5,4,3,2,1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 15; int Arg3 = 6; verify_case(3, Arg3, minTime(Arg0, Arg1, Arg2)); } void test_case_4() { int Arr0[] = {100,100,100,100,100,100,100,100,100,100}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2,4,6,8,10,1,3,5,7,9}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 5000; int Arg3 = 50; verify_case(4, Arg3, minTime(Arg0, Arg1, Arg2)); } void test_case_5() { int Arr0[] = {30,40,20,10}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {10,20,30,40}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 5; int Arg3 = 100; verify_case(5, Arg3, minTime(Arg0, Arg1, Arg2)); } // END CUT HERE }; // BEGIN CUT HERE int main() { FarmvilleDiv2 ___test; ___test.run_test(-1); return 0; } // END CUT HERE |
550 Problem Statement
Alice and Bob have a rectangular board divided into a grid with r rows and c columns. The grid is described by the vector <string> s. Each character of s represents one cell. There are four types of cells:
'E' denotes an exit. There may be arbitrarily many exits, possibly zero.
'T' means the square contains a single token. Initially there will be exactly one token on the entire board.
'#' denotes an obstacle.
'.' denotes an empty cell.
Alice and Bob will play a game on this board. The game is parameterized by the int k. The token initially has the number k on it. The players will take alternating turns, starting with Alice. A player's turn consists of the following steps:
The player moves the token one square up, down, left, or right. The token cannot go over the edge of the board and it cannot enter a cell with an obstacle.
If this token is on an exit, it disappears from the board.
Otherwise, subtract one from the number on the token. If the number on the token is zero, remove it from the board.
The first player unable to make a move loses the game. (This happens when the token is stuck and also when it already left the board.)
You are given the vector <string> s and the int k Compute the winner of the game, assuming both Alice and Bob play optimally. Return the winner's name: either "Alice" or "Bob". Note that the return value is case-sensitive.
Definition
Class: BoardEscapeDiv2
Method: findWinner
Parameters: vector <string>, int
Returns: string
Method signature: string findWinner(vector <string> s, int k)
(be sure your method is public)
题目类型:博弈(记忆化搜索)
算法分析:对于完全信息的博弈问题,求解过程需要在一个博弈树上进行dfs遍历并在回溯时计算当前节点的估价值并选择对于自己来说最好的那个状态。这里设定估价值"1"表示对当前节点好的值,"0"表示对当前节点不好的值。回溯时判断当前节点的所有子节点的估价值,若所有子节点的估价值都是"1",则当前节点的估价值为"0"。否则,当前节点的估价值为"1"。对于计算过的值使用数组dp存下来节省时间开销
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
// BEGIN CUT HERE // END CUT HERE #line 5 "BoardEscapeDiv2.cpp" #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int dp[66][66][166]; int n, m; vector <string> g; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; class BoardEscapeDiv2 { public: int dfs (int x, int y, int k) { if (x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '#') return 1; if (g[x][y] == 'E' || k == 0) return 0; if (dp[x][y][k] != -1) return dp[x][y][k]; for (int i = 0; i < 4; i++) if (!dfs (x + dx[i], y + dy[i], k - 1)) return dp[x][y][k] = 1; return dp[x][y][k] = 0; } string findWinner(vector <string> s, int k) { n = s.size (), m = s[0].size (); g = s; memset (dp, -1, sizeof (dp)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (g[i][j] == 'T') { g[i][j] = '.'; int win = dfs (i, j, k); if (win) return string ("Alice"); else return string ("Bob"); } } // BEGIN CUT HERE public: 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(); if ((Case == -1) || (Case == 5)) test_case_5(); } 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 string &Expected, const string &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[] = {"T.#", "#.E"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 3; string Arg2 = "Alice"; verify_case(0, Arg2, findWinner(Arg0, Arg1)); } void test_case_1() { string Arr0[] = {"E#E", "#T#", "E#E"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 99; string Arg2 = "Bob"; verify_case(1, Arg2, findWinner(Arg0, Arg1)); } void test_case_2() { string Arr0[] = {"#E...", "#...E", "E.T#.", "..#.."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 13; string Arg2 = "Alice"; verify_case(2, Arg2, findWinner(Arg0, Arg1)); } void test_case_3() { string Arr0[] = {"TE"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 50; string Arg2 = "Alice"; verify_case(3, Arg2, findWinner(Arg0, Arg1)); } void test_case_4() { string Arr0[] = {".T."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; string Arg2 = "Alice"; verify_case(4, Arg2, findWinner(Arg0, Arg1)); } void test_case_5() { string Arr0[] = {"..........................", "......EEE..EEE..E...E.....", ".....E.....E..E.EE.EE.....", "......EEE..EEE..E.E.E.....", ".........E.E.E..E...E.....", "......EEE..E..E.E...E.....", "..........................", "...E#E#E#E#E#E#E#E#E#E#...", "..........................", "......EEE..EEE...EEE......", ".....E........E.E.........", "......EEE.....E..EEE......", ".....E...E...E..E...E.....", "......EEE....E...EEE......", "..........................", "...#E#E#E#E#E#E#E#E#E#E...", "..........................", "....EE...E...E..E.EEE.E...", "...E.....E...E..E.E...E...", "...E.EE..E...EEEE.EE..E...", "...E..E..E...E..E.E.......", "....EE...EEE.E..E.E...E...", "T........................."}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 100; string Arg2 = "Bob"; verify_case(5, Arg2, findWinner(Arg0, Arg1)); } // END CUT HERE }; // BEGIN CUT HERE int main() { BoardEscapeDiv2 ___test; ___test.run_test(-1); return 0; } // END CUT HERE |
- « 上一篇:poj2182