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的话就是向下连。

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

 #include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream> using namespace std;
const int inf = 0x3f3f3f3f ;
int n , m ;
int maxn ;
int path[] ;
vector<int> b[] ;
class CutTheNumbers {
public:
int maximumSum(vector<string> board) {
n = board.size () ;
m = board[].size () ;
for (int i = ; i < n ; i ++) b[i].clear () ;
for (int i = ; i < n ; i ++) {
for (int j = ; j < m ; j ++) {
b[i].push_back( board[i][j] - '' );
}
}
maxn = - inf ;
dfs () ;
return maxn ;
} void dfs (int dep) {
if (dep == n) {
solve () ;
return ;
}
for (int i = ; i < ( << m) ; i ++) {
path[dep] = i ;
dfs (dep + ) ;
}
} void solve () {
bool map[][] ;
int ans = ;
memset (map , , sizeof(map) ) ; for (int i = ; i < n ; i ++) {
int j = ;
int tmp = path[i] ;
while (tmp) {
map[i][j ++] = tmp & ;
tmp>>= ;
}
reverse (map[i] , map[i] + m) ;
} bool mark[][] ;
memset (mark , , sizeof(mark) ) ; for (int i = ; i < n ; i ++) {
for (int j = ; j < m ; j ++) {
if (!mark[i][j]) {
int tmp = ;
if (!map[i][j]) {
for (int k = j ; k < m && !map[i][k] ; k ++) {
mark[i][k] = ;
tmp = tmp * + b[i][k] ;
}
}
else {
for (int k = i ; k < n && map[k][j] ; k ++) {
mark[k][j] = ;
tmp = tmp * + b[k][j] ;
}
}
ans += tmp ;
}
}
}
maxn = max (maxn , ans) ;
} }; // CUT begin
ifstream data("CutTheNumbers.sample"); string next_line() {
string s;
getline(data, s);
return s;
} template <typename T> void from_stream(T &t) {
stringstream ss(next_line());
ss >> t;
} void from_stream(string &s) {
s = next_line();
} template <typename T> void from_stream(vector<T> &ts) {
int len;
from_stream(len);
ts.clear();
for (int i = ; i < len; ++i) {
T t;
from_stream(t);
ts.push_back(t);
}
} template <typename T>
string to_string(T t) {
stringstream s;
s << t;
return s.str();
} string to_string(string t) {
return "\"" + t + "\"";
} bool do_test(vector<string> board, int __expected) {
time_t startClock = clock();
CutTheNumbers *instance = new CutTheNumbers();
int __result = instance->maximumSum(board);
double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC;
delete instance; if (__result == __expected) {
cout << "PASSED!" << " (" << elapsed << " seconds)" << endl;
return true;
}
else {
cout << "FAILED!" << " (" << elapsed << " seconds)" << endl;
cout << " Expected: " << to_string(__expected) << endl;
cout << " Received: " << to_string(__result) << endl;
return false;
}
} int run_test(bool mainProcess, const set<int> &case_set, const string command) {
int cases = , passed = ;
while (true) {
if (next_line().find("--") != )
break;
vector<string> board;
from_stream(board);
next_line();
int __answer;
from_stream(__answer); cases++;
if (case_set.size() > && case_set.find(cases - ) == case_set.end())
continue; cout << " Testcase #" << cases - << " ... ";
if ( do_test(board, __answer)) {
passed++;
}
}
if (mainProcess) {
cout << endl << "Passed : " << passed << "/" << cases << " cases" << endl;
int T = time(NULL) - ;
double PT = T / 60.0, TT = 75.0;
cout << "Time : " << T / << " minutes " << T % << " secs" << endl;
cout << "Score : " << * (0.3 + (0.7 * TT * TT) / (10.0 * PT * PT + TT * TT)) << " points" << endl;
}
return ;
} int main(int argc, char *argv[]) {
cout.setf(ios::fixed, ios::floatfield);
cout.precision();
set<int> cases;
bool mainProcess = true;
for (int i = ; i < argc; ++i) {
if ( string(argv[i]) == "-") {
mainProcess = false;
} else {
cases.insert(atoi(argv[i]));
}
}
if (mainProcess) {
cout << "CutTheNumbers (1000 Points)" << endl << endl;
}
return run_test(mainProcess, cases, argv[]);
}
// 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. RabbitMQ 异常与任务分发

    异常情况处理 上篇最后提到了这个问题, consumer异常退出.queue出错.甚至rabbitMQ崩溃.因为它们都是软件 ,软件都会有bug,这是无法避免的.所以RabbitMQ在设计的时候也想到 ...

  2. Request 对象

    Request 对象用于检索从浏览器向服务器发送的请求中的信息. 1.使用Request对象的Browser属性,可以访问HttpBrowserCapabilities属性获得当前正在使用哪种类型的浏 ...

  3. MySQL时间函数,用起来比PHP还爽

    前一篇写了PHP的时间函数(还是草稿),这一篇就写Mysql的时间函数吧.最近做的项目,关乎权限,于是自然而然的就与有效期联系在了一起.其中有一个功能是生成特殊表格,可以根据用户的选择,按周.月.季. ...

  4. Mysql学习笔记(三)对表数据的增删改查。

    正文内容. 这一部分是最简单的,也是最麻烦的.简单是因为其实只包括增删该插四个部分.大体上看,增加数据.删除数据.修改数据.查询数据都不麻烦啊,我们日常都是常用的.这个谁不会呢?以前在培训机构学mys ...

  5. 京东分布式MySQL集群方案介绍

    背景 数据库作为一个非常基础的系统,任何一家互联网公司都会使用,数据库产品也很多,有Oracle.SQL Server .MySQL.PostgeSQL.MariaDB等,像SQLServer/Ora ...

  6. Jquery 基本知识(二)

    一.数据类型 5种基本数据类型:Null/Undefined/String/Boolean/Number 1种复杂数据类型:Object 二.数据类型检测 1.typeof 2.instanceof/ ...

  7. string.capwords() 将每个单词首字母大写

    string.capwords() 将每个单词首字母大写 代码: import string s = ' The quick brown fox jumped over the lazy dog. ' ...

  8. zepto.js之ajax剖析

    1.ajax的baseHeaders ajax插件中的baseHeaders对象的是http请求头部的信息 var mime = settings.accepts[dataType], baseHea ...

  9. Xcode如何编译Debug版和Release版

    在Run和Stop按钮的右边有一个工程名 点击工程名,选择Manage Schemes 选择Edit... 左侧选择Run ProjectName.app 右侧选择Info页,在Build Confi ...

  10. Which hashing algorithm is best for uniqueness and speed?

    http://programmers.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness- ...