topcoder srm 600 div1
problem1 link
首先,如果一个数字的某一位是1但是$goal$的这一位不是1,那么这个数字是不用管它的。那么对于剩下的数字,只需要统计在$goal$为1的位上,这些数字对应位上也是1的数字个数。所有这样的位取最小值即可。这些数字就是要都被删除的。
problem2 link
首先暴力枚举哪些行是最后回文的行。然后对于列来说,将其对称折叠成$\frac{m}{2}$列,每一列可以选择0、1、2个,最后选出$columnCount$列。这个可以动态规划。
problem3 link
考虑一条线一条线添加进去。
每次添加一条线所增加的区域等于1加上这条线与已有的线交点的个数。如果多个线交于一点那么这个点只能算一次。
对于一条线$y=ax+b$,只需要考虑以下这些直线:$y=a^{'}x+b^{'},0\leq a^{'}<a,0\leq b^{'}<B$
交点为横坐标为$x=\frac{b^{'}-b}{a-a^{'}}=\frac{p}{q},-b\leq p<B-b,1\leq q\leq a$
可以将$p$分为三段:$-b\leq p<0,p=0,0<p<B-b$
如果当前直线与之前的两条线相交于一点,那么有$\frac{p_{1}}{q_{1}}=\frac{p_{2}}{q_{2}}$,那么只需要考虑$p_{i},q_{i}$互质的那一组即可。由于$q$的连续性,必有一组是互质的。
令$f[a][b]=\sum_{i=1}^{a}\sum_{j=1}^{b}[Gcd(i,j)=1]$
所以答案为$f[a][b]+1+f[a][B-b-1]$表示上面分成的三段。
code for problem1
#include <algorithm>
#include <vector> class ORSolitaire {
public:
int getMinimum(const std::vector<int> &numbers, int goal) {
std::vector<int> b(30);
for (auto x : numbers) {
if ((goal & x) == x) {
for (int i = 0; i < 30; ++i) {
if ((x & (1 << i)) != 0) {
++b[i];
}
}
}
}
int result = -1;
for (int i = 0; i < 30; ++i) {
if ((goal & (1 << i)) != 0) {
if (result == -1 || result > b[i]) {
result = b[i];
}
}
}
return result;
}
};
code for problem2
#include <string>
#include <vector> class PalindromeMatrix {
public:
int minChange(const std::vector<std::string> &A, int rowCount,
int columnCount) {
int n = static_cast<int>(A.size());
int m = static_cast<int>(A[0].size());
int result = n * m; for (int mask = 0; mask < (1 << n); ++mask) {
std::vector<int> rows;
for (int i = 0; i < n; ++i) {
if ((mask & (1 << i)) != 0) {
rows.push_back(i);
}
}
if (static_cast<int>(rows.size()) == rowCount) {
result = std::min(result, Compute(rows, A, columnCount, n, m));
}
}
return result;
} private:
int Compute(const std::vector<int> &rows, const std::vector<std::string> &A,
int column, int n, int m) {
auto Cost = [&](int c1, int c2, int tag) {
std::vector<int> visited(n);
std::vector<int> row_hash(n);
for (auto r : rows) {
row_hash[r] = 1;
}
int result = 0;
for (auto r : rows) {
if (visited[r] == 1) {
continue;
}
int c[2] = {0, 0};
visited[r] = 1;
++c[A[r][c1] - '0'];
++c[A[r][c2] - '0'];
if (tag == 1) {
++c[A[n - 1 - r][c1] - '0'];
visited[n - 1 - r] = 1;
if (row_hash[n - 1 - r] == 1) {
++c[A[n - 1 - r][c2] - '0'];
}
} else if (tag == 2) {
++c[A[n - 1 - r][c2] - '0'];
visited[n - 1 - r] = 1;
if (row_hash[n - 1 - r] == 1) {
++c[A[n - 1 - r][c1] - '0'];
}
} else if (tag == 3) {
++c[A[n - 1 - r][c2] - '0'];
++c[A[n - 1 - r][c1] - '0'];
visited[n - 1 - r] = 1;
}
result += std::min(c[0], c[1]);
} for (int i = 0; i < n / 2; ++i) {
if (visited[i] == 0) {
if ((tag & 1) == 1 && A[i][c1] != A[n - 1 - i][c1]) {
++result;
}
if ((tag & 2) == 2 && A[i][c2] != A[n - 1 - i][c2]) {
++result;
}
}
}
return result;
};
std::vector<std::vector<int>> f(m >> 1, std::vector<int>(column + 1, -1));
auto Update = [&](int i, int j, int cost) {
if (j <= column && (f[i][j] == -1 || f[i][j] > cost)) {
f[i][j] = cost;
}
};
Update(0, 0, Cost(0, m - 1, 0));
Update(0, 1, std::min(Cost(0, m - 1, 1), Cost(0, m - 1, 2)));
Update(0, 2, Cost(0, m - 1, 3)); for (int i = 1; i < (m >> 1); ++i) {
for (int j = 0; j <= column; ++j) {
if (f[i - 1][j] == -1) {
continue;
}
Update(i, j, f[i - 1][j] + Cost(i, m - 1 - i, 0));
Update(i, j + 1, f[i - 1][j] + std::min(Cost(i, m - 1 - i, 1),
Cost(i, m - 1 - i, 2)));
Update(i, j + 2, f[i - 1][j] + Cost(i, m - 1 - i, 3));
}
}
return f[m / 2 - 1][column];
}
};
code for problem3
constexpr int kMax = 1200;
int table[kMax][kMax]; class LotsOfLines {
public:
long long countDivisions(int A, int B) {
Initialize(A, B);
long long result = B + 1;
for (int a = 1; a < A; ++a) {
for (int b = 0; b < B; ++b) {
result += 2 + table[a][b] + table[a][B - 1 - b];
}
}
return result;
} private:
void Initialize(int A, int B) {
for (int i = 1; i < A; ++i) {
for (int j = 1; j < B; ++j) {
int t = Gcd(i, j) == 1 ? 1 : 0;
table[i][j] =
table[i - 1][j] + table[i][j - 1] - table[i - 1][j - 1] + t;
}
}
} int Gcd(int x, int y) { return y == 0 ? x : Gcd(y, x % y); }
};
topcoder srm 600 div1的更多相关文章
- Topcoder SRM 600 div1题解
日常TC计划正式启动! Easy(250pts): 题目大意:给你一个集合,里面一堆数,初始数为0,给你一个目标数,你可以选择集合中若干个数进行OR操作来得到目标数.问至少删去多少个数,使得你永远无法 ...
- Topcoder SRM 643 Div1 250<peter_pan>
Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...
- Topcoder Srm 726 Div1 Hard
Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...
- Topcoder SRM 584 DIV1 600
思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...
- TopCoder SRM 722 Div1 Problem 600 DominoTiling(简单插头DP)
题意 给定一个$12*12$的矩阵,每个元素是'.'或'X'.现在要求$1*2$的骨牌铺满整个矩阵, 'X'处不能放置骨牌.求方案数. 这道题其实和 Uva11270 是差不多的,就是加了一些条件. ...
- topcoder srm 714 div1
problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...
- topcoder srm 738 div1 FindThePerfectTriangle(枚举)
Problem Statement You are given the ints perimeter and area. Your task is to find a triangle wi ...
- Topcoder SRM 602 div1题解
打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...
- Topcoder SRM 627 div1 HappyLettersDiv1 : 字符串
Problem Statement The Happy Letter game is played as follows: At the beginning, several players ...
随机推荐
- ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
约束字段为自动增长,被约束的字段必须同时被key约束 没有设置成primary key 时,会报错. 加上primary key 则设置成功.
- Web开发(XAMPP服务器搭建)
XAMPP是一个功能强大的搭建服务器环境的软件集成包.它集成了Apache.MySql.php.perl这几个服务器常用的软件.而我们在使用时,省去了安装这些软件的步骤,只需要下载XAMPP,解压缩. ...
- RxSwift + Moya + ObjectMapper
https://www.jianshu.com/p/173915b943af use_frameworks! target 'RXDemo' do pod 'RxSwift' pod 'RxCocoa ...
- Redis单机多节点集群实验
第一步:安装Redis 前面已经安装过了 不解释, Reids安装包里有个集群工具,要复制到/usr/local/bin里去 cp redis-3.2.9/src/redis-trib.rb /usr ...
- Mybatis插入记录并返回MySQL自增主键
mapper Integer insertConfigAndGetId(CrawlerConfig config); xml <insert id="insertConfigAndGe ...
- 《图解HTTP》读书笔记(二:各种协议与HTTP协议之间的关系)
涉及到DNS协议.TCP协议.IP协议,话不多说,上图:
- jQuery 选择具有特殊属性的元素
如今有这样一种需求,须要选出全部有背景图片的元素. 这个问题有点棘手.我们无法使用选择表达式来完毕这个问题了. 使用jQuery的DOM过滤方法filter(),能够依据函数中表达的不论什么条件选择元 ...
- mysql5安装
一.MYSQL的安装 1.打开下载的mysql安装文件mysql-5.0.27-win32.zip,双击解压缩,运行“setup.exe”. 2.选择安装类型,有“Typical(默认)”.“Comp ...
- 本地Linux备份服务器[Client]定期备份云服务器[Server]上的文件(下)
https://www.cnblogs.com/kevingrace/p/5972563.html Client上使用rsync命令查看服务器上文件列表rsync --list-only -e &qu ...
- datetime模块处理时间
python常用的处理时间的库有:datetime,time,calendar.datetime库包括了date(储存日期:(年.月.日),time(储存时间:(小时.分.秒和微秒),timedelt ...