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. 如何使用Visual Studio构建libiconv

    参考博文:How to Build libiconv with Microsoft Visual Studio - CodeProject libiconv源码下载地址:libiconv - GNU ...

  2. noi2014魔法森林

    为了得到书法大家的真传,小 E 同学下定决心去拜访住在魔法森林中的隐 士.魔法森林可以被看成一个包含

  3. android adb 源码框架分析(1 系统)【转】

    本文转载自:http://blog.csdn.net/luansxx/article/details/25203269 ‘ Adb模块包括adb,adbd,源代码都在system/core/adb目录 ...

  4. atexit函数的使用【学习笔记】

    #include "apue.h" static void my_exit1(void); static void my_exit2(void); int main(void) { ...

  5. HDU3507 Print Article —— 斜率优化DP

    题目链接:https://vjudge.net/problem/HDU-3507 Print Article Time Limit: 9000/3000 MS (Java/Others)    Mem ...

  6. ewasm项目初探

    为了改进EVM1.0,以太坊的新一代虚拟机项目ewasm (github.com/ewasm)将支持WebAssembly(wasm),wasm在性能,扩展性,开发工具,社区都更有优势.除以太坊外,一 ...

  7. JavaScript页面刷新与弹出窗口问题的解决方法

    1. [代码][JavaScript]代码     ​一.无提示刷新网页 大家有没有发现,有些网页,刷新的时候,会弹出一个提示窗口,点“确定”才会刷新.而有的页面不会提示,不弹出提示窗口,直接就刷新了 ...

  8. Go语言的管道Channel用法

    本文实例讲述了Go语言的管道Channel用法.分享给大家供大家参考.具体分析如下: channel 是有类型的管道,可以用 channel 操作符 <- 对其发送或者接收值. ch <- ...

  9. mysql忘记root用户密码找回步骤

    修改或找回root密码步骤1.修改MySQL的登录设置: # vi /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 保存并且退出vi. 2.重新启动mys ...

  10. skynet源码阅读<1>--lua与c的基本交互

    阅读skynet的lua-c交互部分代码时,可以看到如下处理: struct skynet_context * context = lua_touserdata(L, lua_upvalueindex ...