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 水题,但是坑了很多人.需要注意以下就是正方 ...
随机推荐
- java 405_Http状态405-方法不允许
解决方法: 删除下列代码. super.doGet(req.resp); super.doPost(req.resp); 分析: 405错误一般指请求method not allowed 错误. 请求 ...
- Centos8.4自定义离线安装Nginx
一.简介 Nginx是一个web服务器也可以用来做负载均衡及反向代理使用. 目前使用最多的就是负载均衡,这篇文章主要介绍了centos8 安装 nginx. Nginx是一种开源的高性能HTTP和反向 ...
- 什么是 MySQL JDBC 连接池中最高效的连接检测语句?
在回答这个问题之前,首先我们看看 MySQL 中有哪些常用的 JDBC 连接池: c3p0 DBCP Druid Tomcat JDBC Pool HikariCP 这些连接池中,c3p0 是一个老牌 ...
- 数字孪生和GIS融合会为各自带来什么样的改变?
数字孪生和地理信息系统(GIS)是两个强大的技术,它们在各自领域发挥着重要作用.而当数字孪生与GIS融合时,将会为它们带来更加深远的改变和增益. 数字孪生技术以数字化的方式模拟和复制现实世界中的物理对 ...
- Python笔记一之excel的读取
本文首发于公众号:Hunter后端 原文链接:Python笔记一之excel的读取 这里我常用的 python 对于 excel 的读取库有两个,一个是 xlsxwriter 用于操作 excel 的 ...
- 基于 WinCC OA 构建分布式可视化平台
WinCC OA 的全称是:SIMATIC WinCC Open Architecture,是西门子工业自动化品牌SIMATIC系列的一部分.专门针对客户定制大型和/或复杂的应用以及需要满足特定系统 ...
- JDK1.8下载阿里云盘不限速
JDK1.8下载阿里云盘不限速 专门给你写篇jdk文章容纳方便下载 废话不多说直接上链接 「jdk-8u202-windows-x64.exe」https://www.aliyundrive.com/ ...
- 使用gradle的方式进行Springboot3的web开发(微服务版)
简要: 最近看了很多的Springboot3的项目,但是发现很多都是用maven来进行版本管理的,很少有用gradle来管理的,通过网上查找资料,看视频,终于自己写一个gradle管理的Springb ...
- 听说生鲜领军企业k8s集群都上云了,鱼会飞了?
在这个中秋都和国庆在一起的双节里,我们的小明还在辛辛苦苦的找工作,听说他经历了一段"难忘"的面试. 小明面对着面试官的"层层拷问",游刃有余的化解了这些难题. ...
- Element UI的第一个程序(标签使用)
1:Element UI 官方文档:https://element.faas.ele.me/ 2:Element UI是什么? 网站快速成型工具 Element,一套为开发者.设计师和产品经理准备的基 ...