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. Tomcat配置,Myeclipse破解和各种设置

    转自:http://www.cnblogs.com/tyjsjl/archive/2006/11/14/2156111.html 根据tomcat来配置eclipse和MyEclipse结合使用起来, ...

  2. 宠物连连看2完整Android代码项目

    宠物连连看2完整代码,该源代码支持多种风格的连连看游戏的,如有国旗类的连连看,还有宠物连连看的等,主要的功能实现了无尽关卡挑战模式.还有催命倒计时,以及链接提示,暂停.多样图片集,挑战眼力和速度等,而 ...

  3. 在HDInsight中的Hadoop介绍

    在HDInsight中的Hadoop介绍 概览 Azure的HDInsight是,部署和规定的Apache™Hadoop®集群在云中,提供用于管理,分析和大数据报告软件框架中的服务. 大数据 数据被描 ...

  4. leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

    1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...

  5. reviews of learn python3 the hard way

    Almost every time,I try my best to write a long review of the book I have read. But this time I want ...

  6. 2016/05/27 php上传文件常见问题总结

    php上传文件常见问题总结 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2015-02-03我要评论 这篇文章主要介绍了php上传文件常见问题总结,基本上经常碰到的问题的处理都列了 ...

  7. 前端遇上Go: 静态资源增量更新的新实践

    前端遇上Go: 静态资源增量更新的新实践https://mp.weixin.qq.com/s/hCqQW1F8FngPPGZAisAWUg 前端遇上Go: 静态资源增量更新的新实践 原创: 洋河 美团 ...

  8. jQuery.ajaxSetup()

    jQuery.ajaxSetup()函数用于设置AJAX的全局默认设置. 该函数用于更改jQuery中AJAX请求的默认设置选项.之后执行的所有AJAX请求,如果对应的选项参数没有设置,将使用更改后的 ...

  9. 获取浏览器屏幕高度(js,jq)

    javascript IE中:document.body.clientWidth ==> BODY对象宽度document.body.clientHeight ==> BODY对象高度do ...

  10. vue开发购物车,解决全选单选问题

    实现全选单选,在vue中无法通过this获取input中的checkbox的checked属性,但是可以通过vue对input的特殊方式v-model来实现对应数据的绑定,同样也可以通过这种方式实现购 ...