• “蔚来杯“2022牛客暑期多校训练营5 B、C、F、G、H、K


    B

    二分答案+排序,时间复杂度O(nlognlogn)

    AC代码:

    1. #include
    2. #define rep(i,a,n) for(int i=a;i
    3. using namespace std;
    4. using LL = long long;
    5. struct node {
    6. LL cost, id;
    7. }a[100010], b[100010];
    8. int n, m;
    9. bool check(LL mid) {
    10. LL cnt = 0;
    11. for (int i = 0; i < mid; i++) {
    12. cnt += b[i].cost;
    13. }
    14. return cnt > m;
    15. }
    16. bool cmp(node a, node b) {
    17. if (a.cost != b.cost) {
    18. return a.cost < b.cost;
    19. }
    20. return a.id < b.id;
    21. }
    22. int main() {
    23. ios::sync_with_stdio(false);
    24. cin.tie(nullptr);
    25. while (cin >> n >> m) {
    26. for (int i = 0; i < n; i++) {
    27. cin >> a[i].cost;
    28. a[i].id = i + 1;
    29. }
    30. LL l = 0, r = n;
    31. while (l + 1 < r) {
    32. LL mid = (l + r) >> 1;
    33. for (int i = 0; i < n; i++) {
    34. b[i] = a[i];
    35. b[i].cost += mid * b[i].id;
    36. }
    37. sort(b, b + n, cmp);
    38. if (check(mid)) {
    39. r = mid;
    40. }
    41. else {
    42. l = mid;
    43. }
    44. }
    45. cout << l << '\n';
    46. }
    47. return 0;
    48. }

    C

    按照题意模拟,有没访问到的地方、YES\NO个数都大于1、YES\NO个数相同、出现>1个YES和NO都出现过的位置、假如没有YES和NO都出现过的位置,但有某个位置YES和NO只出现了一次,因为错误的地方至多出现一个,无法判断这里是1还是0----以上均输出-1,否则输出ans,

    AC代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. #include
    17. #define rep(i,a,n) for(int i=a;i
    18. using namespace std;
    19. using LL = long long;
    20. //head
    21. int main() {
    22. ios::sync_with_stdio(false);
    23. cin.tie(nullptr);
    24. int n;
    25. while (cin >> n) {
    26. vectorint, int>> a(n);
    27. for (int i = 0; i < 3 * n; i++) {
    28. int x;
    29. string s;
    30. cin >> x >> s;
    31. if (s == "YES") {
    32. a[x].first++;
    33. }
    34. else {
    35. a[x].second++;
    36. }
    37. }
    38. bool ok = true;
    39. string ans = "";
    40. int cnt = 0, cnt1 = 0;
    41. for (int i = 0; i < n; i++) {
    42. if (a[i].first == 0 && a[i].second == 0) {
    43. ok = false;
    44. break;
    45. }
    46. else if (a[i].second > 1 && a[i].first > 1) {
    47. ok = false;
    48. break;
    49. }
    50. else if (a[i].first == a[i].second) {
    51. ok = false;
    52. break;
    53. }
    54. }
    55. if (ok) {
    56. for (int i = 0; i < n; i++) {
    57. if (a[i].first && a[i].second) {
    58. cnt++;
    59. }
    60. if (a[i].first + a[i].second == 1) {
    61. cnt1++;
    62. }
    63. if (a[i].first > a[i].second) {
    64. ans += '1';
    65. }
    66. else {
    67. ans += '0';
    68. }
    69. }
    70. if (cnt > 1) {
    71. cout << "-1\n";
    72. }
    73. else if (cnt == 1) {
    74. cout << ans << '\n';
    75. }
    76. else {
    77. if (cnt1) {
    78. cout << "-1\n";
    79. }
    80. else {
    81. cout << ans << '\n';
    82. }
    83. }
    84. }
    85. else {
    86. cout << "-1\n";
    87. }
    88. }
    89. return 0;
    90. }

    F

    贪心

    AC代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. using namespace std;
    9. const double PI = acos(-1);
    10. const double PI2 = PI * 2;
    11. const int MAXN = 2010;
    12. const double eps = 1e-6;
    13. int n;
    14. double xs[MAXN], ys[MAXN], rs[MAXN];
    15. typedef std::pair<double, double> PDD;
    16. PDD cover[MAXN]; int bak;
    17. inline double pows(double x) { return x * x; }
    18. inline double absx(double x) { return x < 0 ? -x : x; }
    19. inline double reduce(double x) { x += PI2, x = x - (int) (x / PI2) * PI2; return x; }
    20. int main() {
    21. while (cin >> n) {
    22. double ans = 0;
    23. for (int i = 1; i <= n; ++i)
    24. scanf("%lf%lf%lf", xs + i, ys + i, rs + i);
    25. for (int i = 1; i <= n; ++i) {
    26. bak = 0;
    27. for (int j = i + 1; j <= n; ++j) {
    28. double D = sqrt(pows(xs[i] - xs[j]) + pows(ys[i] - ys[j]));
    29. if (D >= absx(rs[i] + rs[j]) - eps) continue;
    30. if (D <= absx(rs[i] - rs[j]) + eps) {
    31. if (rs[j] > rs[i])
    32. cover[++bak] = PDD(0, PI2);
    33. continue;
    34. }
    35. double y = (D - (pows(rs[j]) - pows(rs[i])) / D) / 2;
    36. double angle = acos(y / rs[i]);
    37. double oa = reduce(atan2(xs[i] - xs[j], ys[i] - ys[j]) + PI / 2);
    38. double L = reduce(oa - angle), R = reduce(oa + angle);
    39. if (L - eps > R) cover[++bak] = PDD(L, PI2), cover[++bak] = PDD(0, R);
    40. else cover[++bak] = PDD(L, R);
    41. }
    42. std::sort(cover + 1, cover + 1 + bak);
    43. ans += PI2 * rs[i];
    44. double lstl = 0, lstr = 0;
    45. for (int j = 1; j <= bak; ++j) {
    46. const double fx = cover[j].first, sx = cover[j].second;
    47. if (fx > lstr) {
    48. ans -= (lstr - lstl) * rs[i];
    49. lstl = fx;
    50. }
    51. lstr = std::max(lstr, sx);
    52. }
    53. ans -= (lstr - lstl) * rs[i];
    54. }
    55. printf("%.7lf\n", ans);
    56. }
    57. return 0;
    58. }

    G

    马拉车或者pam求以‘k’或‘f’或‘c'为结尾的回文串的个数

    AC代码:

    1. #include
    2. #define rep(i,a,n) for(int i=a;i
    3. using namespace std;
    4. using LL = long long;
    5. char s[2000001];
    6. int len[2000001], n, num[2000001], fail[2000001], last, cur, pos, trie[2000001][26], tot = 1;
    7. int getfail(int x, int i) {
    8. while (i - len[x] - 1 < 0 || s[i - len[x] - 1] != s[i]) {
    9. x = fail[x];
    10. }
    11. return x;
    12. }
    13. int main() {
    14. ios::sync_with_stdio(false);
    15. cin.tie(nullptr);
    16. while (cin >> n) {
    17. cin >> s;
    18. vector<int> v;
    19. fail[0] = 1;
    20. len[1] = -1;
    21. for(int i = 0; i <= n - 1; i++) {
    22. pos = getfail(cur, i);
    23. if (!trie[pos][s[i] - 'a']) {
    24. fail[++tot] = trie[getfail(fail[pos], i)][s[i] - 'a'];
    25. trie[pos][s[i] - 'a'] = tot;
    26. len[tot] = len[pos] + 2;
    27. num[tot] = num[fail[tot]] + 1;
    28. }
    29. cur = trie[pos][s[i] - 'a'];
    30. last = num[cur];
    31. v.push_back(last);
    32. }
    33. map<char, int> mp;
    34. for (int i = 0; i < n; i++) {
    35. mp[s[i]] = mp[s[i]] + v[i];
    36. }
    37. cout << mp['k'] << " " << mp['f'] << " " << mp['c'] << '\n';
    38. }
    39. return 0;
    40. }

    H

     面积为圆的面积+阴影多边形圆外面的面积,即整个圆的面积+(四个三角形面积-半圆的面积)

    AC代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. #include
    17. #define rep(i,a,n) for(int i=a;i
    18. using namespace std;
    19. using LL = long long;
    20. //head
    21. int main() {
    22. ios::sync_with_stdio(false);
    23. cin.tie(nullptr);
    24. int n;
    25. while (cin >> n) {
    26. double r = 1.0 * n / 2;
    27. const double PI = acos(-1.0);
    28. cout << fixed << setprecision(9) << r * r * PI / 2 + 2 * r * r << '\n';
    29. }
    30. return 0;
    31. }

    K

    可知一共有N对蓝牙耳机,Yasa拿走了k对耳机,那么剩下了N-k对耳机=2*(N-k)个耳机,如果要组成k+1对耳机,那么根据鸽巢原理,需要拿N-k+k+1->N+1个耳机保证能组成k+1对耳机,那么最少能组成k+1对耳机的条件就是剩下的耳机对数起码要有k+1对

    AC代码:

    1. #include
    2. using namespace std;
    3. int main() {
    4. ios::sync_with_stdio(false);
    5. cin.tie(0);
    6. int n, k;
    7. while (cin >> n >> k) {
    8. if (n >= 2 * k + 1) {
    9. cout << n + 1 << '\n';
    10. }
    11. else {
    12. cout << "-1\n";
    13. }
    14. }
    15. return 0;
    16. }

  • 相关阅读:
    java实现http/https请求
    开发笔记 —— Centos7 在急救模式下修改密码
    labelme标注的json数据集转换成coco数据集
    如何使用jsDelivr+Github 实现免费CDN加速?
    AWS Cloudformation入门项目实践
    Vue3速成
    MTK手机平台充电原理
    js笔试题(6)
    1375. 二进制字符串前缀一致的次数-前序遍历法
    【愚公系列】2022年11月 .NET CORE工具案例-CSRedis执行Lua脚本实现商品秒杀
  • 原文地址:https://blog.csdn.net/eyuhaobanga/article/details/126115523