topcoder srm 590 div1 (max_flow_template)
problem1 link
对于每一个,找到其在目标串中的位置,判断能不能移动即可。
problem2 link
如果最后的$limit$为$11=(1011)_{2}$,那么可以分别计算值为$(1011)_{2},(1010)_{2},(100x)_{2},(0xxx)_{2}$的答案数,$x$位置表示可以为任意。也就是可以忽略这些位。
当答案固定时,可以用高斯消元求解。
problem3 link
令$d(i,j)$表示点 $i$到点$j$的距离。
使用最小割求解。将每个点拆成$n$个点,第$i$个点拆成$p(i,0),p(i,1),...,p(i,n-1)$.其中$p(i,j)$如果与源点在一个割集,说明$d(0,i)\leq j$为假,与汇点在一个割集说明$d(0,i)\leq j$为真。
所以,如果一个割产生在边$p(i,j-1)\rightarrow p(i,j)$说明最后$d(0,i)=j$.其中边$p(i,j-1)\rightarrow p(i,j)$的代价为$(want[i]-j)^{2}$
有以下边:
(1)对于0点来说,$p(0,0)$与汇点的边流量为无穷大,说明,最后它与汇点在一个割集,所以$d(0,0)\leq 0$为真;
(2)对于$1\leq i < n$号点来说,源点到$p(i,0)$为无穷大(一定不可能),$p(i, n-1)$到汇点为无穷大(一定为真); $p(i,j-1)\rightarrow p(i,j),1\leq j < n)$为$(want[i]-j)^{2}$
(3)如果原来存在边$i\rightarrow j$,那么$p(i,k)\rightarrow p(j,k-1)$为无穷大,表示如果$d(0,i)>k$,那么一定有$d(0,j)>k-1$
code for problem1
#include <string> class FoxAndChess {
public:
std::string ableToMove(const std::string &begin, const std::string &target) {
int n = static_cast<int>(begin.size());
int idx = 0;
for (int i = 0; i < n; ++i) {
if (begin[i] == 'L' || begin[i] == 'R') {
while (idx < n && target[idx] == '.') {
++idx;
}
if (idx == n || begin[i] != target[idx] ||
(begin[i] == 'L' && i < idx) || (begin[i] == 'R' && i > idx)) {
return "Impossible";
}
++idx;
}
}
while (idx < n && target[idx] == '.') {
++idx;
}
if (idx != n) {
return "Impossible";
}
return "Possible";
}
};
code for problem2
#include <vector> class XorCards {
public:
long long numberOfWays(const std::vector<long long> &number,
long long limit) {
const int n = 52;
const int m = static_cast<int>(number.size());
long long result = 0;
auto GetBit = [](long long t, int b) -> int {
return (t & (1ll << b)) != 0 ? 1 : 0;
};
{
std::vector<std::vector<int>> g(n, std::vector<int>(m + 1, 0));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
g[i][j] = GetBit(number[j], i);
}
g[i][m] = GetBit(limit, i);
}
result += Gauss(g);
}
for (int i = 0; i < n; ++i) {
if (GetBit(limit, i) == 0) {
continue;
}
std::vector<std::vector<int>> g(n, std::vector<int>(m + 1, 0));
for (int j = i; j < n; ++j) {
for (int k = 0; k < m; ++k) {
g[j][k] = GetBit(number[k], j);
}
if (j > i) {
g[j][m] = GetBit(limit, j);
}
}
result += Gauss(g);
}
return result;
} private:
long long Gauss(std::vector<std::vector<int>> &g) {
int n = static_cast<int>(g.size());
int m = static_cast<int>(g[0].size()) - 1;
int col = 0; auto HasBit = [&](int start, int col) {
for (int i = n - 1; i >= start; --i) {
if (g[i][col] != 0) return i;
}
return -1;
};
int row_number = 0;
for (int i = 0; i < n && col < m; ++i) {
int row = HasBit(i, col);
while (row == -1 && col + 1 < m) {
row = HasBit(i, ++col);
}
if (row == -1) {
break;
}
if (row != i) {
std::swap(g[i], g[row]);
}
for (int idx = 0; idx < n; ++idx) {
if (idx != i && g[idx][col] != 0) {
for (int k = 0; k <= m; ++k) {
g[idx][k] ^= g[i][k];
}
}
}
++col;
++row_number;
}
for (int i = row_number; i < n; ++i) {
if (g[i][m] != 0) {
return 0;
}
}
if (m < row_number) {
return 0;
}
return 1ll << (m - row_number);
}
};
code for problem3
#include <limits>
#include <unordered_map>
#include <vector> template <typename FlowType>
class MaxFlowSolver {
static constexpr FlowType kMaxFlow = std::numeric_limits<FlowType>::max();
static constexpr FlowType kZeroFlow = static_cast<FlowType>(0);
struct node {
int v;
int next;
FlowType cap;
}; public:
int VertexNumber() const { return used_index_; } FlowType MaxFlow(int source, int sink) {
source = GetIndex(source);
sink = GetIndex(sink); int n = VertexNumber();
std::vector<int> pre(n);
std::vector<int> cur(n);
std::vector<int> num(n);
std::vector<int> h(n);
for (int i = 0; i < n; ++i) {
cur[i] = head_[i];
num[i] = 0;
h[i] = 0;
}
int u = source;
FlowType result = 0;
while (h[u] < n) {
if (u == sink) {
FlowType min_cap = kMaxFlow;
int v = -1;
for (int i = source; i != sink; i = edges_[cur[i]].v) {
int k = cur[i];
if (edges_[k].cap < min_cap) {
min_cap = edges_[k].cap;
v = i;
}
}
result += min_cap;
u = v;
for (int i = source; i != sink; i = edges_[cur[i]].v) {
int k = cur[i];
edges_[k].cap -= min_cap;
edges_[k ^ 1].cap += min_cap;
}
}
int index = -1;
for (int i = cur[u]; i != -1; i = edges_[i].next) {
if (edges_[i].cap > 0 && h[u] == h[edges_[i].v] + 1) {
index = i;
break;
}
}
if (index != -1) {
cur[u] = index;
pre[edges_[index].v] = u;
u = edges_[index].v;
} else {
if (--num[h[u]] == 0) {
break;
}
int k = n;
cur[u] = head_[u];
for (int i = head_[u]; i != -1; i = edges_[i].next) {
if (edges_[i].cap > 0 && h[edges_[i].v] < k) {
k = h[edges_[i].v];
}
}
if (k + 1 < n) {
num[k + 1] += 1;
}
h[u] = k + 1;
if (u != source) {
u = pre[u];
}
}
}
return result;
} MaxFlowSolver() = default; void Clear() {
edges_.clear();
head_.clear();
vertex_indexer_.clear();
used_index_ = 0;
} void InsertEdge(int from, int to, FlowType cap) {
from = GetIndex(from);
to = GetIndex(to);
AddEdge(from, to, cap);
AddEdge(to, from, kZeroFlow);
} private:
int GetIndex(int idx) {
auto iter = vertex_indexer_.find(idx);
if (iter != vertex_indexer_.end()) {
return iter->second;
}
int map_idx = used_index_++;
head_.push_back(-1);
return vertex_indexer_[idx] = map_idx;
} void AddEdge(int from, int to, FlowType cap) {
node p;
p.v = to;
p.cap = cap;
p.next = head_[from];
head_[from] = static_cast<int>(edges_.size());
edges_.emplace_back(p);
} std::vector<node> edges_;
std::vector<int> head_; std::unordered_map<int, int> vertex_indexer_;
int used_index_ = 0;
}; #include <string> class FoxAndCity {
public:
int minimalCost(const std::vector<std::string> linked,
const std::vector<int> want) {
constexpr int kMaxCapacity = 1000000;
int n = static_cast<int>(linked.size());
MaxFlowSolver<int> solver;
auto Index = [&](int u, int k) { return u * n + k; };
int source = -1;
int sink = -2;
for (int i = 0; i < n; ++i) {
if (i == 0) {
solver.InsertEdge(Index(i, 0), sink, kMaxCapacity);
continue;
} solver.InsertEdge(source, Index(i, 0), kMaxCapacity);
solver.InsertEdge(Index(i, n - 1), sink, kMaxCapacity);
for (int j = 1; j < n; ++j) {
solver.InsertEdge(Index(i, j - 1), Index(i, j),
(want[i] - j) * (want[i] - j));
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (linked[i][j] == 'Y') {
for (int k = 1; k < n; ++k) {
solver.InsertEdge(Index(i, k), Index(j, k - 1), kMaxCapacity);
}
}
}
}
return solver.MaxFlow(source, sink);
}
};
topcoder srm 590 div1 (max_flow_template)的更多相关文章
- 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 590
第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement Fox Ciel is going to play Gomoku with her friend ...
- SRM 590 DIV1
转载请注明出处,谢谢viewmode=contents">http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlov ...
- 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 ...
- Topcoder SRM 584 DIV1 600
思路太繁琐了 ,实在不想解释了 代码: #include<iostream> #include<cstdio> #include<string> #include& ...
随机推荐
- python DBUtils 线程池 连接 Postgresql(多线程公用线程池,DB-API : psycopg2)
一.DBUtils DBUtils 是一套允许线程化 Python 程序可以安全和有效的访问数据库的模块,DBUtils提供两种外部接口: PersistentDB :提供线程专用的数据库连接,并自动 ...
- 创建多线程Thread
创建一个线程: # 方式一from threading import Thread import time def func(n): time.sleep(1) print(n) t = Thread ...
- MySQL建表、插入语句等
不定时更新MySQL的一些基础语句以及出现过的问题 5.10 建表语句 CREATE TABLE `policy_landvalue` ( `id` ) NOT NULL AUTO_INCREMENT ...
- tomcat8.5.11安装教程
备份,省得以后自己又忘了. 1.下载tomcat 2.环境变量设置 打开环境变量设置 注意,要点击下面红框处的新建按钮,而不要点击上面的.新建系统变量: 注意,变量名一定得设置为CATALINA_HO ...
- 怎么把mkv转成mp4,有什么方法
Mkv怎样转换成MP4呢?mkv是一种开放标准的自由的容器和文件格式,是一种多媒体封装格式,能够在一个文件中容纳无限数量的视频.音频.图片或字幕轨道.所以其不是一种压缩格式,而是Matroska定义的 ...
- Codeforces 1090B - LaTeX Expert - [字符串模拟][2018-2019 Russia Open High School Programming Contest Problem B]
题目链接:https://codeforces.com/contest/1090/problem/B Examplesstandard input The most famous characters ...
- python-颜色显示
格式:\033[显示方式;字体色;背景色m......[\033[0m] ------------------------------------------- 字体色 | 背景色 | 颜色描述 -- ...
- ES6 字符串
拓展的方法 子串的识别 ES6 之前判断字符串是否包含子串,用 indexOf 方法,ES6 新增了子串的识别方法. includes():返回布尔值,判断是否找到参数字符串. startsWith( ...
- Visual Studio 2017使用Asp.Net Core构建Angular4应用程序
文章转载请著名出处:http://www.cnblogs.com/smallprogram 你需要了解的名词 1. NodeJS,这是一个基于Chrome V8 JavaScript引擎构建的Java ...
- vue 点击弹窗外框关闭弹框
https://blog.csdn.net/zjw0742/article/details/77822777 ready() { document.addEventListener('click', ...