Goolge Kick Start Round A 2020 (A ~ D题题解)
A. Allocation
题意
给出 \(N\) 栋房子的价格,第 \(i\) 栋房子的价格为 \(A_i\),你有 \(B\) 美元,问最多可以买多少栋房子?
思路
典型的贪心问题,将所有的房子按价格从低到高排序后选取即可。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int _, Case;
void solve() {
Case++;
int n, b;
cin >> n >> b;
int a[n];
for (int i = 0; i < n; ++i) cin >> a[i];
sort(a, a + n);
int ans = 0;
for (int i = 0; i < n; ++i) {
if (b < a[i])
break;
else
b -= a[i], ans++;
}
cout << "Case #" << Case << ": " << ans << endl;
}
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> _;
while (_--) solve();
}
B. Plates
题意
有 \(N\) 叠盘子,每叠有 \(K\) 个盘子,每个盘子有个 beauty value,现在要拿 \(P\) 个盘子,使得 beauty value 之和最大。拿盘子的条件:如果一个盘子的上面的盘子都被拿走了,才能拿到这个盘子。
思路
本题有一点点像多重背包。首先计算每叠盘子各自的前缀和 \(sum[N][K]\),设 \(dp[i][j]\) 表示前 \(i\) 堆盘子中取 \(j\) 个盘子的 beauty value 的最大值,转移方程为:
\]
代码
#include <bits/stdc++.h>
#define ms(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int N = 1510;
int _, Case = 0;
int n, p, k, x;
int sum[N][N], a[N][N], dp[N][N];
void solve() {
ms(sum, 0), ms(a, 0), ms(dp, 0);//初始化
cin >> n >> k >> p;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= k; ++j) cin >> a[i][j];
cout << "Case #" << ++Case << ": ";
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= k; ++j) sum[i][j] = sum[i][j - 1] + a[i][j];
for (int i = 0; i <= k; ++i) dp[1][i] = sum[1][i];
for (int i = 2; i <= n; ++i)
for (int j = 1; j <= p; ++j) //这里不需要太大,到p就够了
for (int l = 0; l <= min(j, k); ++l)//接下来就是写状态转移方程了
dp[i][j] = max(dp[i][j], dp[i - 1][j - l] + sum[i][l]);
cout << dp[n][p] << endl;
}
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
for (cin >> _; _; _--) solve();
}
C. Workout
题意
给定 \(N\) 个严格递增的数,往这 \(N\) 个数中间插 \(K\) 个数,插入后要使所有的数仍然保持严格递增,而且要保证相邻两数的最大绝对值之差最小,求最小的绝对值之差。
思路
想了挺久的,最后二分过的,典型的最大值最小的问题。判断函数的思路:传入参数 \(x\),表示答案为 \(x\),然后遍历每个数 \(num[i]\),如果 \(num[i+1]−num[i]>x\),就插入 \(num[i]+x\),如果插入的数的个数大于 \(K\) 个就返回 false,否则返回 true。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10;
int _, Case;
int n, k;
ll tmp[N], a[N];
bool check(int x) {
int cnt = 0;
for (int i = 0; i < n - 1; ++i)
while (tmp[i + 1] - tmp[i] > x) tmp[i] += x, cnt++;
if (cnt > k) return false;
return true;
}
void solve() {
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i];
cout << "Case #" << ++Case << ": ";
int l = 1, r = inf;
while (l < r) {
int mid = (l + r) >> 1;
for (int i = 0; i < n; ++i) tmp[i] = a[i];
if (check(mid))
r = mid;
else
l = mid + 1;
}
cout << r << endl;
}
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
for (cin >> _; _; _--) solve();
}
D. Bundling
题意
给定 \(N\) 个字符串,把它们分组,每组 \(K\) 个。每组的分数是该组所有字符串的最长公共前缀。求最大的所有组的分数和。
思路
很容易想到前缀树 (字典树 / trie 树,相关算法讲解),建完树后从根节点 dfs,同时记录深度 d,然后从叶子节点回溯,统计每个节点出现的个数 cnt,如果某个节点 \(u\) 的 \(cnt[u]≥K\),那么说明有 K 个字符串的前缀是以该节点结尾,深度 d 表示它们的前缀的长度,由于是从叶子节点回溯的,所以一定是最长公共前缀,所以 \(ans=ans+d\),同时 \(cnt[u]\) 减去 \(k\),即这 \(k\) 个字符串已经分完组,不再分到其他组。
代码
#include <bits/stdc++.h>
#define ms(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10;
int trie[N << 2][30], tot = 1;
int cnt[N << 2], n, k, _, Case;
ll ans;
string s;
void init() { ms(trie, 0), ms(cnt, 0), tot = 1, ans = 0; }
void insert(string s) {
int p = 0;
for (auto c : s) {
int idx = c - 'A';
if (!trie[p][idx]) trie[p][idx] = tot++;
p = trie[p][idx];
}
++cnt[p];
}
void dfs(int u, int d) { //起点,深度
for (int v = 0; v < 26; ++v) {
if (trie[u][v]) {
dfs(trie[u][v], d + 1);
cnt[u] += cnt[trie[u][v]];
}
}
ans += cnt[u] / k * d, cnt[u] %= k;
}
void solve() {
init();
cout << "Case #" << ++Case << ": ";
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> s, insert(s);
dfs(0, 0);
cout << ans << endl;
}
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
for (cin >> _; _; _--) solve();
}
这是第一次做Google平台的比赛,感觉难易度适中(适合我这种蒟蒻,感觉以后可以多写写)
Goolge Kick Start Round A 2020 (A ~ D题题解)的更多相关文章
- Codeforces Round #612 (Div. 2) 前四题题解
这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...
- Codeforces Round #579 (Div. 3) 套题 题解
A. Circle of Students 题目:https://codeforces.com/contest/1203/problem/A 题意:一堆人坐成一个环,问能否按逆时针或者顺时针 ...
- Codeforces Round #573 (Div. 2) D题题解
一.题目 Tokitsukaze, CSL and Stone Game Tokitsukaze和CSL正在玩一些石头游戏. 一开始,有n堆的石头,第i堆石头数记为 \(a_i\),两人轮 ...
- Codeforces Round #744 (Div. 3) G题题解
淦,最后一道题没写出来,...还是我太菜了,不过这个题确实比较有趣. G. Minimal Coverage 简化题意:就是你处在坐标轴的0点上,给你一个序列\(a_i\),每次你可以选择向左走\(a ...
- Codeforces Round #741 (Div. 2)部分题题解
我果然还是太菜了,就写了两道题....真是水死了.... A The Miracle and the Sleeper 简化题意:给定\(l,r\),求\(a\)%\(b\)的最大值,其中\(r> ...
- Google Kick Start Round G 2019
Google Kick Start Round G 2019 Book Reading 暴力,没啥好说的 #include<bits/stdc++.h> using namespace s ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 65 (Rated for Div. 2)题解
Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
随机推荐
- Object.assign () 和深拷贝
先看看啥叫深拷贝?啥叫浅拷贝? 假设B复制了A,修改A的时候,看B是否发生变化: 如果B跟着也变了,说明是浅拷贝,拿人手短!(修改堆内存中的同一个值) 如果B没有改变,说明是深拷贝,自食其力!(修改堆 ...
- java-EasyExcel模板导出
前言: 需求:根据自定义模板导出Excel,包含图片.表格,采用EasyExcel 提示:EasyExcel请使用 3.0 以上版本, 对图片操作最重要的类就是 WriteCellData<V ...
- 用最清爽的方式开发dotNet
用最清爽的方式开发dotNet 不管是官方自带模板还是其他开源搞的,总是一来一大堆,如果你也嫌弃这些过于臃肿,不如看看我这个方式 前提 假设我要做一个简单的api 方式 想到清爽,那肯定是简单方便,脑 ...
- MySQL笔记01: MySQL入门_1.3 MySQL启动停止与登录
1.3 MySQL启动停止与登录 1.3.1 MySQL启动与停止 MySQL数据库分为客户端和服务器端,只有服务器端服务开启以后,才可以通过客户端登录MySQL服务端. 首先,以管理员身份运行&qu ...
- python数据类型元组、列表、集合、字典相互嵌套
系统 Windows 10 专业工作站版22H2 软件 python-3.9.6-amd64.exe 拓展库: jupyter==1.0.0 notebook==7.0.6 1.元组嵌套 1.1 元组 ...
- GKCTF2020WP-Crypto Misc
Crypto 小学生的密码学 题目 e(x)=11x+6(mod26) 密文:welcylk (flag为base64形式) 我的解答: 考点:仿射密码,已知a,b 结果base64加密即可 flag ...
- 使用kubeadm在Centos8上部署kubernetes1.18
// 查看系统版本 cat /etc/centos-release CentOS Linux release 8.1.1911 (Core) // 如果系统环境为8.0(云服务器默认最大安装环境为8. ...
- JavaScript异步编程4——Promise错误处理
目录 1. 概述 2. 详论 3. 参考 1. 概述 在上一篇文章<JavaScript异步编程3--Promise的链式使用>中,通过Promise的链式使用,避免程序中多次嵌套回调(回 ...
- 记录:websoket切换页面后重复执行问题
问题描述 因为项目需求,实时播放执行信息.而项目的websoket只在这个页面,会有切换情况.从websoket连接得到执行列表数据.断开重连后会传递新连接数据+旧连接数据.也就是说,如果第一次进入页 ...
- C++篇:第十章_命名空间_知识点大全
C++篇为本人学C++时所做笔记(特别是疑难杂点),全是硬货,虽然看着枯燥但会让你收益颇丰,可用作学习C++的一大利器 十.命名空间 命名空间可以在全局作用域或其他命名空间内部定义,但不能在函数.结构 ...