SRM 513 2 1000CutTheNumbers


Problem Statement

Manao has a board filled with digits represented as String[] board. The j-th character of the i-th element of board represents the digit written in cell in row i, column j of the board. The rows are numbered from top to bottom and the columns are numbered from left to right.

Manao is going to cut it into several non-overlapping fragments. Each of the fragments will be a horizontal or vertical strip containing 1 or more elements. A strip of length N can be interpreted as an N-digit number in base 10 by concatenating the digits on the strip in order. The horizontal strips are read from left to right and the vertical strips are read from top to bottom. The picture below shows a possible cutting of a 4x4 board: 

The sum of the numbers on the fragments in this picture is 493 + 7160 + 23 + 58 + 9 + 45 + 91 = 7879.

Manao wants to cut the board in such a way that the sum of the numbers on the resulting fragments is the maximum possible. Compute and return this sum.

Definition

  • ClassCutTheNumbers
  • MethodmaximumSum
  • Parametersvector<string>
  • Returnsint
  • Method signatureint maximumSum(vector<string> board)
(be sure your method is public)

Limits

  • Time limit (s)2.000
  • Memory limit (MB)64

Notes

  • The numbers on the cut strips are allowed to have leading zeros. See example #2 for details.

Constraints

  • board will contain between 1 and 4 elements, inclusive.
  • board[0] will be between 1 and 4 characters long, inclusive.
  • Each element of board will be of the same length as board[0].
  • Each character in board will be a decimal digit ('0'-'9').

Test cases

  1.  
    • board{"123",
       "312"}
     

    Returns435

     
    Manao can cut out both rows in whole, obtaining 123 + 312 = 435. He also could cut the columns one by one for a total of 66, or cut the first column and the residual rows one by one, obtaining 13 + 23 + 12 = 48, or even cut out single elements, but would not get a better sum.
  2.  
    • board{"99",
       "11"}
     

    Returns182

     
    It's better to cut out the whole columns.
  3.  
    • board{"001",
       "010",
       "111",
       "100"}
     

    Returns1131

     
    The numbers on the strips may have leading zeros. Cutting the columns in whole, Manao obtains 0011 + 0110 + 1010 = 1131.
  4.  
    • board{ "8" }
     

    Returns8


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

你可以认为0的话,就是向左连,1的话就是向下连。

然后状态压缩一下。枚举所有的状态即可。

  1. #include <cstdio>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <ctime>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <set>
  8. #include <vector>
  9. #include <sstream>
  10. #include <typeinfo>
  11. #include <fstream>
  12.  
  13. using namespace std;
  14. const int inf = 0x3f3f3f3f ;
  15. int n , m ;
  16. int maxn ;
  17. int path[] ;
  18. vector<int> b[] ;
  19. class CutTheNumbers {
  20. public:
  21. int maximumSum(vector<string> board) {
  22. n = board.size () ;
  23. m = board[].size () ;
  24. for (int i = ; i < n ; i ++) b[i].clear () ;
  25. for (int i = ; i < n ; i ++) {
  26. for (int j = ; j < m ; j ++) {
  27. b[i].push_back( board[i][j] - '' );
  28. }
  29. }
  30. maxn = - inf ;
  31. dfs () ;
  32. return maxn ;
  33. }
  34.  
  35. void dfs (int dep) {
  36. if (dep == n) {
  37. solve () ;
  38. return ;
  39. }
  40. for (int i = ; i < ( << m) ; i ++) {
  41. path[dep] = i ;
  42. dfs (dep + ) ;
  43. }
  44. }
  45.  
  46. void solve () {
  47. bool map[][] ;
  48. int ans = ;
  49. memset (map , , sizeof(map) ) ;
  50.  
  51. for (int i = ; i < n ; i ++) {
  52. int j = ;
  53. int tmp = path[i] ;
  54. while (tmp) {
  55. map[i][j ++] = tmp & ;
  56. tmp>>= ;
  57. }
  58. reverse (map[i] , map[i] + m) ;
  59. }
  60.  
  61. bool mark[][] ;
  62. memset (mark , , sizeof(mark) ) ;
  63.  
  64. for (int i = ; i < n ; i ++) {
  65. for (int j = ; j < m ; j ++) {
  66. if (!mark[i][j]) {
  67. int tmp = ;
  68. if (!map[i][j]) {
  69. for (int k = j ; k < m && !map[i][k] ; k ++) {
  70. mark[i][k] = ;
  71. tmp = tmp * + b[i][k] ;
  72. }
  73. }
  74. else {
  75. for (int k = i ; k < n && map[k][j] ; k ++) {
  76. mark[k][j] = ;
  77. tmp = tmp * + b[k][j] ;
  78. }
  79. }
  80. ans += tmp ;
  81. }
  82. }
  83. }
  84. maxn = max (maxn , ans) ;
  85. }
  86.  
  87. };
  88.  
  89. // CUT begin
  90. ifstream data("CutTheNumbers.sample");
  91.  
  92. string next_line() {
  93. string s;
  94. getline(data, s);
  95. return s;
  96. }
  97.  
  98. template <typename T> void from_stream(T &t) {
  99. stringstream ss(next_line());
  100. ss >> t;
  101. }
  102.  
  103. void from_stream(string &s) {
  104. s = next_line();
  105. }
  106.  
  107. template <typename T> void from_stream(vector<T> &ts) {
  108. int len;
  109. from_stream(len);
  110. ts.clear();
  111. for (int i = ; i < len; ++i) {
  112. T t;
  113. from_stream(t);
  114. ts.push_back(t);
  115. }
  116. }
  117.  
  118. template <typename T>
  119. string to_string(T t) {
  120. stringstream s;
  121. s << t;
  122. return s.str();
  123. }
  124.  
  125. string to_string(string t) {
  126. return "\"" + t + "\"";
  127. }
  128.  
  129. bool do_test(vector<string> board, int __expected) {
  130. time_t startClock = clock();
  131. CutTheNumbers *instance = new CutTheNumbers();
  132. int __result = instance->maximumSum(board);
  133. double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC;
  134. delete instance;
  135.  
  136. if (__result == __expected) {
  137. cout << "PASSED!" << " (" << elapsed << " seconds)" << endl;
  138. return true;
  139. }
  140. else {
  141. cout << "FAILED!" << " (" << elapsed << " seconds)" << endl;
  142. cout << " Expected: " << to_string(__expected) << endl;
  143. cout << " Received: " << to_string(__result) << endl;
  144. return false;
  145. }
  146. }
  147.  
  148. int run_test(bool mainProcess, const set<int> &case_set, const string command) {
  149. int cases = , passed = ;
  150. while (true) {
  151. if (next_line().find("--") != )
  152. break;
  153. vector<string> board;
  154. from_stream(board);
  155. next_line();
  156. int __answer;
  157. from_stream(__answer);
  158.  
  159. cases++;
  160. if (case_set.size() > && case_set.find(cases - ) == case_set.end())
  161. continue;
  162.  
  163. cout << " Testcase #" << cases - << " ... ";
  164. if ( do_test(board, __answer)) {
  165. passed++;
  166. }
  167. }
  168. if (mainProcess) {
  169. cout << endl << "Passed : " << passed << "/" << cases << " cases" << endl;
  170. int T = time(NULL) - ;
  171. double PT = T / 60.0, TT = 75.0;
  172. cout << "Time : " << T / << " minutes " << T % << " secs" << endl;
  173. cout << "Score : " << * (0.3 + (0.7 * TT * TT) / (10.0 * PT * PT + TT * TT)) << " points" << endl;
  174. }
  175. return ;
  176. }
  177.  
  178. int main(int argc, char *argv[]) {
  179. cout.setf(ios::fixed, ios::floatfield);
  180. cout.precision();
  181. set<int> cases;
  182. bool mainProcess = true;
  183. for (int i = ; i < argc; ++i) {
  184. if ( string(argv[i]) == "-") {
  185. mainProcess = false;
  186. } else {
  187. cases.insert(atoi(argv[i]));
  188. }
  189. }
  190. if (mainProcess) {
  191. cout << "CutTheNumbers (1000 Points)" << endl << endl;
  192. }
  193. return run_test(mainProcess, cases, argv[]);
  194. }
  195. // CUT end

SRM 513 2 1000CutTheNumbers(状态压缩)的更多相关文章

  1. 状态压缩DP SRM 667 Div1 OrderOfOperations 250

    Problem Statement      Cat Noku has just finished writing his first computer program. Noku's compute ...

  2. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  3. HDU 3605:Escape(最大流+状态压缩)

    http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意:有n个人要去到m个星球上,这n个人每个人对m个星球有一个选择,即愿不愿意去,"Y" ...

  4. [HDU 4336] Card Collector (状态压缩概率dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题目大意:有n种卡片,需要吃零食收集,打开零食,出现第i种卡片的概率是p[i],也有可能不出现卡 ...

  5. HDU 4336 Card Collector (期望DP+状态压缩 或者 状态压缩+容斥)

    题意:有N(1<=N<=20)张卡片,每包中含有这些卡片的概率,每包至多一张卡片,可能没有卡片.求需要买多少包才能拿到所以的N张卡片,求次数的期望. 析:期望DP,是很容易看出来的,然后由 ...

  6. codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

    B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...

  7. NOIP2005过河[DP 状态压缩]

    题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...

  8. vijos1426兴奋剂检查(多维费用的背包问题+状态压缩+hash)

    背景 北京奥运会开幕了,这是中国人的骄傲和自豪,中国健儿在运动场上已经创造了一个又一个辉煌,super pig也不例外……………… 描述 虽然兴奋剂是奥运会及其他重要比赛的禁药,是禁止服用的.但是运动 ...

  9. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

随机推荐

  1. POJ 1804 Brainman(归并排序)

    传送门 Description Background Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted ...

  2. [JavaEE]理解ThreadLocal

    转http://www.iteye.com/topic/103804 首先,ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线程中 ...

  3. 【Beta版本】冲刺-Day1

    队伍:606notconnected 会议时间:12月9日 目录 一.行与思 二.站立式会议图片 三.燃尽图 四.代码Check-in 一.行与思 张斯巍(433) 今日进展:git学习,xml语言学 ...

  4. 【Phylab2.0】Alpha版本测试报告

    测试报告集 点击链接

  5. python Scrapy安装和介绍

    python Scrapy安装和介绍 Windows7下安装1.执行easy_install Scrapy Centos6.5下安装 1.库文件安装yum install libxslt-devel ...

  6. D/A转换器实验

    1.代码: #include<reg52.h>typedef unsigned char u8;typedef unsigned int u16;void delay (u16 num){ ...

  7. log4net 记录到数据库和本地文件

    1)配置代码 <?xml version="1.0" encoding="utf-8" ?> <configuration> <c ...

  8. js日期加减

    先补充下基础知识: var myDate = new Date(); //myDate默认返回当前时间 myDate.getYear(); //获取当前年份(2位) myDate.getFullYea ...

  9. linux配置oracle11G监听及本地网络服务 及 数据库建库

    配置监听及本地网络服务 在oracle用户的图形界面oracle用户中,新开启一个终端,输入命令netca 会弹出如下界面. 数据库建库 在oracle用户的图形界面oracle用户中,新开启一个终端 ...

  10. JavaScript学习笔记——节点

    javascript-节点属性详解 根据 DOM,HTML 文档中的每个成分都是一个节点. DOM 是这样规定的: 整个文档是一个文档节点 每个 HTML 标签是一个元素节点 包含在 HTML 元素中 ...