Educational Codeforces Round 34
F - Clear The Matrix
分析
题目问将所有星变成点的花费,限制了行数(只有4行),就可以往状压DP上去靠了。
\(dp[i][j]\) 表示到第 \(i\) 列时状态为 \(j\) 的花费,只需要记录 16 位二进制,因为我们最多只能影响到 4 * 4 的星,那么每次都是从一个 4 * 4 的矩阵转移到一个 4 * 4 的矩阵,注意,转移时必须保证最左边列全部为 1 (即都是星号),那么最后答案就是 \(dp[n][(1 << 16) - 1]\)。
比如我们选定点 (i, j),将 3 * 3 的星变成点,那么变的就是左上角 (i, j - 2) 右下角 (i + 2, j) 的这个矩阵。
为了状态转移,我们会同时对一列的星进行变换,可能有多种方案,这个可以预处理再加些优化,最后合法的是很少的。
code
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
string mp[4];
int dp[2][1 << 16];
int n, cnt, a[5], b[133], c[133];
int calc(int xl, int xr, int yl, int yr) {
int res = 0;
for (int i = xl; i <= xr; i++) {
for (int j = yl; j <= yr; j++) {
res += 1 << ((i * 4) + j);
}
}
return res;
}
void dfs(int x, int mx, int st, int cost) {
if (x == 4) {
b[cnt] = st;
c[cnt++] = cost;
return;
}
for (int i = 4; i >= 1; i--) {
if (x + i > 4) continue;
if (!x) {
dfs(x + 1, i, st | calc(4 - i, 3, x, i - 1), cost + a[i]);
} else {
if (x + i > mx) {
dfs(x + 1, x + i, st | calc(4 - i, 3, x, x + i - 1), cost + a[i]);
} else {
break;
}
}
}
dfs(x + 1, x, st, cost);
}
int main() {
cin >> n >> a[1] >> a[2] >> a[3] >> a[4];
for (int i = 0; i < 4; i++) {
cin >> mp[i];
}
dfs(0, 0, 0, 0);
memset(dp[0], 0x3f, sizeof dp[0]);
dp[0][(1 << 16) - 1] = 0;
int cur = 0;
for (int i = 0; i < n; i++) {
int cst = 0;
for (int j = 0; j < 4; j++) {
if (mp[j][i] == '.') {
cst += 1 << (12 + j);
}
}
cur = !cur;
memset(dp[cur], 0x3f, sizeof dp[cur]);
for (int j = 0; j < (1 << 12); j++) {
dp[cur][j | cst] = dp[!cur][(j << 4) + 15];
if (dp[!cur][(j << 4) + 15] == 0x3f3f3f3f) continue;
for (int k = 0; k < cnt; k++) {
dp[cur][j | cst | b[k]] = min(dp[!cur][(j << 4) + 15] + c[k], dp[cur][j | cst | b[k]]);
}
}
}
cout << dp[cur][(1 << 16) - 1] << endl;
return 0;
}
G. Yet Another Maxflow Problem
分析
一道“网络流”的题目。
本题主要注意最小割等于最大流,我们去构造这个解,即怎样才算最小割。注意到本题边的限制颇多,\(A_i\)-\(A_{i+1}\) 连有向边,\(B_i\)-\(B_{i+1}\) 连有向边,且从 A 到 B 连有向边,考虑 A 这部分,如果删掉边 \(A_i\)-\(A_{i+1}\) ,那么 \(A_{i+1}\) 下面所有边都无意义了,对于 B 这部分,删掉 \(B_i\)-\(B_{i+1}\),则 \(B_i\) 上面所有边都无意义了,有这样的性质后,我们枚举左边的边,用线段树维护删掉右边的边的代价的最小值(右边也有可能不删),用 multiset 维护全局最优解。
code
#include <bits/stdc++.h>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
using namespace std;
const int N = 2e5 + 10;
int a[N], b[N];
vector<pair<int, int> > G[N];
long long s[N << 4], lazy[N << 4];
void pushDown(int rt) {
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
s[rt << 1] += lazy[rt];
s[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
}
void pushUp(int rt) { s[rt] = min(s[rt << 1], s[rt << 1 | 1]); }
void build(int l, int r, int rt) {
if (l == r)
s[rt] = b[l - 1];
else {
int m = l + r >> 1;
build(lson);
build(rson);
pushUp(rt);
}
}
void update(int L, int R, int c, int l, int r, int rt) {
if (l >= L && r <= R) {
lazy[rt] += c;
s[rt] += c;
} else {
pushDown(rt);
int m = l + r >> 1;
if (m >= L) update(L, R, c, lson);
if (m < R) update(L, R, c, rson);
pushUp(rt);
}
}
long long query(int L, int R, int l, int r, int rt) {
if (l >= L && r <= R)
return s[rt];
else {
pushDown(rt);
int m = l + r >> 1;
long long res = (1LL << 62);
if (m >= L) res = query(L, R, lson);
if (m < R) res = min(res, query(L, R, rson));
pushUp(rt);
return res;
}
}
multiset<long long> mset;
long long cb[N];
int main() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i < n; i++) {
scanf("%d%d", &a[i], &b[i]);
}
for (int i = 0; i < m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
G[u].push_back(pair<int, int>(v, w));
}
build(1, n, 1);
for (int i = 1; i <= n; i++) {
for (int j = 0; j < G[i].size(); j++) {
update(1, G[i][j].first, G[i][j].second, 1, n, 1);
}
cb[i] = s[1];
mset.insert(cb[i] + a[i]);
}
printf("%I64d\n", *mset.begin());
while (q--) {
int v, w;
scanf("%d%d", &v, &w);
mset.erase(mset.lower_bound(a[v] + cb[v]));
a[v] = w;
mset.insert(w + cb[v]);
printf("%I64d\n", *mset.begin());
}
return 0;
}
Educational Codeforces Round 34的更多相关文章
- Educational Codeforces Round 34 (Rated for Div. 2) A B C D
Educational Codeforces Round 34 (Rated for Div. 2) A Hungry Student Problem 题目链接: http://codeforces. ...
- Educational Codeforces Round 34 (Rated for Div. 2) D - Almost Difference(高精度)
D. Almost Difference Let's denote a function You are given an array a consisting of n integers. You ...
- Educational Codeforces Round 34 (Rated for Div. 2) C. Boxes Packing
C. Boxes Packing time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Educational Codeforces Round 34 D. Almost Difference【模拟/stl-map/ long double】
D. Almost Difference time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Educational Codeforces Round 34 C. Boxes Packing【模拟/STL-map/俄罗斯套娃】
C. Boxes Packing time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Educational Codeforces Round 34 B. The Modcrab【模拟/STL】
B. The Modcrab time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Educational Codeforces Round 34 A. Hungry Student Problem【枚举】
A. Hungry Student Problem time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Educational Codeforces Round 34 (Rated for Div. 2)
A. Hungry Student Problem time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Educational Codeforces Round 34 (Rated for Div. 2) B题【打怪模拟】
B. The Modcrab Vova is again playing some computer game, now an RPG. In the game Vova's character re ...
随机推荐
- 如何解决更新被拒绝,因为远程版本库包含您本地尚不存在的提交。这通常是因为另外 提示:一个版本库已向该引用进行了推送。再次推送前,您可能需要先整合远程变更 提示:(如 'git pull ...')。
不要通过网页提交,通过网页提交一次,然后在终端再次push的时候,会认为网上代码仓库已经被其他地方提交过一次代码,此时会拒绝终端push 这个时候只能是pull,然后才能再次在终端提交. 也就是说,避 ...
- scrapy里的selector,不能有正则提取
参考:http://blog.csdn.net/dawnranger/article/details/50037703 Selector 有一个 .re() 方法,用来通过正则表达式来提取数据. 不同 ...
- 用Inferno代替React开发高性能响应式WEB应用
什么是Inferno Inferno可以看做是React的另一个精简.高性能实现.它的使用方式跟React基本相同,无论是JSX语法.组件的建立.组件的生命周期,还是与Redux或Mobx的配合.路由 ...
- Android Studio 提示android.support.v4不存在的解决方法
最近想学习仿QQ列表的侧滑删除功能,看完资料之后,发现有一堆错误,看了一下,说是不存在android.support.v4包不存在,浪费了一个多小时,终于是找到了解决方法,便是记录下来 打开file- ...
- Python 项目实践二(下载数据)第三篇
接着上节继续学习,在本章中,你将从网上下载数据,并对这些数据进行可视化.网上的数据多得难以置信,且大多未经过仔细检查.如果能够对这些数据进行分析,你就能发现别人没有发现的规律和关联.我们将访问并可视化 ...
- ArcGIS API for JavaScript 4.2学习笔记[16] 弹窗自定义功能按钮及为要素自定义按钮(第五章完结)
这节对Popups这一章的最后两个例子进行介绍和解析. 第一个[Popup Actions]介绍了弹窗中如何自定义工具按钮(名为actions),以PopupTemplate+FeatureLayer ...
- Time Complexity of Loop with Powers
以下功能的时间复杂度是多少? void fun(int n, int k) { for (int i=1; i<=n; i++) { int p = pow(i, k); for (int j= ...
- bzoj 4898: [Apio2017]商旅
Description 在广阔的澳大利亚内陆地区长途跋涉后,你孤身一人带着一个背包来到了科巴.你被这个城市发达而美丽的市场所 深深吸引,决定定居于此,做一个商人.科巴有个集市,集市用从1到N的整数编号 ...
- bzoj 1566: [NOI2009]管道取珠
Description Input 第一行包含两个整数n, m,分别表示上下两个管道中球的数目. 第二行为一个AB字符串,长度为n,表示上管道中从左到右球的类型.其中A表示浅色球,B表示深色球. ...
- 微信JS-SDK使用步骤(以微信扫一扫为例)
概述: 微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包. 通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统的能力,同时可以直接使用 ...