topcoder srm 570 div1
problem1 link
找到周期,每个周期的增量是相同的.
problem2 link
对于分给某一个公司的有$c$个联通分量,其中$k$个联通分量只有1个节点,$c$个联通分量一共有$x$个节点.首先,对于那些节点大于1的联通分量($c-k$个),将这些连接在一起需要$c-k-1$条边,耗费了$2(c-k-1)$个节点.还有$x-k-2(c-k-1)$个节点可以用来连接那些只有1个节点的联通分量,分两种情况:
(1)$k \leq x-k-2(c-k-1)$.这时候不需要额外的代价.
(2)$k>x-k-2(c-k-1)$.这时候,需要代价为$k-(x-k-2(c-k-1))=2c+2-x$
可以看出与$k$无关.那么可以计算出选出$c$个联通分量恰好有$x$个节点的方案数.由于对称,最后乘以2再除以总的方案数$2^{n}$就是答案.
problem3 link
第一步,将格子进行黑白染色:

第二步,黑白染色之后,格子就分成了两类,蓝色和绿色.每个格子向相邻的格子连边.新增源点汇点.源点向绿色格子连边,流量为2,蓝色格子向汇点连边,流量为2.如果最大流等于$2n$($n$为非W节点的个数),那么存在一种方案连接所有格子:

第三步,考虑是否可以满足所有有效格子里都是弯曲的形状,那么进入一个绿色格子和从这个绿色格子出去的一定是一个横着进来一个竖着出去.所以将每个格子拆分成两个,一个横着的,一个竖着的.横着的只能向相邻的竖着的节点连边,竖着的只能向横着的连边.这时候如果还满流,那么就存在一种方案使得所有格子都是弯曲的:

第四步,如果不满组第三步的条件,那么只需要每个节点内部横着的和竖着的连一条边,但是对于要求是弯曲形状的格子,过这条边代价就是1,否则代价就是0.然后求最小费用最大流即可.

code of problem1
#include <cmath>
#include <iostream>
#include <vector> class RobotHerb {
public:
long long getdist(int T, std::vector<int> a) {
struct node {
int x, y, dir;
};
const int kDirX[] = {0, 1, 0, -1};
const int kDirY[] = {1, 0, -1, 0}; auto Go = [&](int dir) -> node {
int x = 0, y = 0;
for (size_t i = 0; i < a.size(); ++i) {
if (kDirX[dir] != 0) {
x += kDirX[dir] * a[i];
} else {
y += kDirY[dir] * a[i];
}
dir = (dir + a[i]) & 3;
}
return {x, y, dir};
}; auto Add = [](const node &a, long long *x, long long *y) {
*x += a.x;
*y += a.y;
}; node t[4];
for (int i = 0; i < 4; ++i) {
t[i] = Go(i);
}
long long start_x = 0, start_y = 0;
int current_dir = 0;
int times = 0;
do {
Add(t[current_dir], &start_x, &start_y);
current_dir = t[current_dir].dir;
++times;
} while (current_dir != 0);
start_x *= T / times;
start_y *= T / times;
T %= times;
for (int i = 0; i < T; ++i) {
Add(t[current_dir], &start_x, &start_y);
current_dir = t[current_dir].dir;
}
return (start_x < 0 ? -start_x : start_x) +
(start_y < 0 ? -start_y : start_y);
}
};
code of problem2
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector> const int MAX = 36; long long f[MAX][2][MAX + 1][MAX + 1];
long long g[2][MAX + 1][MAX + 1];
long long g1[2][MAX + 1][MAX + 1]; class CentaurCompany {
public:
double getvalue(std::vector<int> a, std::vector<int> b) {
node_number_ = static_cast<int>(a.size() + 1);
tree_.resize(node_number_);
for (int i = 0; i < node_number_ - 1; ++i) {
int u = a[i] - 1;
int v = b[i] - 1;
tree_[u].push_back(v);
tree_[v].push_back(u);
}
Dfs(0, -1);
long long result = 0;
for (int n = 1; n <= node_number_; ++n) {
for (int c = 1; c <= n; ++c) {
if (Cost(n, c) > 0) {
result += Cost(n, c) * Count(n, c);
}
}
}
return 2.0 * result / (1ll << node_number_);
} private:
int Cost(int n, int c) { return std::max(0, c + c - n - 2); } long long Count(int n, int c) { return f[0][0][n][c] + f[0][1][n][c]; } void Copy(long long source[2][MAX + 1][MAX + 1],
long long sink[2][MAX + 1][MAX + 1]) {
for (int i = 0; i <= node_number_; ++i) {
for (int j = 0; j <= i; ++j) {
sink[0][i][j] = source[0][i][j];
sink[1][i][j] = source[1][i][j];
}
}
} void Dfs(int rt, int parent) {
for (size_t i = 0; i < tree_[rt].size(); ++i) {
if (tree_[rt][i] != parent) {
Dfs(tree_[rt][i], rt);
}
}
memset(g, 0, sizeof(g));
g[0][0][0] = 1;
g[1][1][1] = 1;
for (size_t i = 0; i < tree_[rt].size(); ++i) {
if (tree_[rt][i] != parent) {
int son = tree_[rt][i];
for (int n = node_number_; n >= 0; --n) {
for (int c = n; c >= 0; --c) {
g1[0][n][c] = 0;
g1[1][n][c] = 0;
for (int n1 = n; n1 >= 0; --n1) {
for (int c1 = c; c1 >= 0; --c1) {
g1[0][n][c] += g[0][n1][c1] * (f[son][0][n - n1][c - c1] +
f[son][1][n - n1][c - c1]);
g1[1][n][c] +=
g[1][n1][c1] * (f[son][0][n - n1][c - c1] +
(c1 > 0 ? f[son][1][n - n1][c - c1 + 1]
: f[son][1][n - n1][c - c1]));
}
}
}
}
Copy(g1, g);
}
}
Copy(g, f[rt]);
} int node_number_;
std::vector<std::vector<int>> tree_;
};
code of problem3
#include <iostream>
#include <limits>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector> template <typename FlowType, typename CostType>
class MaxFlowMinimunCost {
static constexpr FlowType kMaxFlow = std::numeric_limits<FlowType>::max();
static constexpr FlowType kZeroFlow = static_cast<FlowType>(0);
static constexpr CostType kMaxCost = std::numeric_limits<CostType>::max();
static constexpr CostType kZeroCost = static_cast<CostType>(0); public:
void Clear() {
index_normalizer_.clear();
node_number_ = 0;
all_edges_.clear();
} void AddEdge(int from, int to, FlowType flow, CostType cost) {
from = GetIndex(from);
to = GetIndex(to);
auto idx0 = Add(from, to, flow, cost);
auto idx1 = Add(to, from, kZeroFlow, -cost);
all_edges_[from][idx0].sym_node_index = idx1;
all_edges_[to][idx1].sym_node_index = idx0;
} std::pair<FlowType, CostType> GetResult(int source, int sink) {
source = GetIndex(source);
sink = GetIndex(sink); struct Node {
FlowType flow;
CostType cost;
int previous_node;
int previous_edge_index;
bool visited;
}; std::vector<Node> dp(node_number_); auto SPFA = [&]() -> FlowType {
for (int i = 0; i < node_number_; ++i) {
dp[i].previous_edge_index = -1;
dp[i].flow = kZeroFlow;
dp[i].visited = false;
dp[i].cost = kMaxCost;
}
std::queue<int> bfs_queue;
bfs_queue.push(source);
dp[source].cost = kZeroCost;
dp[source].flow = kMaxFlow;
while (!bfs_queue.empty()) {
int current_node = bfs_queue.front();
bfs_queue.pop(); dp[current_node].visited = false; for (size_t i = 0; i < all_edges_[current_node].size(); ++i) {
int next_node = all_edges_[current_node][i].to;
CostType cost = all_edges_[current_node][i].cost;
FlowType flow = all_edges_[current_node][i].flow;
if (flow > kZeroFlow &&
dp[next_node].cost > dp[current_node].cost + cost) {
dp[next_node].cost = dp[current_node].cost + cost;
dp[next_node].flow = std::min(dp[current_node].flow, flow);
dp[next_node].previous_node = current_node;
dp[next_node].previous_edge_index = static_cast<int>(i);
if (!dp[next_node].visited) {
dp[next_node].visited = true;
bfs_queue.push(next_node);
}
}
}
}
return dp[sink].flow;
}; FlowType total_flow = kZeroFlow;
CostType total_cost = kZeroCost; while (true) {
FlowType flow = SPFA();
if (IsNoFlow(flow)) {
break;
}
total_flow += flow;
for (int i = sink; i != source;) {
int idx = dp[i].previous_edge_index;
int pre_node = dp[i].previous_node;
total_cost += flow * all_edges_[pre_node][idx].cost;
all_edges_[pre_node][idx].flow -= flow;
all_edges_[i][all_edges_[pre_node][idx].sym_node_index].flow += flow;
i = pre_node;
}
}
return {total_flow, total_cost};
} private:
bool IsNoFlow(FlowType flow) {
constexpr double kEpsilon = 1e-3;
return static_cast<double>(flow) < kEpsilon;
} int GetIndex(int t) {
if (index_normalizer_.find(t) != index_normalizer_.end()) {
return index_normalizer_[t];
}
index_normalizer_[t] = node_number_++;
all_edges_.push_back({});
return node_number_ - 1;
} size_t Add(int from, int to, FlowType flow, CostType cost) {
EdgeNode node;
node.to = to;
node.flow = flow;
node.cost = cost;
all_edges_[from].push_back(node);
return all_edges_[from].size() - 1;
} struct EdgeNode {
int to;
FlowType flow;
CostType cost;
int sym_node_index;
}; std::vector<std::vector<EdgeNode>> all_edges_; std::unordered_map<int, int> index_normalizer_;
int node_number_ = 0;
}; class CurvyonRails {
public:
int getmin(std::vector<std::string> field) {
int n = static_cast<int>(field.size());
int m = static_cast<int>(field[0].size()); auto GetIndex = [&](int i, int j, int t) { return (i * m + j) << 1 | t; }; auto IsGreen = [](int i, int j) { return (i & 1) == (j & 1); }; auto IsValid = [&](int i, int j) {
return i >= 0 && i < n && j >= 0 && j < m && field[i][j] != 'w';
}; MaxFlowMinimunCost<int, int> solver;
const int source = GetIndex(n, m, 0);
const int sink = source + 1;
int delta_x[] = {0, 0, 1, -1};
int delta_y[] = {1, -1, 0, 0};
int total = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (!IsValid(i, j)) {
continue;
}
++total;
if (IsGreen(i, j)) {
solver.AddEdge(source, GetIndex(i, j, 0), 1, 0);
solver.AddEdge(source, GetIndex(i, j, 1), 1, 0);
for (int d = 0; d < 4; ++d) {
int x = i + delta_x[d];
int y = j + delta_y[d];
if (IsValid(x, y)) {
int t = d >> 1;
solver.AddEdge(GetIndex(i, j, t), GetIndex(x, y, t^1), 1, 0);
}
}
} else {
solver.AddEdge(GetIndex(i, j, 0), sink, 1, 0);
solver.AddEdge(GetIndex(i, j, 1), sink, 1, 0);
}
int self_cost = field[i][j] == 'C' ? 1 : 0;
solver.AddEdge(GetIndex(i, j, 0), GetIndex(i, j, 1), 1, self_cost);
solver.AddEdge(GetIndex(i, j, 1), GetIndex(i, j, 0), 1, self_cost);
}
}
auto result = solver.GetResult(source, sink);
int max_flow = result.first;
int min_cost = result.second;
if (max_flow != total) {
return -1;
}
return min_cost;
}
};
topcoder srm 570 div1的更多相关文章
- 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 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& ...
- TopCoder SRM 605 DIV1
604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...
- topcoder srm 575 div1
problem1 link 如果$k$是先手必胜那么$f(k)=1$否则$f(k)=0$ 通过对前面小的数字的计算可以发现:(1)$f(2k+1)=0$,(2)$f(2^{2k+1})=0$,(3)其 ...
随机推荐
- poj 3525
多边形内最大半径圆. 哇没有枉费了我自闭了这么些天,大概五天前我看到这种题可能毫无思路抓耳挠腮举手投降什么的,现在已经能1A了哇. 还是先玩一会计算几何,刷个几百道 嗯这个半平面交+二分就阔以解决.虽 ...
- Java课程寒假之开发记账本软件(网页版)之三
一.实现基础功能之一(查询)(补) 在上一篇中解释的不够详细,在本篇中补充一下指定日期查询,其实和查询没有什么区别,就是设置select下拉框来对于日期的起始与结束日期,然后就是一个简单的mysql语 ...
- ubuntu下apache新建虚拟主机
最近发现在一个服务器上面布了一些项目,如果不用虚拟主机,用链接跳进去的话,有时候路径会出错,而自己在配置虚拟主机的时候又出现了一些问题,看似简单的东西,却花费了一上午,所以决定把简单的方法记下来,供和 ...
- 从vue渲染想到的数组方法
<div id="app"> <ul> <li v-for='item in goods'>{{item}}</li> </u ...
- Spring-Boot构建多模块项目
Spring-Boot构建多模块项目 功能模块单独项目开发,可以将一个庞大的项目分解成多个小项目,便于细分开发 Maven多模块项目不能独立存在,必须有一个介质来包含. 1.创建一个Maven 项目, ...
- HTML5 - Canvas【申明:来源于网络】
HTML5 - Canvas 地址:http://hwaphon.site/
- try 和 catch 和 finally
try块中出现异常走进catch,异常后面的代码不会再被执行:finally块里try和catch执行完后要执行的代码,且一定会执行. 在释放资源的时候,不要多个放在一个try块里面. 流的关闭顺序: ...
- JavaScript面向对象之get和set设置读写属性
之前我们通过this和prototype申明的属性都是可读写的属性,如果想实现单独控制,就必须使用get和set存取期. 基本方法的 步骤一般包含两个步骤,1,使用var关键字定义一个私有属性作为中间 ...
- [daily][mathematica][fcitx] mathematica 无法输入中文的问题
mathematica无法输入中文, 我的输入法使用 fcitx 于是我给fcitx的作者提了issue https://github.com/fcitx/fcitx/issues/372 数日之后, ...
- [设计语言][统一建模语言][软件开发方法学][UML] UML
背景介绍 UML是一种统计建模语言. WIKI: https://en.wikipedia.org/wiki/Unified_Modeling_Language UML是OMG (object man ...