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() { ...
随机推荐
- Python 爬虫知识点
一.基础知识 1.HTML分析 2.urllib爬取 导入urilib包(Python3.5.2) 3.urllib保存网页 import urllib.requesturl = "http ...
- org.apache.activemq.transport.InactivityIOException: Cannot send, channel has already failed
项目是使用activeMQ 发布订阅的模式,在本地测试正常,但是 放到服务器上出现这个错误: org.apache.activemq.transport.InactivityIOException: ...
- js正则函数match、exec、test、search、replace、split使用介绍集合,学习正则表达式的朋友可以参考下。
match 方法 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) 参数 stringObj 必选项.对其进行查找的 String 对 ...
- C++11新特性之一——Lambda表达式
C++11新特性总结可以参考:http://www.cnblogs.com/pzhfei/archive/2013/03/02/CPP_new_feature.html#section_6.8 C++ ...
- EL表达式各种函数使用大全
引入<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> ...
- JS Date parse
因为JS中的Date转换格式没有“-”这种间隔符,Date.parse会生成NAN,所以只能进行转换. <script type="text/javascript"> ...
- MUI Hbuilder设置模拟器运行APP项目
1 安装hbuilder和夜神模拟器 2 hbuilder 新建app项目 3 hbuilder:运行-> 设置web服务器->Hbuilder 第三方安卓模拟器端口:62001 4 运 ...
- Docker源码分析(七):Docker Container网络 (上)
1.前言(什么是Docker Container) 如今,Docker技术大行其道,大家在尝试以及玩转Docker的同时,肯定离不开一个概念,那就是“容器”或者“Docker Container”.那 ...
- min-height的兼容性问题
1.经测试 IE+和其它主流浏览器均支持min-height属性,已经满足目前的需求. 2.当height和min-height同时设置时,浏览器自动选择数值更大的一个(测试IE7+及其他主流浏览器) ...
- Android Activity与Fragment生命周期 对应关系