当前位置:网站首页>“蔚来杯“2022牛客暑期多校训练营5 B、C、F、G、H、K
“蔚来杯“2022牛客暑期多校训练营5 B、C、F、G、H、K
2022-08-04 12:23:00 【eyuhaobanga】
二分答案+排序,时间复杂度O(nlognlogn)
AC代码:
#include <bits/stdc++.h> #define rep(i,a,n) for(int i=a;i<n;i++) using namespace std; using LL = long long; struct node { LL cost, id; }a[100010], b[100010]; int n, m; bool check(LL mid) { LL cnt = 0; for (int i = 0; i < mid; i++) { cnt += b[i].cost; } return cnt > m; } bool cmp(node a, node b) { if (a.cost != b.cost) { return a.cost < b.cost; } return a.id < b.id; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); while (cin >> n >> m) { for (int i = 0; i < n; i++) { cin >> a[i].cost; a[i].id = i + 1; } LL l = 0, r = n; while (l + 1 < r) { LL mid = (l + r) >> 1; for (int i = 0; i < n; i++) { b[i] = a[i]; b[i].cost += mid * b[i].id; } sort(b, b + n, cmp); if (check(mid)) { r = mid; } else { l = mid; } } cout << l << '\n'; } return 0; }
按照题意模拟,有没访问到的地方、YES\NO个数都大于1、YES\NO个数相同、出现>1个YES和NO都出现过的位置、假如没有YES和NO都出现过的位置,但有某个位置YES和NO只出现了一次,因为错误的地方至多出现一个,无法判断这里是1还是0----以上均输出-1,否则输出ans,
AC代码:
#include <iostream> #include <cstdio> #include <queue> #include <deque> #include <stack> #include <string> #include <cstring> #include <numeric> #include <functional> #include <cstdlib> #include <vector> #include <set> #include <map> #include <algorithm> #include <cmath> #include <iomanip> #define rep(i,a,n) for(int i=a;i<n;i++) using namespace std; using LL = long long; //head int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; while (cin >> n) { vector<pair<int, int>> a(n); for (int i = 0; i < 3 * n; i++) { int x; string s; cin >> x >> s; if (s == "YES") { a[x].first++; } else { a[x].second++; } } bool ok = true; string ans = ""; int cnt = 0, cnt1 = 0; for (int i = 0; i < n; i++) { if (a[i].first == 0 && a[i].second == 0) { ok = false; break; } else if (a[i].second > 1 && a[i].first > 1) { ok = false; break; } else if (a[i].first == a[i].second) { ok = false; break; } } if (ok) { for (int i = 0; i < n; i++) { if (a[i].first && a[i].second) { cnt++; } if (a[i].first + a[i].second == 1) { cnt1++; } if (a[i].first > a[i].second) { ans += '1'; } else { ans += '0'; } } if (cnt > 1) { cout << "-1\n"; } else if (cnt == 1) { cout << ans << '\n'; } else { if (cnt1) { cout << "-1\n"; } else { cout << ans << '\n'; } } } else { cout << "-1\n"; } } return 0; }
贪心
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> #include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double PI2 = PI * 2; const int MAXN = 2010; const double eps = 1e-6; int n; double xs[MAXN], ys[MAXN], rs[MAXN]; typedef std::pair<double, double> PDD; PDD cover[MAXN]; int bak; inline double pows(double x) { return x * x; } inline double absx(double x) { return x < 0 ? -x : x; } inline double reduce(double x) { x += PI2, x = x - (int) (x / PI2) * PI2; return x; } int main() { while (cin >> n) { double ans = 0; for (int i = 1; i <= n; ++i) scanf("%lf%lf%lf", xs + i, ys + i, rs + i); for (int i = 1; i <= n; ++i) { bak = 0; for (int j = i + 1; j <= n; ++j) { double D = sqrt(pows(xs[i] - xs[j]) + pows(ys[i] - ys[j])); if (D >= absx(rs[i] + rs[j]) - eps) continue; if (D <= absx(rs[i] - rs[j]) + eps) { if (rs[j] > rs[i]) cover[++bak] = PDD(0, PI2); continue; } double y = (D - (pows(rs[j]) - pows(rs[i])) / D) / 2; double angle = acos(y / rs[i]); double oa = reduce(atan2(xs[i] - xs[j], ys[i] - ys[j]) + PI / 2); double L = reduce(oa - angle), R = reduce(oa + angle); if (L - eps > R) cover[++bak] = PDD(L, PI2), cover[++bak] = PDD(0, R); else cover[++bak] = PDD(L, R); } std::sort(cover + 1, cover + 1 + bak); ans += PI2 * rs[i]; double lstl = 0, lstr = 0; for (int j = 1; j <= bak; ++j) { const double fx = cover[j].first, sx = cover[j].second; if (fx > lstr) { ans -= (lstr - lstl) * rs[i]; lstl = fx; } lstr = std::max(lstr, sx); } ans -= (lstr - lstl) * rs[i]; } printf("%.7lf\n", ans); } return 0; }
马拉车或者pam求以‘k’或‘f’或‘c'为结尾的回文串的个数
AC代码:
#include <bits/stdc++.h> #define rep(i,a,n) for(int i=a;i<n;i++) using namespace std; using LL = long long; char s[2000001]; int len[2000001], n, num[2000001], fail[2000001], last, cur, pos, trie[2000001][26], tot = 1; int getfail(int x, int i) { while (i - len[x] - 1 < 0 || s[i - len[x] - 1] != s[i]) { x = fail[x]; } return x; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); while (cin >> n) { cin >> s; vector<int> v; fail[0] = 1; len[1] = -1; for(int i = 0; i <= n - 1; i++) { pos = getfail(cur, i); if (!trie[pos][s[i] - 'a']) { fail[++tot] = trie[getfail(fail[pos], i)][s[i] - 'a']; trie[pos][s[i] - 'a'] = tot; len[tot] = len[pos] + 2; num[tot] = num[fail[tot]] + 1; } cur = trie[pos][s[i] - 'a']; last = num[cur]; v.push_back(last); } map<char, int> mp; for (int i = 0; i < n; i++) { mp[s[i]] = mp[s[i]] + v[i]; } cout << mp['k'] << " " << mp['f'] << " " << mp['c'] << '\n'; } return 0; }
面积为圆的面积+阴影多边形圆外面的面积,即整个圆的面积+(四个三角形面积-半圆的面积)
AC代码:
#include <iostream> #include <cstdio> #include <queue> #include <deque> #include <stack> #include <string> #include <cstring> #include <numeric> #include <functional> #include <cstdlib> #include <vector> #include <set> #include <map> #include <algorithm> #include <cmath> #include <iomanip> #define rep(i,a,n) for(int i=a;i<n;i++) using namespace std; using LL = long long; //head int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; while (cin >> n) { double r = 1.0 * n / 2; const double PI = acos(-1.0); cout << fixed << setprecision(9) << r * r * PI / 2 + 2 * r * r << '\n'; } return 0; }
可知一共有N对蓝牙耳机,Yasa拿走了k对耳机,那么剩下了N-k对耳机=2*(N-k)个耳机,如果要组成k+1对耳机,那么根据鸽巢原理,需要拿N-k+k+1->N+1个耳机保证能组成k+1对耳机,那么最少能组成k+1对耳机的条件就是剩下的耳机对数起码要有k+1对
AC代码:
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; while (cin >> n >> k) { if (n >= 2 * k + 1) { cout << n + 1 << '\n'; } else { cout << "-1\n"; } } return 0; }
边栏推荐
猜你喜欢
程序猿七夕礼物-如何30分钟给女友快速搭建专属语聊房
【黑马早报】尚乘数科上市13天,市值超阿里;北大终止陈春花聘用合同;新东方花近200亿退学费和遣散费;张小泉75%产品贴牌代工...
Share | technology integration electronic fence function of scheduling system
A Survey of Multi-Label Classification under Supervised and Semi-Supervised Learning
基于BiLSTM的回归预测方法
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
开发小程序插件如何实现盈利?
#夏日挑战赛#OpenHarmony 给你的输入法加点彩—星球崛起
如何用一条命令将网页转成电脑 App
【VBox】解决复制VBox虚拟机后提示硬盘UUID 已经存在的问题
随机推荐
分布式链路追踪Jaeger + 微服务Pig在Rainbond上的实践分享
yolo系列的head模块
《独行月球》猛药,治不了开心麻花内耗
OAuth2图文快速入门
树莓派入门
正则表达式
记我的第一篇CCF-A会议论文|在经历六次被拒之后,我的论文终于中啦,耶!
UMA&港理工&阿里提出SP-ViT,为视觉Transformer学习2D空间先验知识!
从数学角度和编码角度解释 熵、交叉熵、KL散度
#夏日挑战赛#OpenHarmony 给你的输入法加点彩—星球崛起
动规(16)-并查集基础题——格子游戏
COVID-CT新冠肺炎检测(DenseNet网络)
新消费、出海、大健康......电子烟寻找“避风港”
监督和半监督学习下的多标签分类综述
2022上半年各银行理财子公司深耕差异化发展,净值型产品数量增加
【UML】信息系统分析与设计知识点总结
使用Stream多年,collect还有这些“骚操作”?
分布式链路追踪Jaeger + 微服务Pig在Rainbond上的实践分享
Tapdata 开源项目基础教程:功能特性及实操演示
广告电商系统开发