Codeforces Round 1013 (Div. 3)(部分题解A-E)

A. Olympiad Date

思路:这道题只需要满足日期出现的数字个数大于需要的个数就行,拿map存一下再判断一下即可。

void solve()
{
    int n, m, k;
    cin >> n;
    map<int, int> mp;
    vector<int> a(n+1);
    for (int i=1; i<=n; i++){
        cin >> a[i];
    }
    for (int i=1; i<=n; i++){
        mp[a[i]]++;
        if (mp[2] >= 2 && mp[0] >= 3 && mp[1] >= 1 && mp[3] >= 1 && mp[5] >= 1){
            cout << i << endl;
            return;
        }
    }
    cout << 0 << endl;
}

B. Team Training

思路:首先要把题意理解清楚,可以任意划分块,但要保证每块的团队价值大于等于x,并要求最多的块数,所以我们贪心的想肯定大于等于x的会自己贡献1,其余只能靠个数来影响贡献,因为会以每一块的最小的值来乘个数,所以我使用了一个大根堆方便查询,用ans记录每一块里的个数,当满足条件num++,并且ans设为0.

void solve()
{
    int n, m, k, x;
    cin >> n >> x;
    map<int, int> mp;
    vector<int> a(n+1);
    for (int i=1; i<=n; i++){
        cin >> a[i];
    }
    int num = 0;
    priority_queue<int> pq;
    for (int i = 1; i <= n; i++){
        if (a[i] >= x){
            num++;
        }
        else {
            pq.push(a[i]);
        }
    }
    int ans = 0;
    while (!pq.empty()){
        int t = pq.top();
        pq.pop();
        ans++;
        if (t*ans >= x){
            num++;
            ans = 0;
            continue;
        }
    }
    cout << num << endl;
}

C. Combination Lock

思路:首先还是得把题意理解清楚,第一层是顺序得从1到n,而第二层就是你要求出的序列。

然后就是观察法,对于样例2,一种可以的解为1 3 5 2 4,观察每次是哪个值是成立的关键,这里可以看出是先奇数,再偶数,所以我们可以退出偶数是肯定没有解的,因为这里会有要有奇偶之间的转换,而偶数不能达成这条件。

void solve()
{
    int n, m, k;
    cin >> n;
    if (n%2==0){
        cout << -1;
    }
    else {
        for (int i =1; i<=n; i+=2){
            cout << i << " ";
        }
        for (int i =2; i<=n; i+=2){
            cout << i << " ";
        }
    }
    cout << endl;
}

D. Place of the Olympiad

思路:这道题很明显的就是贪心,要让最长的桌最小,肯定n行就要尽可能地去均分这k个桌子,而我们就是要对该后的最长来讨论(也就是上取整后的值),这里要让其在m长下尽可能得小,我们二分枚举最小的桌长,就是让每一段之间有一个空位,这里满足m除以x+1的下取整在加上最后一段大于等于k。

void solve()
{
    int n, m, k;
    cin >> n >> m >> k;
    int l=0, r = 1e9+10;
    int num = (k+n-1)/n;                                   
    auto check = [&](int x)->bool
    {
        int res = 0;
        if (x >= num) return true;
        else {
            int ch = m/(x+1);
            res = ch*x+m-ch*(x+1);
        }
        return res >= num;
    };
    while (l+1 < r){
        int mid = (l+r)/2;
        if (check(mid)){
            r = mid;
        }
        else l = mid;
    }
    cout << r << endl;
}

 E. Interesting Ratio

思路:这道题被题面吓到了,以为是数论题,结果只需要打表看出规律就行了。

首先是我的打表函数:

bool isnp[MAXN];
vector<int>prime;
map<int, bool> mp;
void init(int n)
{
    for (int i = 2; i<=n; i++)
    {
        if (!isnp[i])
        {
            prime.push_back(i);
            mp[i] = 1;
        }
        for (int p:prime)
        {
            if (p*i>n) break;
            isnp[p*i] = true;
            if (i%p == 0) break;//最小质因子
        }
    }
}
int gcd(int a, int b){
    if (b == 0) return a;
    return gcd(b, a % b);
}
int lcm(int a, int b){
    return a / gcd(a, b) * b;
}
void solve()
{
    int n, m, k;
    cin >> n;
    int num = 0;

    vector<array<int, 2>> ans;
    for (int i=1; i<n; i++){
        for (int j = i+1; j<=n; j++){
            int res = lcm(i, j)/gcd(i, j);
            if (mp[res]) {
                num++;
                ans.push_back({i, j});
            }
        }
    }
    for (auto x:ans){
        cout << x[0] << " " << x[1] << endl;
    }
    cout << num << endl;
    cout << "========================" << endl;
}

得到的数据为(a,b):

// 1 2
// 1 3
// 1 5
// 2 4
// 4
// ========================
// 1 2
// 1 3
// 1 5
// 1 7
// 2 4
// 2 6
// 2 10
// 3 6
// 3 9
// 4 8
// 5 10
// 11
// ========================
// 1 2
// 1 3
// 1 5
// 1 7
// 1 11
// 1 13
// 1 17
// 1 19
// 1 23
// 1 29
// 1 31
// 2 4
// 2 6
// 2 10
// 2 14
// 2 22
// 2 26
// 2 34
// 3 6
// 3 9
// 3 15
// 3 21
// 3 33
// 4 8
// 4 12
// 4 20
// 4 28
// 5 10
// 5 15
// 5 25
// 6 12
// 6 18
// 6 30
// 7 14
// 7 21
// 8 16
// 8 24
// 9 18
// 9 27
// 10 20
// 10 30
// 11 22
// 11 33
// 12 24
// 13 26
// 14 28
// 15 30
// 16 32
// 17 34
// 49
// ========================

即b是a倍数,并且是素数倍,所以我们只要枚举a的值其最小的组合的二倍要小于等于n,再二分地去找其有几个素数倍是小于等于n的。

const int N = 1e8+10;
const int MAXN = 1e8+10;


bool isnp[MAXN];
vector<int>prime;
void init(int n)
{
    for (int i = 2; i<=n; i++)
    {
        if (!isnp[i])
        {
            prime.push_back(i);
        }
        for (int p:prime)
        {
            if (p*i>n) break;
            isnp[p*i] = true;
            if (i%p == 0) break;//最小质因子
        }
    }
}
void solve()
{
    int n, m, k;
    cin >> n;
    int num = 0;
    for (int i = 1; i*2<=n; i++){
        int l = -1, r = prime.size()+1;
        while (l+1 < r){
            int mid = (l+r)/2;
            if (i*prime[mid] <= n){
                l = mid;
            }
            else {
                r = mid;
            }
        }
        // cout << l+1 << endl;
        num += (l+1);
    }
    cout << num << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    init(N);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    
    return 0;
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wirepuller_king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值