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

B

二分答案+排序,时间复杂度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;
}

C

按照题意模拟,有没访问到的地方、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;
}

F

贪心

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

G

马拉车或者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;
}

H

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

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

K

可知一共有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;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值