TopCoder SRM 667 Div.2题解
概览:
T1 枚举 T2 状压DP T3 DP
TopCoder SRM 667 Div.2 T1
解题思路
由于数据范围很小,所以直接枚举所有点,判断是否可行。时间复杂度O(δX × δY),空间复杂度O(1)。
参考程序段
#include <bits/stdc++.h>
using namespace std;
class PointDistance {
public:
vector <int> findPoint( int x1, int y1, int x2, int y2 );
};
int sqr(int x){ return x * x; }
vector<int> ans;
vector <int> PointDistance::findPoint(int x1, int y1, int x2, int y2) {
ans.clear();
for(int x = -100; x <= 100; x++)
for(int y = -100; y <= 100; y++)
if(sqr(x1 - x) + sqr(y1 - y) > sqr(x2 - x) + sqr(y2 - y)){
ans.push_back(x);
ans.push_back(y);
return ans;
}
return ans;
}
TopCoder SRM 667 Div.2 T2
解题思路
可能大家的第一反应都是每一步选花费最小来贪心,但是发现,某一步有多个花费最小时,会出问题。
我们看一组数据
0000
1100
0011
0111
如果贪心, 可能顺序是1,2,3,4, 这样答案为8, 而事实上按顺序1,3,4,2,最优,为6。
于是我们考虑DP。
首先,我们令dp[i]表示在i的二进制表示下为1的那些指令处理完毕所需的最小花费。则可以从任何二进制表示下比i少一个1的j转移过来,我们取其最小。开始状态为dp[0] = 0,结束状态为dp[(1 << n) - 1]。
参考程序段
程序中,并非枚举dp[j]向dp[i]转移,而是对于dp[j]向每个可能的dp[i]转移,并使用了类似于spfa的写法。
同时,程序中q数组保存的数指向dp数组的下标,q_used对应花费。
#include <bits/stdc++.h>
using namespace std;
class OrderOfOperationsDiv2 {
public:
int minTime( vector <string> s );
};
int n;
int rec[20];
int l, r, q[1 << 23], q_used[1 << 23];
int dp[1 << 21];
int len;
int OrderOfOperationsDiv2::minTime(vector <string> s) {
n = s.size();
len = s[0].size();
for(int i = 0; i < n; i++){
rec[i] = 0;
for(int j = 0; j < len; j++)
if(s[i][j] == '1') rec[i] |= (1 << j);//转成二进制方便处理
}
memset(dp, 255, sizeof(dp));
dp[0] = 0;
l = 0; r = 1; q[1] = 0; q_used[1] = 0;
while(l < r){
l++;
int rec_vis = q[l];
int rec_used = q_used[l];
for(int i = 0; i < n; i++){//枚举可能被更新的状态
if((rec_vis >> i) & 1) continue;
int x = rec_vis | (1 << i);
int y = rec_used | rec[i];
int tt = 0;
for(int j = 0; j < len; j++)
if(((y >> j) & 1) == 1 && ((rec_used >> j) & 1) == 0) tt++;
tt = tt * tt;
if(dp[x] == -1 || dp[x] > dp[rec_vis] + tt){
dp[x] = dp[rec_vis] + tt;
r++;
q[r] = x;
q_used[r] = y;
}
}
}
return dp[(1 << n) - 1];
}
TopCoder SRM 667 Div.2 T3
解题思路
乍一看好像很麻烦呐。看起来有后效性?仿佛不能用dp?唉?然而数据只有30?所以考虑以下乱搞?
emmm
我们还是dp,大不了扩大维数,增加状态数。嗯。我们令dp[i][j][k]表示前i幢楼,在第i幢楼这里开了j家店,同时计划在下一幢楼开k家能获得的利益。那么我们可以得到状态方程
\]
特判一下第一栋楼和最后一栋楼。
同时注意下小细节
1、题目里并没有给出c[-1]的定义,所以任意相邻三栋楼中一定要开一家:)
2、只有一栋楼的情况:)
时间复杂度O(n^4), 空间复杂度O(n^3)【这个我觉得还是叫暴力比较好:捂脸:】
参考代码段
#include <bits/stdc++.h>
using namespace std;
class ShopPositions {
public:
int maxProfit( int n, int m, vector <int> c );
};
int f[40][40][40];
int ShopPositions::maxProfit(int n, int m, vector <int> c) {
memset(f, 0, sizeof(f));
int ans = 0;
for(int j = 0; j <= m; j++)//特判第一栋楼
for(int k = 0; k <= m; k++){
if(j + k == 0) continue;
f[0][j][k] = max(f[0][j][k], j * c[0 * 3 * m + (j + k) - 1]);
ans = max(ans, f[0][j][k]);
}
for(int i = 1; i < n - 1; i++)
for(int j = 0; j <= m; j++)
for(int k = 0; k <= m; k++)//O(n^4)DP
for(int l = 0; l <= m; l++){
if(j + k + l == 0) continue;
f[i][j][k] = max(f[i][j][k], f[i - 1][l][j] + j * c[i * 3 * m + (j + k + l) - 1]);
ans = max(ans, f[i][j][k]);
}
if(n > 1)//如果不止一栋楼,就特判最后一栋楼
for(int j = 0; j <= m; j++)
for(int l = 0; l <= m; l++){
if(j + l == 0) continue;
f[n - 1][j][0] = max(f[n - 1][j][0], f[n - 2][l][j] + j * c[(n - 1) * 3 * m + (j + l) - 1]);
ans = max(ans, f[n - 1][j][0]);
}
return ans;
}
TopCoder SRM 667 Div.2题解的更多相关文章
- Topcoder SRM 674 Div.2题解
T1 解题思路 这题应该不是很难,主要是题意理解问题. 注意给出的两个数组里映射关系已经对应好了,只要判断是否为双射即可 参考程序 #include <bits/stdc++.h> usi ...
- TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E
传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...
- [topcoder]SRM 633 DIV 2
第一题,http://community.topcoder.com/stat?c=problem_statement&pm=13462&rd=16076 模拟就可以了. #includ ...
- TopCoder SRM 596 DIV 1 250
body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...
- [topcoder]SRM 646 DIV 2
第一题:K等于1或者2,非常简单.略.K更多的情况,http://www.cnblogs.com/lautsie/p/4242975.html,值得思考. 第二题:http://www.cnblogs ...
- 【topcoder SRM 702 DIV 2 250】TestTaking
Problem Statement Recently, Alice had to take a test. The test consisted of a sequence of true/false ...
- Topcoder SRM 656 (Div.1) 250 RandomPancakeStack - 概率+记忆化搜索
最近连续三次TC爆零了,,,我的心好痛. 不知怎么想的,这题把题意理解成,第一次选择j,第二次选择i后,只能从1~i-1.i+1~j找,其实还可以从j+1~n中找,只要没有被选中过就行... [题意] ...
- Topcoder SRM 648 (div.2)
第一次做TC全部通过,截图纪念一下. 终于蓝了一次,也是TC上第一次变成蓝名,下次就要做Div.1了,希望div1不要挂零..._(:зゝ∠)_ A. KitayutaMart2 万年不变的水题. # ...
- Topcoder SRM 145 DIV 1
Bonuses 模拟 题意:给你一个序列,按照百分比排序,再将百分比取整,再把剩余百分比加到最大的那几个. 题解:按照题意模拟就好.
随机推荐
- SqlServer中#和##的区别
本地临时表的名称以单个数字字符(#)开头,它们仅对当前的用户连接是可见的. 全局临时表的名称以两个数字字符(##)开头,创建后对任何用户都是可见的.
- 纯CSS3绘制神奇宝贝伊布动画特效
在线演示 本地下载
- Java——BufferedImage对象
BufferedImage对象中最重要的两个组件是Raster与ColorModel,分别用于存储图像的像素数据和颜色数据. 1.Raster对象的作用与像素存储 BufferedImage支持从Ra ...
- Javassist操作方法总结
CSDN参考Javassist tutorial 1.读取和输出字节码 ClassPool pool = ClassPool.getDefault(); //会从classpath中查询该类 CtCl ...
- 使用@ResponseBody输出JSON
添加jackson依赖 添加@ResponseBody 测试: 原理: 当一个处理请求的方法标记为@ResponseBody时,就说明该方法需要输出其他视图(json.xml),SpringMVC通过 ...
- 深入理解hadoop之机架感知
深入理解hadoop之机架感知 机架感知 hadoop的replication为3,机架感知的策略为: 第一个block副本放在和client所在的datanode里(如果client不在集群范围内, ...
- postgres导入和导出
导出整个数据库: pg_dump -h 127.0.0.1 -U zhang mydb >mydb_dum.sql 导出某个表: pg_dump -h 127.0.0.1 -U zhang my ...
- Linux umask 档案预设权限/touch 建立空档案或修档案件时间
1 建立档案时,权限一般设为-rw-r--r-- 2 建立目录时,权限一般设为drwxr-xr-x 3 touch 选项与参数 -a : 仅修订access time -c :仅修改档案的时间,若该 ...
- maven中配置jboss仓库
有两种方式,一种是在项目的pom.xml中<repositories>中添加,这是配置是针对具体的某一个项目,更多时候,我们想把jboss仓库作为所有项目的仓库,这就需要在maven的se ...
- 配置Nexus为maven的私服
1.配置Nexus为maven的私服 第一种方式:在项目的POM中如下配置 <repositories> <repository> <id>nexus_public ...