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 水题,但是坑了很多人.需要注意以下就是正方 ...
随机推荐
- 飞码LowCode前端技术之画布的设计
简介 本章节从精准定位.分层设计.异步组件.拖拽四个方面分析飞码画布设计. 一.精准定位设计 飞码画布是一个套件,可对外提供画布能力.精准定位有两种情况,一是目标组件无子组件,而是目标组件有子组件. ...
- Web前端工程的装机必备软件
前言 最近作者的电脑 C 盘变红了,这让我很难受(有点小强迫症),所以准备重新安装下系统,顺便把 C 盘扩大点. 注意: 操作系统是 windows 11 23H2. 所有的命令行都是使用 Windo ...
- [c/c++][考研复习笔记]内部排序篇学习笔记
考研排序复习笔记 插入排序 #include<stdio.h> #include<stdlib.h> #define MaxSize 9 //折半插入排序 void ZBIns ...
- 解决GET http://192.168.41.103:9528/sockjs-node/info?t=1678639328658 net::ERR_CONNECTION_TIMED_OUT
问题现象 解决办法 找到依赖/node_modules/sockjs-client/dist/sockjs.js注释掉下面的一行代码
- pytorch学习笔记——加载checkpoint时,程序报错,存在GPU和CPU不同步的情况
当我们需要加载之前训练的checkpoint的时候,有时候会发现之前能训练的代码无法继续训练. 这时候很有可能加载优化器的步骤在加载模型前面,这样可能会导致优化器的参数仍然在CPU上,因此代码需要由原 ...
- NetSuite 开发日记 —— 非空判断
使用N/util模块进行非空判断,N/util模块说明:https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_45 ...
- git报错解决,warning: could not find UI helper 'git-credential-manager-ui'
在克隆远程代码时,可能遇到这样的报错 warning: could not find UI helper 'git-credential-manager-ui' 这样的报错经常会在我们换了一台电脑或者 ...
- ElasticSearch之cat repositories API
命令样例如下: curl -X GET "https://localhost:9200/_cat/repositories?v=true&pretty" --cacert ...
- 通过 KernelUtil 截取 QQ / TIM 客户端 ClientKey 详细教程
前言 众所周知,由于最新版本 QQ 9.7.20 已经不能通过模拟网页快捷登录来截取 Clientkey,估计是针对访问的程序做了限制,然而经过多方面测试,诸多的地区.环境.机器也针对这种获取方法做了 ...
- Spring Boot整合Spring Data连接postgreSQL完成简单的CRUD操作
导入jpa依赖和postgresql依赖: <!-- jpa依赖 --> <dependency> <groupId>org.springframework.boo ...