Educational Codeforces Round 58 Solution
A. Minimum Integer
签到。
#include <bits/stdc++.h>
using namespace std; #define ll long long
ll l, r, d; int main()
{
int t; scanf("%d", &t);
while (t--)
{
scanf("%lld%lld%lld", &l, &r, &d);
if (d < l) printf("%lld\n", d);
else printf("%lld\n", ((r / d) + ) * d);
}
return ;
}
B. Accordion
签到。
#include <bits/stdc++.h>
using namespace std; #define N 5000010
char s[N]; int work()
{
int l = -, r = -, flag, len = strlen(s + );
flag = false;
for (int i = ; i <= len; ++i)
{
if (s[i] == '[') flag = true;
else if (s[i] == ':' && flag)
{
l = i;
break;
}
}
if (l == -) return -;
flag = false;
for (int i = len; i >= ; --i)
{
if (s[i] == ']') flag = true;
else if (s[i] == ':' && flag)
{
r = i;
break;
}
}
if (r == -) return -;
if (r <= l) return -;
int res = ;
for (int i = l + ; i < r; ++i) if (s[i] == '|')
++res;
return res;
} int main()
{
while (scanf("%s", s + ) != EOF)
printf("%d\n", work());
return ;
}
C. Division and Union
Solved.
题意:
有n个区间,将它分成两个集合,使得每个集合任意出一个区间组成的一对,这对没有交
思路:
按左端点排序,如果存在这样的划分,那么必定一个界限使得当前区间与之前的那个区间没有交,这样的话,后面的区间和之前的区间都不会有交
#include <bits/stdc++.h>
using namespace std; #define N 100010
int t, n, ans[N];
struct node
{
int l, r, id;
void scan(int id) { scanf("%d%d", &l, &r); this->id = id; }
bool operator < (const node &other) const { return l < other.l || (l == other.l && r < other.r); }
}a[N]; int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for (int i = ; i <= n; ++i) a[i].scan(i);
sort(a + , a + + n);
int pos = -; int maxr = a[].r;
for (int i = ; i <= n; ++i)
{
if (a[i].l > maxr)
{
pos = i;
break;
}
maxr = max(maxr, a[i].r);
}
if (pos == -) puts("-1");
else
{
for (int i = ; i <= n; ++i) ans[a[i].id] = i < pos ? : ;
for (int i = ; i <= n; ++i) printf("%d%c", ans[i], " \n"[i == n]);
}
}
return ;
}
D. GCD Counting
Upsolved.
题意:
一棵树中,每个点有权值,找出一条最长的简单路径,使得这个路劲上所有点的点权的gcd > 1
思路:
枚举质因子,再在虚树上dp
质因子很多,有1w多个,但是我们考虑每个质因子对应的集合的总和不会太多,
因为一个数的质因子个数不会太多,2e5一下也就十几个,那么一个数的贡献也就十几个
最后的总和就是O(nlogn)级别的
其实不用建虚树,直接在dfs序上dp就可以了
#include <bits/stdc++.h>
using namespace std; #define N 200010
int n, a[N], res;
vector <int> G[N];
vector <int> fac[N]; int fa[N], p[N], pos[N], cnt; int f[][N];
void pre(int u)
{
p[u] = ++cnt;
for (auto v : G[u]) if (v != fa[u])
{
fa[v] = u;
pre(v);
}
} void init()
{
for (int i = ; i <= n; ++i) G[i].clear();
for (int i = ; i < N; ++i) fac[i].clear();
memset(pos, -, sizeof pos);
res = ; cnt = ;
} int main()
{
while (scanf("%d", &n) != EOF)
{
init();
for (int i = ; i <= n; ++i) scanf("%d", a + i);
for (int i = , u, v; i < n; ++i)
{
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
pre();
for (int i = ; i <= n; ++i)
{
int tmp = a[i];
int limit = sqrt(tmp);
for (int j = ; j <= limit; ++j)
{
if (tmp % j == )
{
fac[j].push_back(i);
while (tmp % j == ) tmp /= j;
}
}
if (tmp != ) fac[tmp].push_back(i);
}
for (int i = ; i < N; ++i) if (fac[i].size() >= )
{
sort(fac[i].begin(), fac[i].end(), [](int x, int y) { return p[x] > p[y]; });
int len = fac[i].size();
for (int j = ; j < len; ++j) for (int o = ; o < ; ++o) f[o][j] = ;
for (int j = ; j < len; ++j) pos[fac[i][j]] = j;
for (int j = , u, v; j < len; ++j)
{
v = fac[i][j];
res = max(res, f[][j] + f[][j] + );
if (pos[fa[v]] != -)
{
int id = pos[fa[v]];
if (f[][j] + >= f[][id])
{
f[][id] = f[][id];
f[][id] = f[][j] + ;
}
else if (f[][j] + >= f[][id])
{
f[][id] = f[][j] + ;
}
}
}
for (int j = ; j < len; ++j) pos[fac[i][j]] = -;
}
printf("%d\n", res);
}
return ;
}
E. Polycarp's New Job
签到.
#include <bits/stdc++.h>
using namespace std; #define N 500010
int n, x, y, l, r; char op[]; int main()
{
l = , r = ;
scanf("%d", &n);
while (n--)
{
scanf("%s%d%d", op, &x, &y);
if (x > y) swap(x, y);
if (op[] == '+')
{
l = max(l, x);
r = max(r, y);
}
else
{
puts(l <= x && r <= y ? "YES" : "NO");
}
}
return ;
}
F. Trucks and Cities
Upsolved.
题意:
$有n个城市,有m辆卡车需要从s_i -> f_i 每公里耗油c_i升,最多加油r_i次$
$求最小的油箱体积,使得所有卡车都能在加油次数内到达目的地$
思路:
本来考虑二分$但是复杂度是O(n \cdot m \cdot log(10^{18}))$
考虑$dp$
$dp[i][j][k] 表示从i -> j 最多加油k次的最少油箱容量$
$dp[i][j][k] = \min_{w = i}^{j} max(dp[i][w][k - 1], a[j] - a[w])$
我们发现 $dp[i][w][k - 1] 随着w递增而增加,a[j] - a[w] 随着w递增而减少$
$但是,max(dp[i][w][k - 1], a[j] -a[w]) 是先减后增的$
并且随着$j的右移动,决策点肯定只会向右移动,而不会回到左边$
#include <bits/stdc++.h>
using namespace std; #define ll long long
#define N 401
int n, m, a[N];
int f[N][N][N]; int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
for (int i = ; i <= n; ++i) scanf("%d", a + i);
memset(f, , sizeof f);
for (int i = ; i <= n; ++i)
for (int j = ; j <= n; ++j)
f[i][j][] = a[j] - a[i];
for (int k = ; k <= n; ++k)
for (int i = , w; i <= n; ++i)
{
w = i;
for (int j = i + ; j <= n; ++j)
{
while (w < j && max(f[i][w + ][k - ], a[j] - a[w + ]) <= max(f[i][w][k - ], a[j] - a[w])) ++w;
f[i][j][k] = max(f[i][w][k - ], a[j] - a[w]);
}
}
ll res = ;
for (int i = , s, e, c, r; i <= m; ++i)
{
scanf("%d%d%d%d", &s, &e, &c, &r);
res = max(res, 1ll * c * f[s][e][r]);
}
printf("%lld\n", res);
}
return ;
}
G. (Zero XOR Subset)-less
Upsolved.
题意:
将一些数分组,使得没有任意一个这些组的子集的异或和等于0
思路:
如果所有数异或起来等于$0$就是无解的情况
否则就是线性基中基的个数,因为每个基的最高位的$1所在位置不同$
$所以这些基不管怎么异或都不会是0$
#include <bits/stdc++.h>
using namespace std; #define N 200010
int n, a[N], p[]; int main()
{
while (scanf("%d", &n) != EOF)
{
int t = ;
for (int i = ; i <= n; ++i) scanf("%d", a + i), t ^= a[i];
if (!t) puts("-1");
else
{
memset(p, , sizeof p);
for (int i = ; i <= n; ++i)
for (int j = ; j >= ; --j)
if ((a[i] >> j) & )
{
if (!p[j])
{
p[j] = a[i];
break;
}
else a[i] ^= p[j];
}
int res = ;
for (int i = ; i < ; ++i) if (p[i]) ++res;
printf("%d\n", res);
}
}
return ;
}
Educational Codeforces Round 58 Solution的更多相关文章
- Educational Codeforces Round 58 (Rated for Div. 2) 题解
Educational Codeforces Round 58 (Rated for Div. 2) 题目总链接:https://codeforces.com/contest/1101 A. Min ...
- Educational Codeforces Round 58 (Rated for Div. 2) F dp + 优化(新坑) + 离线处理
https://codeforces.com/contest/1101/problem/F 题意 有n个城市,m辆卡车,每辆卡车有起点\(s_i\),终点\(f_i\),每公里油耗\(c_i\),可加 ...
- Educational Codeforces Round 58 (Rated for Div. 2) D 树形dp + 数学
https://codeforces.com/contest/1101/problem/D 题意 一颗n个点的树,找出一条gcd>1的最长链,输出长度 题解 容易想到从自底向长转移 因为只需要g ...
- Educational Codeforces Round 58 (Rated for Div. 2) G 线性基
https://codeforces.com/contest/1101/problem/G 题意 一个有n个数字的数组a[],将区间分成尽可能多段,使得段之间的相互组合异或和不等于零 题解 根据线性基 ...
- Educational Codeforces Round 58 A,B,C,D,E,G
A. Minimum Integer 链接:http://codeforces.com/contest/1101/problem/A 代码: #include<bits/stdc++.h> ...
- Educational Codeforces Round 58 Div. 2 自闭记
明明多个几秒就能场上AK了.自闭. A:签到. #include<iostream> #include<cstdio> #include<cmath> #inclu ...
- Educational Codeforces Round 58
D. GCD Counting 题意: 给出n个点的树,每个点有一个权值,找出一条最长的路径使得路径上所有的点的gcd>1 题解: gcd>1的一定不会有很多.所以暴力搞一下就行,不需要点 ...
- Educational Codeforces Round 56 Solution
A. Dice Rolling 签到. #include <bits/stdc++.h> using namespace std; int t, n; int main() { scanf ...
- Educational Codeforces Round 57 Solution
A. Find Divisible 签到. #include <bits/stdc++.h> using namespace std; int t, l, r; int main() { ...
随机推荐
- BI产品学习笔记
理解现在--挖掘规律--预测未来------------------------------------------------------精准营销智能风控运营优化 多维分析挖掘预测敏捷BI 分析展示 ...
- docker run 详解
docker run 用于把镜像启动为容器,语法如下: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 常见用法: [root@localhost ~]$ ...
- Java之类型的转换
1.String 类型转化为 int 类型,需要使用 Integer 类中的 parseInt() 方法或者 valueOf() 方法进行转换. int a = Integer.parseInt(st ...
- LeetCode——Binary Tree Paths
Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...
- Linux 2.6.16 TCP连接速度异常的问题分析
版权声明:本文由余子军原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/104 来源:腾云阁 https://www.qclo ...
- SimpleDateFormat使用详解 <转>
public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (d ...
- sencha touch 扩展篇之将sencha touch打包成安装程序(上)- 使用sencha cmd打包安装程序
由于最近一直忙着android原生的开发,很久没有更新博客了,官方的sencha cmd工具功能非常强大,创建项目,压缩项目,打包安装程序都能轻松实现,这讲我们就给大家介绍下如何使用sencha cm ...
- 【黑金ZYNQ7000系列原创视频教程】02.视频接口——hdmi编码输出实验
黑金论坛地址: http://www.heijin.org/forum.php?mod=viewthread&tid=36636&extra=page%3D1 爱奇艺地址: http: ...
- iOS5 ARC学习笔记:strong、weak等详解
2013-03-25 13:41 佚名 oschina 字号:T | T iOS5中加入了新知识,就是ARC,其实我并不是很喜欢它,因为习惯了自己管理内存.但是学习还是很有必要的.现在我们看看iOS5 ...
- [MongoDB] 安装MongoDB配置Replica Set
MongoDB的环境主要包括StandAlone,Replication和Sharding. StandAlone:单机环境,一般开发测试的时候用. Replication:主从结构,一个Primar ...