5.15rating赛补题

B题

模拟题,自己当时做的时候有个地方排序的边界误加了1,导致爆了8发wa,其实只要按着题目意思模拟一遍就行了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>

using namespace std;

struct kind_max{
    string name;
    int id = 0;
    int taxi = 0;
    int pizza = 0;
    int girl = 0;
} kind[110];

int char_to_int(char c){
    return c - '0';
}

bool pizza_judge(int a, int b, int c, int d, int e, int f){
    if (char_to_int(a) > char_to_int(b) && char_to_int(b) > char_to_int(c) 
                && char_to_int(c) > char_to_int(d) && char_to_int(d) > char_to_int(e)
                && char_to_int(e) > char_to_int(f))
        return true;
    return false;
}

bool taxi_judge(int a, int b, int c, int d, int e, int f){
    if (char_to_int(a) == char_to_int(b) && char_to_int(b) == char_to_int(c) 
                && char_to_int(c) == char_to_int(d) && char_to_int(d) == char_to_int(e)
                && char_to_int(e) == char_to_int(f))
        return true;
    return false;
}

bool cmp_taxi(kind_max a, kind_max b){
    if (a.taxi == b.taxi)
        return a.id < b.id;
    return a.taxi > b.taxi;
}

bool cmp_pizza(kind_max a, kind_max b){
    if (a.pizza == b.pizza)
        return a.id < b.id;
    return a.pizza > b.pizza;
}

bool cmp_girl(kind_max a, kind_max b){
    if (a.girl == b.girl)
        return a.id < b.id;
    return a.girl > b.girl;
}

int main(){
    int n;
    cin >> n;
    int t;
    char a, b, c, d, e, f;
    for (int i = 0; i < n; i ++){
        cin >> t;
        cin >> kind[i].name;
        getchar();
        kind[i].id = i;
        while (t --){
            scanf("%c%c-%c%c-%c%c", &a, &b, &c, &d, &e, &f);
            if (pizza_judge(a, b, c, d, e, f))
                kind[i].pizza++;
            else if (taxi_judge(a, b, c, d, e, f))
                kind[i].taxi++;
            else
                kind[i].girl++;
            getchar();
        }
    }

    //taxi
    sort(kind, kind + n, cmp_taxi);
    cout << "If you want to call a taxi, you should call: ";
    cout << kind[0].name;
    for (int i = 1; i < n; i ++){
        if (kind[i].taxi != kind[i - 1].taxi)
            break;
        else
            cout << ", " << kind[i].name;
    }
       
    cout << "." << endl;

    //pizza
    sort(kind, kind + n, cmp_pizza);
    cout << "If you want to order a pizza, you should call: ";
    cout << kind[0].name;
    for (int i = 1; i < n; i ++){
        if (kind[i].pizza != kind[i - 1].pizza)
            break;
        else
            cout << ", " << kind[i].name;
    }
    cout << "." << endl;

    //girl
    sort(kind, kind + n, cmp_girl);
    cout << "If you want to go to a cafe with a wonderful girl, you should call: ";
    cout << kind[0].name;
   for (int i = 1; i < n; i ++){
        if (kind[i].girl != kind[i - 1].girl)
            break;
        else
            cout << ", " << kind[i].name;
    }
    cout << ".";
}

C题

这个题刚开始自己想用素数筛找到q的质因子,后来发现q数据范围到了1e13,而且数据是只有一组,素数筛显然不具有优势,并且在后面判断的时候也耗费时间,因此自己使用试除法分解质因数进行判断。

#include <iostream>
#include <cstdio>

using namespace std;

typedef long long ll;

const ll N = 1e6 + 10;

ll a[N];

int main(){
    ll q;
    cin >> q;

    //试除法分解质因数
    ll sum = 0, t = q;

    for (ll i = 2; i <= q / i; i ++){
        while (q % i == 0){
            sum++;
            q /= i;
            a[sum] = i;
        }
    }

    if (q > 1 && q != t)
        sum ++, a[sum] = q;
    
    if (sum != 2)
        cout << 1 << endl
             << a[1] * a[2] << endl;
    else
        cout << 2 << endl;
}

D题

在网上看题解时找规律自己实在理解不了,看其他题解发现要用并查集。因为自己比较菜之前没学过并查集,这两天就学了下并查集解决了这个题。

#include<stdio.h>
#include<iostream>
#include<algorithm>

using namespace std;

typedef long long ll;

const int N = 2100;

ll mod = 1e9 + 7;

ll n,m,k;

int p[N];

int find(int x)
{
    if (p[x] != x)
        p[x] = find(p[x]);
    return p[x];
}

bool Union(int x,int y)
{
    int fx = find(x);
    int fy = find(y);
    if(fx!=fy)
    {
        p[fx]=fy;
        return true;
    }
    return false;
}

ll po(ll x,ll y)
{
    ll cnt=1;
    while(y)
    {
        if(y%2)
        {
            cnt = (cnt * x) % mod;
        }
        x = ( x * x ) % mod;
        y/=2;
    }
    return cnt;
}

int main()
{
    int i,j;
    while(scanf("%lld%lld%lld", &n, &m, &k) != EOF)
    {
        for(i = 1 ; i <= n ;i++)
            p[i] = i;
        int x = n;
        for(i = 1; i <= n-k+1; i++)
        {
            for(j = 1; j <= k/2; j++)
            {
                if(Union(i+j-1, i+k-j))
                    x--;
            }
        }
        printf("%lld\n",po(m,x));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值