SRM 667 DIV2 OrderOfOperationsDiv2 500-point
2015-10-08 09:26
411 查看
Problem Statement
Cat Noku has just finished writing his first computer program. Noku’s computer has m memory cells. The cells have addresses 0 through m-1. Noku’s program consists of n instructions. The instructions have mutually independent effects and therefore they may be executed in any order. The instructions must be executed sequentially (i.e., one after another) and each instruction must be executed exactly once.You are given a description of the n instructions as a vector with n elements. Each instruction is a string of m characters. For each i, character i of an instruction is ‘1’ if this instruction accesses memory cell i, or ‘0’ if it does not.
Noku’s computer uses caching, which influences the time needed to execute an instruction. More precisely, executing an instruction takes k^2 units of time, where k is the number of new memory cells this instruction accesses. (I.e., k is the number of memory cells that are accessed by this instruction but have not been accessed by any previously executed instruction. Note that k may be zero, in which case the current instruction is indeed executed in 0 units of time.)
Noku’s instructions can be executed in many different orders. Clearly, different orders may lead to a different total time of execution. Find and return the shortest amount of time in which it is possible to execute all instructions.
Definition
Class:
OrderOfOperationsDiv2
Method:
minTime
Parameters:
vector
Returns:
int
Method signature:
int minTime(vector s)
(be sure your method is public)
Limits
Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256
Constraints
n,m will be between 1 and 20, inclusive.
s will have exactly n elements.
Each element of s will have exactly m characters.
Each character of s[i] will be either ‘0’ or ‘1’ for all valid i.Examples
0)
{
“111”,
“001”,
“010”
}
Returns: 3
Cat Noku has 3 instructions. The first instruction (“111”) accesses all three memory cells. The second instruction (“001”) accesses only memory cell 2. The third instruction (“010”) accesses only memory cell 1. If Noku executes these three instructions in the given order, it will take 3^2 + 0^2 + 0^2 = 9 units of time. However, if he executes them in the order “second, third, first”, it will take only 1^2 + 1^2 + 1^2 = 3 units of time. This is one optimal solution. Another optimal solution is to execute the instructions in the order “third, second, first”.
1)
{
“11101”,
“00111”,
“10101”,
“00000”,
“11000”
}
Returns: 9
2)
{
“11111111111111111111”
}
Returns: 400
A single instruction that accesses all 20 memory cells.
3)
{
“1000”,
“1100”,
“1110”
}
Returns: 3
4)
{
“111”,
“111”,
“110”,
“100”
}
Returns: 3
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.
My Solution
#include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <cstring> using namespace std; class OrderOfOperationsDiv2 { public: int minTime(vector <string> s); private: int rec(int mask); vector <int> data; int n, m, dp[(1<< 20) + 5]; }; int OrderOfOperationsDiv2::minTime(vector <string> s) { n = s.size(); m = s[0].size(); memset(dp, 0xff, sizeof(dp)); for (int i = 0; i < n; ++i) { //将字符串表示的二进制表示为实际的二进制整数 //由于m 不超过20,所以32位int足以表示 int mask = 0; for (int j = 0; j < m; ++j) if (s[i][j] == '1') mask |= (1 << j); data.push_back(mask); } return rec(0); } int OrderOfOperationsDiv2::rec(int mask) { if (mask == (1 << n) - 1) return 0; int &temp = dp[mask]; if (temp != -1) return temp; temp = 1 << 30; int used = 0; for (int i = 0; i < n; ++i) if (mask & (1 << i)) used |= data[i]; for (int i = 0; i < n; ++i) if (!(mask & (1 << i))) { int k = (~used) & data[i]; k = __builtin_popcount(k); temp = min(temp, rec(mask | (1 << i)) + k * k); } return temp; }
相关文章推荐
- 概率最大骰子总和 Topcoder SRM 536 DIV1 第2题
- TopCoder SRM 569 DIV2 Level3: MegaFactorialDiv2
- Topcoder SRM Div2 Level2
- Topcoder SRM 547 DIV1 Level 1: Pillars
- TopCoder SRM DIV2 Level 3: RelativelyPrimeSubset
- TopCoder SRM 558 DIV2 Level 3:CatAndRabbit
- Topcoder SRM642 TaroCutting
- topcoder arena 插件配置
- Convert from char to int OR Convert from int to char
- Topcoder Open 2014 Algorithm Round 1A
- TopCoder SRM 593: MayTheBestPetWin 势均力敌的赛跑
- TopCoder SRM 610: The Matrix 区分现实与梦境的棋盘
- Topcoder/SRM565
- TOPCODER/SRM565 DIVII 250、500pt(500pt无递归解法)
- TOPCODER/SRM 566 DIVII(250、500、1000题)(1000PT暂未附上代码)
- TOPCODE/SRM567 DIVII 250、500PT
- ACM-计算几何 #Topcoder #SRM187-DIV2 PointInPolygon
- TopCoder SRM 144 DIV2(200-point)
- TopCoder SRM 144 DIV2(550-point)
- ZigZag