2023牛客寒假算法基础集训营1(10/13)
admin
2024-05-13 00:25:30
0

World Final? World Cup! (I)

后面不用踢当且仅当即便后面全进,也赶不上另一方,注意还能踢几次球的计算

AC代码:

#include 
using namespace std;
using LL = long long;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin >> T;while (T--) {string s;cin >> s;int len = s.size();int cnt1 = 0, cnt2 = 0, ans = -1;for (int i = 0; i < len; i++) {if (i & 1) {if (s[i] == '1') {cnt2++;}if (cnt1 > cnt2 + 5 - (i + 1) / 2 || cnt2 > cnt1 + 5 - (i + 1) / 2) {ans = i + 1;break;}} else {if (s[i] == '1') {cnt1++;}if (cnt1 > cnt2 + 5 - i / 2 || cnt2 > cnt1 + 4 - i / 2) {ans = i + 1;break;}}}cout << ans << '\n';}return 0;
}

现在是,学术时间 (I)

贪心地想,只要每个教授发表自己的,引用量大于等于1即可获得1点H指数,所以统计非正数即可

AC代码:

#include 
using namespace std;
using LL = long long;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin >> T;while (T--) {int n;cin >> n;int ans = 0;vector a(n + 1);for (int i = 1; i <= n; i++) {cin >> a[i];if (a[i] > 0) {ans++;}}cout << ans << '\n';}return 0;
}

现在是,学术时间 (II)

根据数据范围发现,点一定都在第一象限内,所以讨论p点的相对位置即可

AC代码:

#include 
using namespace std;
using LL = long long;
struct point {long long x, y;point operator + (const point& p) {return {x + p.x, y + p.y};}point operator - (const point& p) {return {x - p.x, y - p.y};}double dot(const point& p) const {return 1.0 * x * p.x + 1.0 * y * p.y;}double cross(const point& p) const {return 1.0 * x * p.y - 1.0 * y * p.x;}double dist(const point& p) const {return sqrt(1.0 * (x - p.x) * (x - p.x) + 1.0 * (y - p.y) * (y - p.y));}
};
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin >> T;while (T--) {point a, b, o;o.x = 0;o.y = 0;cin >> a.x >> a.y >> b.x >> b.y;point c, d;c.x = a.x;c.y = 0;d.y = a.y;d.x = 0;double ans;function calc = [&](point x, point y) {double res = 1.0 * abs(x.x - y.x) * abs(x.y - y.y);return res;};if (b.x >= a.x && b.y >= a.y) {ans = (1.0 * a.x * a.y) / (1.0 * b.x * b.y);} else if (b.x < a.x && b.y >= a.y) {ans = max((calc(b, o) - calc(b, d)) / (calc(b, d) + 1.0 * a.x * a.y), (calc(b, c) - calc(b, a)) / (calc(b, a) + 1.0 * a.x * a.y));} else if (b.x >= a.x && b.y < a.y) {ans = max((calc(b, d) - calc(b, a)) / (calc(b, a) + 1.0 * a.x * a.y), (calc(b, o) - calc(b, c)) / (calc(b, c) + 1.0 * a.x * a.y));} else {ans = max({calc(b, o), calc(b, a), calc(b, c), calc(b, d)}) / (1.0 * a.x * a.y);}cout << fixed << setprecision(10) << ans << '\n';}return 0;
}

鸡算几何

根据题意,判断有没有操作三,就判断铁丝是否发生了镜像的变化,因为铁丝是不会发生形变的,而且ABC和DEF不一定对应,就考虑叉积判断修改C和F是否都在AB和DE线段的逆时针方向,然后再判断此时AB和DE,BC和EF长度是否相等

AC代码:

#include 
using namespace std;
using LL = long long;
struct point {double x, y;point operator + (const point& p) {return {x + p.x, y + p.y};}point operator - (const point& p) {return {x - p.x, y - p.y};}double dot(const point& p) const {return 1.0 * x * p.x + 1.0 * y * p.y;}double cross(const point& p) const {return 1.0 * x * p.y - 1.0 * y * p.x;}double dist(const point& p) const {return sqrt(1.0 * (x - p.x) * (x - p.x) + 1.0 * (y - p.y) * (y - p.y));}
};
const double eps = 1e-8;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin >> T;while (T--) {point a, b, c, d, e, f;cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y;cin >> d.x >> d.y >> e.x >> e.y >> f.x >> f.y;point x, y, i, j;x.x = a.x - b.x;x.y = a.y - b.y;y.x = c.x - b.x;y.y = c.y - b.y;i.x = d.x - e.x;i.y = d.y - e.y;j.x = f.x - e.x;j.y = f.y - e.y;if (x.cross(y) < 0) {point z = a;a = c;c = z;}if (i.cross(j) < 0) {point z = d;d = f;f = z;}x.x = a.x - b.x;x.y = a.y - b.y;y.x = c.x - b.x;y.y = c.y - b.y;i.x = d.x - e.x;i.y = d.y - e.y;j.x = f.x - e.x;j.y = f.y - e.y;if (sqrt(x.dot(x)) - sqrt(i.dot(i)) > eps || sqrt(y.dot(y)) - sqrt(j.dot(j)) > eps) {cout << "YES\n";} else {cout << "NO\n";}}return 0;
}

鸡玩炸蛋人

因为图是无向图,所以先考虑没有一个炸弹的情况时,在一个联通块里的方案数是联通块大小的平方,那么答案就是所有联通块大小平方的和,如果一个联通块里存在炸弹,那么答案为存在炸弹的联通块大小的平方,如果多个联通块存在炸弹,无解

AC代码:

#include 
using namespace std;
using LL = long long;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, m;cin >> n >> m;vector> G(n + 1);for (int i = 0; i < m; i++) {int u, v;cin >> u >> v;G[u].push_back(v);G[v].push_back(u);}int root = -1;vector c(n + 1);vector vis(n + 1);for (int i = 1; i <= n; i++) {cin >> c[i];if (c[i] > 0) {root = i;}}int sz = 0;function dfs = [&](int u) {for (auto v : G[u]) {if (!vis[v]) {vis[v] = true;sz++;dfs(v);}}};LL ans = 0;if (root == -1) {for (int i = 1; i <= n; i++) {if (!vis[i]) {vis[i] = true;sz = 1;dfs(i);ans = ans + 1LL * sz * sz;}}} else {vis[root] = true;sz = 1;dfs(root);for (int i = 1; i <= n; i++) {if (c[i] > 0 && !vis[i]) {cout << "0\n";return 0;}}ans = ans + 1LL * sz * sz;}cout << ans << '\n';return 0;
}

鸡格线

通过暴力跑f(x)=round(10*sqrt(x))发现,0无论跑多少次都是0,[1,99]跑有限次就达到99不再变化,[100,∞]也是跑有限次达到100后就不再变化,所以只需要存储下来非0的下标,以及多少次操作后就不再变化的操作数,操作时只修改还会变化的,否则从数组中删去,并修改所有数的和

AC代码:

#include 
using namespace std;
using LL = long long;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, m;cin >> n >> m;vector a(n + 1), b, c;vector vis(n + 1);LL sum = 0;for (int i = 1; i <= n; i++) {cin >> a[i];int x = a[i];int cnt = 0;if (x >= 100) {while (x != 100) {x = round(10 * sqrt(x));cnt++;}b.push_back(i);c.push_back(cnt);} else if (x > 0) {while (x != 99) {x = round(10 * sqrt(x));cnt++;}b.push_back(i);c.push_back(cnt);}sum += a[i];}while (m--) {int op;cin >> op;if (op == 2) {cout << sum << '\n';} else {int l, r, k;cin >> l >> r >> k;int p = lower_bound(b.begin(), b.end(), l) - b.begin();int q = upper_bound(b.begin(), b.end(), r) - b.begin();int len = b.size();if (len == 0) {continue;}if (p == q) {continue;} else {vector z;for (int i = p; i < q; i++) {c[i] -= k;if (c[i] <= 0) {z.push_back(i);if (a[b[i]] >= 100) {sum -= a[b[i]];sum += 100;} else {sum -= a[b[i]];sum += 99;}} else {sum -= a[b[i]];for (int j = 0; j < k; j++) {a[b[i]] = round(10 * sqrt(a[b[i]]));}sum += a[b[i]];}}int len1 = z.size();int cnt = 0;for (int i = 0; i < len1; i++) {b.erase(z[i] + b.begin() - cnt);c.erase(z[i] + c.begin() - cnt);cnt++;}}}}return 0;
}

本题主要考察了DFS

因为这张图是完整的,所以考虑一个边有多少个凸出来的以及需要有多少凹进去的与他对应,没有恰好相等的,一定需要最后一块拼图给补上缺口

AC代码:

#include 
using namespace std;
using LL = long long;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int T;cin >> T;while (T--) {int n;cin >> n;n *= n;vector a(n);vector cnt1(4), cnt2(4);for (int i = 1; i < n; i++) {cin >> a[i];for (int j = 0; j < 4; j++) {if (a[i][j] == '1') {cnt1[j]++;} else if (a[i][j] == '2') {cnt2[j]++;}}}int ans = 10;for (int i = 0; i < 4; i++) {if (cnt1[i] == cnt2[(i + 2) % 4]) {} else {if (cnt1[i] > cnt2[(i + 2) % 4]) {ans++;} else {ans--;}}}cout << ans << '\n';}return 0;
}

本题主要考察了dp

贪心地考虑,类似于100100100...1111放置,这样一定能保证坏区间是最小的

AC代码:

#include 
using namespace std;
using LL = long long;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, m;cin >> n >> m;string s = "";for (int i = 1; i <= n; i++) {if (n - i >= m) {if (i % 3 == 1) {s += '1';m--;} else {s += '0';}} else {while (m) {s += '1';m--;}break;}}int ans = 0;for (int i = 0; i < n - 2; i++) {int cnt = (s[i] == '1' ? 1 : 0) + (s[i + 1] == '1' ? 1 : 0) + (s[i + 2] == '1' ? 1 : 0);if (cnt >= 2) {ans++;}}cout << ans << '\n';return 0;
}

本题主要考察了运气

因为问团队和问第几个人相对独立,所以分别考虑他们两个各自期望操作数是多少

问团队:一次问出,两次问出,三次问出,四次问出,四次问出,最多需要四次就能判断出,那么期望就是(1+2+3+4+4)/5

问第几个人:一次问出,两次问出,三次问出,三次问出,最多需要三次就能判断出,那么期望就是(1+2+3+3)/4

两者加起来2.8+2.25=4.05,答案为32

AC代码:

32

本题主要考察了找规律

01背包,n个人背包容量为m,dp[i][j]就可以表示为前i个人占用j个空间时最大价值,因为题目没有给出物品体积,即每次需要枚举[0,m]所有体积,如果不选dp[i][j]=dp[i-1][j],如果选,首先占用的空间不能超过背包目前占用空间j,并且此时手里有的仙贝数要大于0,此时(这次还没分配)手里的仙贝数就是(m-j+所选物品的体积),所以产生的价值就是 所选物品体积/此时(这次还没分配)手里的仙贝数

AC代码:

#include 
using namespace std;
using LL = long long;
int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n, m;cin >> n >> m;vector> dp(n + 1, vector (m + 1));for (int i = 1; i <= n; i++) {for (int j = 0; j <= m; j++) {dp[i][j] = dp[i - 1][j];for (int k = 0; k <= m; k++) {if (j >= k && m - j + k > 0) {dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + 1.0 * k / (m - j + k));}}}}double ans = 0.0;for (int i = 0; i <= m; i++) {ans = max(ans, dp[n][i]);}cout << fixed << setprecision(10) << ans << '\n';return 0;
}

相关内容

热门资讯

海南自贸港“样板间”抢抓开放机... 中新网海口5月16日电 (记者 王子谦)洋浦经济开发区是海南自贸港“样板间”,也是外界观察自贸港建设...
净利增速2.98%,违规频发!... 近期,中信银行2025年年报与2026年一季报接连公布,报告显示,中信银行总资产站稳10万亿元台阶,...
原创 放... 全网的人几乎都在挤破头往海外大都市扎,可有一个女博主,却偏偏反着来。她拥有五百多万粉丝,本可以继续在...
原创 在... 在中国,买卖虚拟货币,到底行不行? 这个问题,很多人心里都犯嘀咕。有人说,法无禁止即可为;也有人说,...
龙粤慈善事业高质量发展与互联网... 近日,为加快培育数字慈善新生态,助力“善行边疆”活动走深走实,“龙粤慈善事业高质量发展与互联网公开募...
黄金大局已定:不出意外的话,2... 在投资领域,贵金属一直是备受关注的资产类别,尤其是黄金,其价格走势和投资价值牵动着无数投资者的心。随...
后巴菲特时代,伯克希尔哈撒韦新... 【导读】伯克希尔哈撒韦最新持仓公布!清仓亚马逊,建仓达美航空 中国基金报记者 张舟 伯克希尔哈撒韦“...
布朗46分胡金秋20+8 广厦... 【搜狐体育战报】北京时间5月16日CBA季后赛,主场作战的浙江浙商证券以111-102击败深圳马可波...
美联储任命鲍威尔担任临时主席 美国联邦储备委员会理事会5月15日发布公告,任命杰罗姆·鲍威尔担任美联储临时主席,直至凯文·沃什宣誓...
李从悠:白癜风患者,夏季防汗疹... 夏季高温多雨,白癜风患者皮肤屏障受损,出汗后汗液无法及时蒸发,易堵塞毛孔,诱发汗疹(热疹),汗疹引发...
最低涨价60元!4款非标茅台酒... 在飞天茅台涨价之后,部分非标茅台酒也提了价。 5月16日早间,贵州茅台自营渠道i茅台发布公告,宣布对...
邯郸10亿共享智造基金落地,撬... 图片为AI生成 据天眼查App显示,近日邯郸市共享智造股权投资基金(有限合伙)正式登记成立,总出资额...
AI制药行业深度:行业概况、市... 一、AI制药行业概况 1、AI药物研发概述 AI制药是指将NLP、深度神经网络,生成模型等AI技...
世界杯在即:国产彩电的出海故事... 球还没看,彩电先破防了 撰文/ 孟会缘 编辑/ 陈邓新 排版/ Annalee 国产彩电品牌,正深陷...
医疗健康领域投融资日报(5月1... 据亿欧数据统计,昨日(2026年5月15日)共披露16起投融资事件,涉及15家国内企业,1家国外企业...
深圳中创商业咨询携手海旗控股集... 海旗控股集团旗下宁波锦曼程新材料有限公司,自创立以来始终深耕高分子材料领域,秉承推动行业创新与可持续...
原创 关... 前言 大家好,我是老金。 国际地缘博弈的棋盘上,从来没有绝对的秘密,只有刻意或无意的战略试探,近期...
原创 欧... 今天来给大家聊一下最近的欧盟,自从特朗普说要来访华,欧洲的动作有点让人看不懂。从四月中旬到五月初,欧...
心系投资者 携手共行动 ——人... 为落实监管工作要求,切实维护金融消费者合法权益,在 “5・15 全国投资者保护宣传日” 当天,人保寿...
黄仁勋打卡蜜雪冰城 同款产品销... 财联社5月16日讯(记者 沈娇娇)5月15日上午,英伟达CEO黄仁勋现身北京南锣鼓巷,并且进入一家蜜...