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

  

状态压缩DP SRM 667 Div1 OrderOfOperations 250的更多相关文章

  1. hoj2662 状态压缩dp

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

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

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  3. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  4. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  5. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

  6. 状态压缩dp问题

    问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...

  7. BZOJ-1226 学校食堂Dining 状态压缩DP

    1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...

  8. Marriage Ceremonies(状态压缩dp)

     Marriage Ceremonies Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  9. HDU 1074 (状态压缩DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...

随机推荐

  1. Redis+EJB实现缓存(一)

        上篇博客大概的对Redis做了一个主要的了解.由于刚刚接触自己也不太明确.所以上篇博客写的乱七八糟的.这篇由于项目须要,学习了一下Redis和EJB集成. 如今脑子相对照较清晰了一些. 实现思 ...

  2. java随记2

    1.Arrays java8里新添加了parallelSort等parallel开头的方法,表示利用cpu并行的能力 2.面向对象 如果继承树里的某个类要被初始化时,系统将会同时初始化该类的所有父类 ...

  3. Kills all phantomjs instances, disregard of their origin python关闭进程

    Python/Linux quit() does not terminate PhantomJS process · Issue #767 · SeleniumHQ/selenium https:// ...

  4. Does Hadoop require SSH?

    https://wiki.apache.org/hadoop/FAQ#Does_Hadoop_require_SSH.3F Hadoop provided scripts (e.g., start-m ...

  5. Java 内存区域与内存溢出异常

    一.Java虚拟机内存划分 1.程序计数器 线程私有 可以看做是当前线程所执行的字节码的行号指示器.字节码解释器工作时是通过改变这个计数器的值来选取下一条需要执行的字节码指令. Java虚拟机是通过多 ...

  6. SVN回滚机制

    引子 工作中遇到一个新同事提交代码时不知怎么的出现了大面积的代码覆盖,由于对SVN也不是特别了解,就看着别人处理问题,自己也验证性的实践了一下,总结一下. 总结 svn每一次提交成功,都会有一个`编号 ...

  7. [Android6.0][RK3399] 修改默认按键 KEY-PAD 的功能【转】

    本文转载自:http://m.blog.csdn.net/dearsq/article/details/70175637 Platform: RK3399 OS: Android 6.0 Kernel ...

  8. Oracle:通过dbv查看数据文件是否有坏块

    我们备份的数据文件,可以通过oacle自带的dbv工具来查看是否是好的. 下面实验如下: 环境:oracle10.2.0.1 1.检查数据文件是否有坏块 [oracle@app orcl]$ dbv ...

  9. 四叉树 bnuoj

    点击打开题目链接 建树+广搜一棵树:最下面有更短代码(很巧妙). #include<iostream> #include<stdio.h> #include<queue& ...

  10. BZOJ_3963_[WF2011]MachineWorks_斜率优化+CDQ分治

    BZOJ_3963_[WF2011]MachineWorks_斜率优化+CDQ分治 Description 你是任意性复杂机器公司(Arbitrarily Complex Machines, ACM) ...