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 <string> 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: OrderOfOperations
Method: minTime
Parameters: vector <string>
Returns: int
Method signature: int minTime(vector <string> s)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 256
Stack limit (MB): 256

Constraints

- n will be between 1 and 50, inclusive.
- 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
 

题意:给n个01串,设计一种顺序,使得每次新出现的1的个数的平方和最小

分析:比赛时不知道是div1的题,以为暴力贪心可以过,结果被hack掉了。题解说没有充分的证明使用贪心是很有风险的,正解是用状态压缩DP。

收获:爆零还能涨分,TC真奇怪。

官方题解

int dp[(1<<20)+10];
int a[55]; class OrderOfOperations {
public:
int minTime( vector <string> s ) {
int n = s.size (), m = s[0].length ();
memset (a, 0, sizeof (a));
int tot = 0;
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
if (s[i][j] == '1') a[i] |= (1<<j);
}
tot |= a[i];
}
memset (dp, INF, sizeof (dp));
dp[0] = 0;
for (int i=0; i<(1<<m); ++i) {
for (int j=0; j<n; ++j) {
int x = i | a[j]; //从i状态转移到x的状态
int y = x - i; //表示新出现的1
int k = __builtin_popcount (y); //内置函数,快速得到二进制下1的个数
dp[x] = min (dp[x], dp[i] + k * k); //类似Bellman_Ford
}
} return dp[tot];
}
};

  

最新文章

  1. 【Java EE 学习 78 上】【数据采集系统第十天】【Service使用Spring缓存模块】
  2. ubuntu下安装JDK并搭建activeMQ
  3. oracle xmltype导入并解析Excel数据 (一)创建表与序
  4. ABAP字符串翻转
  5. Javascript正则表达式的初步学习
  6. C++ sort函数
  7. time_wait 过多 造成网络慢 实战
  8. ios 添加通用断点定位到异常点
  9. 第一次写博客,关于前端开发deMVC在js中的应用
  10. jackson - 生成jason工具-简单示例
  11. VS2010 express中改变VC Default include/lib/… 目录
  12. MT【332】椭圆正交变换
  13. 关于H5页面在iPhoneX适配
  14. RabbitMQ 分发到多Consumer(Publish/Subscribe)
  15. Sqoop找不到主类 Error: Could not find or load main class org.apache.sqoop.Sqoop
  16. caffe中google protobuf使用问题
  17. 动态规划法(三)子集和问题(Subset sum problem)
  18. 关于ListBox在Grid中无法充满的问题
  19. 【Android内存泄漏检测】LeakCanary使用总结
  20. HDU 2087 剪花布条(字符串匹配,KMP)

热门文章

  1. WPF DataGrid获取选择行的数据
  2. android控件之间事件传递
  3. 利用QBuffer和QLinkedList做数据块存储队列
  4. 简易SQL语句
  5. date 命令 时间戳到标准格式转换
  6. Spring Boot2.0之整合Redis
  7. Ubuntu安装mycli,让mysql命令行可以自动提示
  8. 查看JVM运行时堆内存
  9. Laravel 新增的Switch模板控制语句非常不错
  10. 多线程-threading模块3