TopCoder: SRM146 DIV2 1000

Problem Statement

    

This task is about the scoring in the first phase of the die-game Yahtzee, where five dice are used. The score is determined by the values on the upward die faces after a roll. The player gets to choose a value, and all dice that show the chosen value are considered active. The score is simply the sum of values on active dice.

Say, for instance, that a player ends up with the die faces showing 2, 2, 3, 5 and 4. Choosing the value two makes the dice showing 2 active and yields a score of 2 + 2 = 4, while choosing 5 makes the one die showing 5 active, yielding a score of 5.

Your method will take as input a vector <int> toss, where each element represents the upward face of a die, and return the maximum possible score with these values.

Definition

    
Class: YahtzeeScore
Method: maxPoints
Parameters: vector <int>
Returns: int
Method signature: int maxPoints(vector <int> toss)
(be sure your method is public)
    
 

Constraints

- toss will contain exactly 5 elements.
- Each element of toss will be between 1 and 6, inclusive.

Examples

0)  
    
{ 2, 2, 3, 5, 4 }
Returns: 5
The example from the text.
1)  
    
{ 6, 4, 1, 1, 3 }
Returns: 6
Selecting 1 as active yields 1 + 1 = 2, selecting 3 yields 3, selecting 4 yields 4 and selecting 6 yields 6, which is the maximum number of points.
2)  
    
{ 5, 3, 5, 3, 3 }
Returns: 10

这题一开始找不出规律,于是就在纸上从3个一直拓展,发现原来是DP题。

ans[i]可以从两种策略里得出,第一种是times[0]回来(最小那个),把times[i-1]带走,这样就是ans[i-1]+times[0]+times[i-1],第二种就是times[i-2]与times[i-1]一起走,一开始要把times[1]放对面去,这样就是times[1]+times[0]+times[i-1]+times[1]+ans[i-2]。

 1 // BEGIN CUT HERE

 2 

 3 // END CUT HERE

 4 #include <functional>

 5 #include <algorithm>

 6 #include <stdexcept>

 7 #include <iostream>

 8 #include <sstream>

 9 #include <fstream>

10 #include <iomanip>

11 #include <cstdlib>

12 #include <cstring>

13 #include <utility>

14 #include <cctype>

15 #include <vector>

16 #include <string>

17 #include <bitset>

18 #include <queue>

19 #include <stack>

20 #include <ctime>

21 #include <list>

22 #include <map>

23 #include <set>

24 #include <math.h>

25 

26 using namespace std;

27 

28 #define pb push_back

29 #define INF 100000000000

30 #define L(s) (int)((s).size())

31 #define FOR(i,a,b) for (int _n(b), i(a); i<=_n; i++)

32 #define rep(i,n) FOR(i,1,(n))

33 #define rept(i,n) FOR(i,0,(n)-1)

34 #define rept2(i, m, j, n) FOR(i, 0, (m)-1) FOR(j, 0, (n)-1)

35 #define rep2(i, m, j, n) FOR(i, 1, (m)) FOR(j, 1, (n))

36 #define C(a) memset((a), 0, sizeof(a))

37 #define ll long long

38 #define VI vector<int>

39 #define ppb pop_back

40 #define mp make_pair

41 #define MOD 1000000007

42 int toInt(string s){ istringstream sin(s); int t; sin>>t;return t;}

43 

44 

45 class BridgeCrossing

46 {

47         public:

48         int minTime(vector <int> times)

49         {

50             if (L(times) == 1) return times[0];

51             if (L(times) == 2) return max(times[0], times[1]);

52             sort(times.begin(), times.end());

53             vector<int> ans(L(times)+1, 0);

54             ans[2] = times[1];

55             ans[3] = times[0]+times[1]+times[2];

56             for (int i = 4; i <= L(times); i++) {

57                 ans[i] = min(ans[i-1]+times[0]+times[i-1], ans[i-2]+times[i-1]+times[0]+times[1]*2);

58             }

59             return ans[L(times)];

60         }

61 

62 // BEGIN CUT HERE

63     public:

64     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(); }

65     private:

66     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(); }

67     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; } }

68     void test_case_0() { int Arr0[] = { 1, 2, 5, 10 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 17; verify_case(0, Arg1, minTime(Arg0)); }

69     void test_case_1() { int Arr0[] = { 1, 2, 3, 4, 5 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 16; verify_case(1, Arg1, minTime(Arg0)); }

70     void test_case_2() { int Arr0[] = { 100 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 100; verify_case(2, Arg1, minTime(Arg0)); }

71     void test_case_3() { int Arr0[] = { 1, 2, 3, 50, 99, 100 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 162; verify_case(3, Arg1, minTime(Arg0)); }

72 

73 // END CUT HERE

74 

75 };

76 // BEGIN CUT HERE

77 int main()

78 {

79         BridgeCrossing ___test;

80         ___test.run_test(-1);

81         return 0;

82 }

83 // END CUT HERE

 

你可能感兴趣的:(topcoder)