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;
}

相关内容

热门资讯

斗金订购APP贵金属期货投资被...   斗金订购APP的投资者被广告宣传给诱导,注册就送什么现金,然后充值返现金卷等等这些宣传方式,都是...
哈易购APP非法期货交易欺骗投...   哈易购APP宣传可做白银铂金贵金属订购交易,但实际上并没有取得相关交易资质!哈易购APP本质上就...
消息称百度旗下昆仑芯瞄准500... 6 月 29 日消息,据《The Information》昨日援引知情人士消息,百度旗下 AI 芯片...
打造夏日消费新场景 第35届北... 北京商报讯(记者 翟枫瑞)6月29日消息,第35届北京国际燕京啤酒文化节新闻发布会在京举行。本届啤酒...
社保基金持仓数据出炉,一季度增... 最近各大上市公司一季度财报都公开了,咱们国家社保基金的持仓数据也全部曝光。目前社保拿着比亚迪价值44...
36氪首发 | 海思、中兴团队... 作者 | 乔钰杰 编辑 | 袁斯来 硬氪获悉,广州宸思通讯科技有限公司(以下简称“宸思科技”)近日完...
两天蒸发47亿市值!一纸税务通... 一纸税务通知书,能让一家百亿龙头两天蒸发47亿市值。 6月22日,北大荒(600598.SH)公告称...
SK海力士将投资1100万亿韩... SK集团会长崔泰源6月29日在韩国“三大重大计划”发布会上宣布,公司将投资1100万亿韩元扩大半导体...
两只A股,终止上市! 两家A股公司,即将摘牌。 6月29日,退市沪科(600608.SH)公告称,上海证券交易所将在202...
原创 M... 一家成立近十年的自动驾驶公司,在IPO时吸引了14家基石投资者认购近一半的发行股份,其中不乏奔驰、比...
基金忠言|国寿安保滤镜碎,三年... 图片来源:视觉中国 蓝鲸新闻6月29日讯(记者 祁和忠)保险系基金公司国寿安保总经理换人了。 6月2...
三星电机计划加码玻璃基板!相关... 6月29日,玻璃基板概念股午后有所回升, 华工科技(000988.SZ)逼近涨停, 彩虹股份(600...
拉萨海关持续壮大外贸经营主体 ...   新华网拉萨6月28日电(记者蒋梦辰)近日,记者从拉萨海关获悉,今年前5个月,西藏有进出口实绩的外...
机构:二季报临近,医药生物板块... 6月29日,华源证券发布了一篇医药生物行业的研究报告,报告指出,业绩期临近,产业链景气度有望再次迎来...
每日收评科创50放量涨超4.5... 财联社6月29日讯,三大指数全线收红,创业板指探底回升,科创50指数大涨4.61%。沪深两市成交额3...
6月多地土拍结构性升温:深圳单... 进入2026年6月,不少城市核心区地块集中诞生高溢价宗地,热度突出的城市包含深圳、杭州、长沙。 其中...
业绩炸裂!盛达资源半年预盈3.... 6月29日,贵金属矿山龙头盛达资源(000603.SZ)发布 2026 年半年度业绩预告,上半年业绩...
A股午后拉升三大股指收涨:半导... A股三大股指6月29日开盘涨跌互现。早盘沪强深弱,创指一度跌超2%。半导体午后拉升,带动两市上涨,沪...
原创 空... 前言 大家好,我是老金。 这几天,两幅极度割裂的画面放在一起,把我看笑了。 一边是在持续的热浪下,欧...