Codeforces Round #446 (Div. 2)

  • 总体:rating涨了好多,虽然有部分是靠和一些大佬(例如 redbagHometown )交流的……希望下次能自己做出来2333

A. Greed

题意

给你\(n\)个罐子,每个罐子都告诉了你它当前的饮料体积和它的总容积,问能否用两个罐子装下所有饮料(\(n\le 100000\))

题解

简单模拟即可,用两个最大的算一下,注意累和会爆\(int\)

int a[100100], b[100100];
long long sum = 0;
int main () {
int n = read();
For (i, 1, n) {
a[i] = read();
sum += a[i];
}
For (i, 1, n)
b[i] = read();
sort (b + 1, b + 1 + n);
if (b[n] + b[n - 1] >= sum)
puts("YES");
else
puts("NO");
return 0;
}

B. Wrath

题意

给你一个长度为\(n\)序列,每个点有个\(l\)值,会覆盖他前面\(l\)个点,问最后还剩多少个点没有被覆盖。(\(n\le 100000\))

题解

应该用差分简单做一下就行了,脑子一抽写了个线段树上去,幸亏过了2333(就当练手吧)

代码

const int N = 1e6 + 1e3;
int sum[N << 2]; #define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
void Build(int o, int l, int r) {
if (l == r) { sum[o] = 1; return ; }
int mid = (l + r) >> 1;
Build(lson); Build(rson);
sum[o] = sum[o << 1] + sum[o << 1 | 1];
} int ul, ur;
int lazy[N << 2];
void push_down(int o) {
if (!lazy[o]) return;
lazy[o << 1] = lazy[o << 1 | 1] = 1;
sum[o << 1] = sum[o << 1 | 1] = 0; lazy[o] = 0;
} void Update(int o, int l, int r) {
if (ul <= l && r <= ur) { lazy[o] = 1; sum[o] = 0; return ;}
push_down(o); int mid = (l + r) >> 1;
if (ul <= mid) Update(lson); if (ur > mid) Update(rson);
sum[o] = sum[o << 1] + sum[o << 1 | 1];
} int main () {
int n = read();
Build(1, 1, n);
For (i, 1, n) {
int len = read();
if (i == 1) continue ;
if (!len) continue ;
ul = max(i - len, 1); ur = i - 1;
Update(1, 1, n);
}
printf ("%d\n", sum[1]);
return 0;
}

C. Pride

题意

给你一个长为\(n\)序列,你能进行一个操作,将相邻两个数中的一个替换为他们的\(gcd\),问至少进行多少次操作能把他们全部变成\(1\),如果不能完成输出\(-1\)。(\(n\le 2000\))

题解

这题一开始搞了我好久QAQ,想错了。其实就是先弄出一个\(1\),然后可以把其他所有的数字都变成\(1\),剩下的步数就是\(n-1\)。找那个步数,一定是一段连续区间\(gcd=1\)的情况,然后你用\(O(n^2)\)去枚举一下,找到一个可行的最小长度。还需要特判一开始就有\(1\)的情况,不然会挂掉(我就惨遭hack了TAT,后来又hack了两个可怜鬼233)。

代码

const int N = 2010;
int a[N], n, ans = 0x7f7f7f7f, res, now; int main () {
n = read();
res = n;
For (i, 1, n) {
a[i] = read();
if (a[i] == 1) -- res;
now = __gcd(now, a[i]);
}
if (now != 1)
return puts("-1"), 0;
if (res != n)
return printf ("%d\n", res), 0;
For (i, 1, n) {
int here = a[i], len = 0;
if (here == 1) {
ans = 0;
continue ;
}
Fordown (j, i - 1, 1) {
++ len;
here = __gcd(here, a[j]);
if (here == 1) {
chkmin(ans, len);
break;
}
}
}
printf ("%d\n", ans + n - 1);
return 0;
}

D. Gluttony

题意

给你一个长为\(n\)序列,其中每个数字都不相同,让你输出一个原序列的一个排列,使得他们中任意一个子集(不能为空集或者全集)的和都互不相同(就是相同位置上的数的和互不相同就行了)(\(n\le 22\))

题解

一开始不知道做法,redbag大佬讲出来了正解,就是将每一个数换成后一个比它大的数,最大的数换成最小的那一个,就行了。

证明

这个还是比较好证明的。

考虑分两种情况讨论:

  1. 原子集没有最大的那个数:这就显然是个正确的,每个数都比之前的数要大,所以总和肯定要大;

  2. 原子集有最大的那个数:最大数改变后的值最整个里面最大的,然后你将这个子集去掉最大的那个数,剩下的变化量的和肯定要小于你最大值变为最小值的值,因为你之间肯定会有多余的空格(因为不能为全集)留着,所以肯定是可行的啦。

代码

int n, a[25];
struct node {
int val, pos;
bool operator < (const node &rhs) const {
return val < rhs.val;
}
};
node lt[25];
int ans[25]; int main () {
n = read();
For (i, 1, n) {
lt[i].val = read();
lt[i].pos = i;
}
sort (lt + 1, lt + 1 + n);
For (i, 1, n)
if (i != n)
ans[lt[i].pos] = lt[i + 1].val;
else
ans[lt[i].pos] = lt[1].val;
For (i, 1, n)
printf ("%d ", ans[i]);
return 0;
}

E. Envy

终于来填远古巨坑了。

题意

给你一个有 \(n\) 个点 \(m\) 条边的带边权无向连通图。

有 \(q\) 次询问,每次询问 \(k\) 条边是否能同时存在于 \(MST\) (最小生成树)中。(不要求在线回答)

\(n, m, q, \sum k \le 5 \times 10^5\)

题解

首先要知道一个很显然的结论。

就是在 \(Kruskal\) 求 \(MST\) 过程中,我们能否把一条边 \((u, v, w)\) 放入 \(MST\) 中,当且仅当 \(<w\) 的所有边联通后,\(u, v\) 仍然不连通。

所以对于每次询问来说,边权不相同的边是互不影响的,我们可以对于这些边权互不相同的边单独考虑。

相同 \(w\) 的如何考虑呢?首先 \(<w\) 的边全部加入,然后对于这些边按任意顺序加入的时候,不能存在 \((u, v)\) 相连的情况。

那么就可以得到一个离线算法了。把所有边权按权值排序,一条条加入。然后对于权值相同的同一询问的边,也逐个加入判断是否可行,最后利用可撤销并查集按秩合并的特性,把当前询问的边撤回就行了。

最后复杂度就是 \(O((m + \sum k) \log n)\) 的。(挂询问用了 std :: map, set 有点慢。。)

代码

还是比较好写的。。

#include <bits/stdc++.h>

#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define fir first
#define sec second
#define mp make_pair using namespace std; typedef pair<int, int> PII; template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, 1 : 0; } inline int read() {
int x(0), sgn(1); char ch(getchar());
for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
return x * sgn;
} void File() {
#ifdef zjp_shadow
freopen ("E.in", "r", stdin);
freopen ("E.out", "w", stdout);
#endif
} const int N = 5e5 + 1e3; struct Edge {
int u, v, w;
} lt[N]; int Hash[N], tot; struct Data {
int u, v, a, b;
}; namespace Union_Set { int fa[N], height[N]; int find(int x) { return x == fa[x] ? x : find(fa[x]); } Data stk[N]; int top; inline bool Merge(int u, int v, bool flag) {
u = find(u); v = find(v); if (u == v) return false;
if (flag) stk[++ top] = (Data) {u, v, height[u], height[v]};
if (height[u] > height[v]) swap(u, v); fa[u] = v;
if (height[u] == height[v]) ++ height[v]; return true;
} inline void Distract() {
Data cur = stk[top --];
height[fa[cur.u] = cur.u] = cur.a;
height[fa[cur.v] = cur.v] = cur.b;
} } bool ans[N]; map<int, multiset<PII>> M[N]; inline int find_pos(int x) {
return lower_bound(Hash + 1, Hash + tot + 1, x) - Hash;
} int main () { File(); int n = read(), m = read(); using namespace Union_Set; For (i, 1, n) fa[i] = i; For (i, 1, m) {
lt[i].u = read();
lt[i].v = read();
Hash[i] = lt[i].w = read();
}
sort(Hash + 1, Hash + m + 1);
tot = unique(Hash + 1, Hash + m + 1) - Hash - 1; int q = read();
For (i, 1, m)
M[find_pos(lt[i].w)][q + 1].insert(mp(lt[i].u, lt[i].v)); For (i, 1, q) {
int k = read(); ans[i] = true;
For (j, 1, k) {
int id = read();
M[find_pos(lt[id].w)][i].insert(mp(lt[id].u, lt[id].v));
}
} For (i, 1, tot) {
for (auto itm : M[i]) {
if (itm.fir != q + 1 && !ans[itm.fir]) continue ;
for (auto its : itm.sec)
ans[itm.fir] &= Merge(its.fir, its.sec, itm.fir <= q);
while (top) Distract();
}
} For (i, 1, q)
puts(ans[i] ? "YES" : "NO"); return 0; }

Codeforces Round #446 (Div. 2)的更多相关文章

  1. Codeforces Round #446 Div. 1

    B:即使看到n<=22也应该猜到这只是为了写spj.将每个数替换为恰好比他大的数即可,最大值替换为最小值.这样原序列中不包含最小值的集合显然都满足条件,并且容易发现包含最小值的集合的变化量都是最 ...

  2. Codeforces Round #446 (Div. 2) C. Pride【】

    C. Pride time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  3. Codeforces Round #446 (Div. 2) B. Wrath【模拟/贪心】

    B. Wrath time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  4. Codeforces Round #446 (Div. 2) A. Greed【模拟】

    A. Greed time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  5. 【Codeforces Round #446 (Div. 2) C】Pride

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 想一下,感觉最后的结果肯定是从某一段开始,这一段的gcd为1,然后向左和向右扩散的. 则枚举那一段在哪个地方. 我们设这一段中所有的 ...

  6. 【Codeforces Round #446 (Div. 2) B】Wrath

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 倒着来,维护一个最小的点就可以了. [代码] #include <bits/stdc++.h> using namesp ...

  7. 【Codeforces Round #446 (Div. 2) A】Greed

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 贪心选容量大的瓶子就好 [代码] #include <bits/stdc++.h> #define int long l ...

  8. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  9. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

随机推荐

  1. Windows Server 2016-图形化迁移FSMO角色

    上章节我们简单介绍了三种不同方式查看FSMO主机角色信息,在开篇之前我们简单回顾一下FSMO五种操作主机角色:林范围操作主机角色有两种,分别是 架构主机角色(Schema Master)和 域命名主机 ...

  2. React项目模板-从项目搭建到部署

    前一段时间做了一个小项目,时间比较紧,就一个人月.最终希望能够通过微信公众号链接启动应用. 项目的业务细节就不多说了,主要是想分享一下做这个项目技术方面的一些经验. 技术选型 参考范围大致三种:Ang ...

  3. CocosCreator游戏开发---菜鸟学习之路(一)

    PS(废话): 辞职后在家好久好久了,久到经济不允许了,接着就准备再次出去找工作了,然而工作哪有那么好找,特别是像我这种菜鸟.而且我还准备转行,准备去做游戏,技能等级接近于0,那工作就更难找了.既然如 ...

  4. Java中excel与对象的互相转换的通用工具类编写与使用(基于apache-poi-ooxml)

    通用excel与对象相互转换的工具类 前言:最近开发需要一个Excel批量导入或者导出的功能,之前用过poi-ooxml开发过一个导入的工具类,正好蹭着这次机会,把工具类的功能进行完善. 使用说明: ...

  5. ubuntu下安装memcached与php扩展测试使用

    1,memcached需要libevent,所以要先安装它 下载地址:http://download.chinaunix.net/download.php?id=45065&ResourceI ...

  6. sql server在一个字段相同值时,另一个字段结果拼接

    如下字段红框里的信息都一样的,通过转换实现字段拼接 SELECT formmain_id,(SELECT field0040+';' FROM formson_5489 WHERE formmain_ ...

  7. appium+Python 启动app(一)

    当我们appium和Python环境都配置好了,如何启动我们第一个app呢?下面介绍appium+Python启动app的操作步骤,为了能够详细查看,我们这里使用夜游神模拟器进行示范. 测试项目:QQ ...

  8. Servlet3.0上传图片示例

    一.前端JSP页面 <%@page pageEncoding="UTF-8"%><!DOCTYPE html><html><head> ...

  9. 回归模型效果评估系列1-QQ图

    (erbqi)导语 QQ图全称 Quantile-Quantile图,也就是分位数-分位数图,简单理解就是把两个分布相同分位数的值,构成点(x,y)绘图:如果两个分布很接近,那个点(x,y)会分布在y ...

  10. java中的Collection集合类

    随着1998年JDK 1.2的发布,同时新增了常用的Collections集合类,包含了Collection和Map接口.而Dictionary类是在1996年JDK 1.0发布时就已经有了.它们都可 ...