1406A. Subset Mex

https://codeforces.com/contest/1406/problem/A

Example

input

4
6
0 2 1 5 0 1
3
0 1 2
4
0 2 0 1
6
1 2 3 4 5 6

output

5
3
4
0

Note

In the first test case,\(A=\{0,1,2\},B=\{0,1,5\}\) is a possible choice.

In the second test case, \(A=\{0,1,2\},B=∅\) is a possible choice.

In the third test case, \(A=\{0,1,2\},B=\{0\}\) is a possible choice.

In the fourth test case,$ A={1,3,5},B={2,4,6}$ is a possible choice.

题意:

给定一个集合,并定义 \(mex\) 操作:集合中的最小非负数。

如:\(mex(\{1,4,0,2,2,1\})=3\)

求 集合分为两部分的最大值:\(max( mex(A) + mex(B) )\)

思路:

通过维护两个变量从0开始,如果有0、1、2、3...这样的直接慢慢向上叠加

#include<bits/stdc++.h>
#define ms(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long ll;
const int N = 1e5 + 100;
ll n, a[N];
void solve() {
cin >> n;
for (int i = 0; i < n; ++i)cin >> a[i];
sort(a, a + n);
ll m = 0, k = 0;
for (int i = 0; i < n; ++i) {
if (a[i] == m)m++;
else if (a[i] == k)k++;
}
cout << m + k << endl;//m、k相当于两个集合中的非负最小值
} int main() {
//freopen("in.txt", "r", stdin);
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
ll _; cin >> _;
while (_--)solve();
}

1406B. Maximum Product

https://codeforces.com/contest/1406/problem/B

Example

input

4
5
-1 -2 -3 -4 -5
6
-1 -2 -3 1 2 -1
6
-1 0 0 0 -1 -1
6
-9 -7 -5 -3 -2 1

output

-120
12
0
945

Note

In the first test case, choosing \(a1,a2,a3,a4,a5\) is a best choice: \((−1)⋅(−2)⋅(−3)⋅(−4)⋅(−5)=−120\).

In the second test case, choosing \(a1,a2,a3,a5,a6\) is a best choice: \((−1)⋅(−2)⋅(−3)⋅2⋅(−1)=12\).

In the third test case, choosing\(a1,a2,a3,a4,a5\) is a best choice: \((−1)⋅0⋅0⋅0⋅(−1)=0\).

In the fourth test case, choosing \(a1,a2,a3,a4,a6\) is a best choice: \((−9)⋅(−7)⋅(−5)⋅(−3)⋅1=945\).

题意:

给定 大小为n的一个数组,求下标 \((i,j,k,l,t) (i<j<k<l<t).\) 使得\(a1,a2,a3,a4,a5\) 最大

思路:

一开始以为不能排序,搞得卡了好久。

先对所给数组进行排序,这样必然倒数5个最大,又因为存在负数的关系,所以也许 $ - * - $ 反而最大。详情见代码

#include<bits/stdc++.h>
#define ms(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long ll;
const int N = 1e5 + 100;
ll n, a[N];
void solve() {
cin >> n; ll ans[4] = {};
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
ans[0] = a[n] * a[n - 1] * a[n - 2] * a[n - 3] * a[n - 4];
ans[1] = a[n] * a[n - 1] * a[n - 2] * a[1] * a[2];
ans[2] = a[n] * a[1] * a[2] * a[3] * a[4];
sort(ans, ans + 3); cout << ans[2] << endl;
} int main() {
//freopen("in.txt", "r", stdin);
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
ll _; cin >> _;
while (_--)solve();
}

1406C. Link Cut Centroids

https://codeforces.com/contest/1406/problem/C

题目太长这里不粘贴了。

题意:

思路:

DFS搜索,详细待补。请先阅读代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n;
int p1, p2, p3, c[N];
vector<int>g[N];
void dfs(int u, int fa) {
c[u] = 1;
for (auto v:g[u]) if (v != fa) {
dfs(v, u), c[u] += c[v];
}
if (!p3) {
if (c[u] == 1) p1 = fa, p2 = u;
if (n - c[u] == c[u]) p3 = fa;
}
}
signed main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
cin >> n;
p1 = p2 = p3 = 0;
for (int i = 1; i <= n; i++) g[i].clear(), c[i] = 0;
for (int i = n; --i;) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, -1);
cout << p1 << ' ' << p2 << '\n' << p2 << ' ' << (p3 ? p3 : p1) << '\n';
}
return 0;
}

Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题)的更多相关文章

  1. Codeforces Round 480 Div 2 光荣掉分记

    痛 痛苦 痛苦啊. 越接近黄名想的越多了啊…… 都说了不要在意rating这破玩意了…… 没出E就算了,策略问题. 居然还FST了: FST个D就算了: FST个A算个**啊. 紧张的时候总会写出一些 ...

  2. Codeforces Round #670 (Div. 2) D. Three Sequences 题解(差分+思维+构造)

    题目链接 题目大意 给你一个长为n的数组a,要你构造一个非严格单调上升的数组b和一个非严格单调下降的数组c,使得\(b_i+c_i=a_i\) 要你使这两个数组b,c中最大的元素最小,还有q次修改(q ...

  3. Codeforces Round #670 (Div. 2) C. Link Cut Centroids (dfs,树)

    C. Link Cut Centroids Fishing Prince loves trees, and he especially loves trees with only one centro ...

  4. Codeforces Round #670 (Div. 2) B. Maximum Product (暴力)

    题意:有一长度为\(n\)的序列,求其中任意五个元素乘积的最大值. 题解:先排序,然后乘积能是正数就搞正数,模拟一下就好了. 代码: int t; ll n; ll a[N]; int main() ...

  5. Codeforces Round #670 (Div. 2) A. Subset Mex (贪心)

    题意:给你一长度为\(n\)的序列,将其分为两个集合,求两个集合中未出现的最小元素的最大值, 题解:用桶存一下每个元素的个数,两次枚举\([1,100]\),找出两个最小值即可. 代码: int t; ...

  6. Codeforces Round #427 (Div. 2)—A,B,C,D题

    A. Key races 题目链接:http://codeforces.com/contest/835/problem/A 题目意思:两个比赛打字,每个人有两个参数v和t,v秒表示他打每个字需要多久时 ...

  7. Codeforces Round #707 (Div. 2)A.英语漏洞 + C.Going Home C题收获不小

    A题英语漏洞 A题传送门: https://codeforces.com/contest/1501/problem/A 其实题目说的很明白, 只是我傻傻的会错了意, 话不多说, 开整. 前两行是说, ...

  8. Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)

    题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...

  9. Codeforces Round #385 (Div. 2) A. Hongcow Learns the Cyclic Shift 水题

    A. Hongcow Learns the Cyclic Shift 题目连接: http://codeforces.com/contest/745/problem/A Description Hon ...

随机推荐

  1. C#LeetCode刷题之#55-跳跃游戏(Jump Game)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3674 访问. 给定一个非负整数数组,你最初位于数组的第一个位置. ...

  2. 深度强化学习:Deep Q-Learning

    在前两篇文章强化学习基础:基本概念和动态规划和强化学习基础:蒙特卡罗和时序差分中介绍的强化学习的三种经典方法(动态规划.蒙特卡罗以及时序差分)适用于有限的状态集合$\mathcal{S}$,以时序差分 ...

  3. java.util.Scanner中hasNext()方法和next()方法的区别

    先说结论: 两者均根据空格划分数据 两者在没有数据输入时均会等待输入 next()方法会将空格划分的数据依次输出,运行一次,输出一个 hasNext()方法会跟着next()方法移动,当前数据不为空, ...

  4. DeepLab系列

    论文: (DeepLabV1)Semantic image segmentation with deep convolutional nets and fully connected CRFs (De ...

  5. Vue在v-for中给css传递一个数组参数

    需求就是将很多个数据,以进度条的形式展示在页面上,形成一个可视化. 接下来是html代码 <!DOCTYPE html> <html> <head> <tit ...

  6. 树莓派 4B VNC Viewer 显示 cannot currently show the desktop 的解决方法 (图文)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/ZChen1996/article/de ...

  7. HahMap(jdk=1.8)源码解读

    简介:岁月磨平了人的棱角,让我们不敢轻易的去放手,即使它在你心中并不那么重要,你依旧害怕失去它,不是舍不得,是内心的迷茫. 一 : 创建HashMap HashMap<Object, Objec ...

  8. windows10永久激活工具 新版win10激活工具(绝对有效的永久激活工具)

    来看这篇文章的,都用过KMS了吧?对!KMS是批量激活的,激活时间是一年,如果给女神激活,此法首选呀!!!但是帮基友激活,过了一年又来找自己,作为程序员的你,脸上是不是有点挂不住然后又不想花钱去买某宝 ...

  9. WKWebView 网络请求Header 丢失

    WKWebView 是苹果手机上主要的H5加载控件,它相比UIWebView 有诸多优势.在次不做比较,但是它的坑缺比较多.网上也有很多的例子但是做的比较好的真不多,我在这里推荐俩博客供大家参考.ht ...

  10. 一进“dos”就自动进入上次的目录

    这个原来在别的地方发过,但是后来发现有bug,处理windows下带有空格的长文件夹会出错.因为原来那个帖子已经不能编辑了,所以修改后写在这里. 这里的“dos”,就是windows下的命令行窗口,所 ...