牛客练习赛23

25 篇文章 0 订阅
18 篇文章 0 订阅

好久都没写程序了,手生了不少,看着曾经的队友都飞黄腾达,去各种牛校夏令营虐场了,自己也不能落下呢

来一场小练习赛,感觉还可以吧,做出了感觉可以做出的题,最后2min压哨过题,海星

A.托米的赌球

思路:这题刚看的时候,感觉会是个复杂的问题,背包或者组合之类的。不过偶然间发现,给的数据很巧,1、2、5刚好可以由高到低凑出所有数。于是,直接暴力模拟一遍就好了

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <map>
#include <cmath>
#include <string>
#include <queue>
#include <stack>

using namespace std;

const int maxn = 1e5+10;

long long an[13];

int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while(T--)
    {
        long long sum, a, b;
        cin >> a >> b;
        sum = a*100LL+b;
        memset(an,0,sizeof an);
        if(sum / 10000 > 0)
        {
            an[0] = sum / 10000;
            sum %= 10000;
        }
        if(sum / 5000 > 0)
        {
            an[1] = sum / 5000;
            sum %= 5000;
        }
        if(sum / 2000 > 0)
        {
            an[2] = sum / 2000;
            sum %= 2000;
        }
        if(sum / 1000 > 0)
        {
            an[3] = sum / 1000;
            sum %= 1000;
        }
        if(sum / 500 > 0)
        {
            an[4] = sum / 500;
            sum %= 500;
        }
        if(sum / 200 > 0)
        {
            an[5] = sum / 200;
            sum %= 200;
        }
        if(sum / 100 > 0)
        {
            an[6] = sum / 100;
            sum %= 100;
        }
        if(sum / 50 > 0)
        {
            an[7] = sum / 50;
            sum %= 50;
        }
        if(sum / 20 > 0)
        {
            an[8] = sum / 20;
            sum %= 20;
        }
        if(sum / 10 > 0)
        {
            an[9] = sum / 10;
            sum %= 10;
        }
        if(sum / 5 > 0)
        {
            an[10] = sum / 5;
            sum %= 5;
        }
        if(sum / 2 > 0)
        {
            an[11] = sum / 2;
            sum %= 2;
        }
        an[12] = sum;

        for(int i=0;i<12;i++)
        {
            cout << an[i] << " ";
        }
        cout << an[12] << endl;
    }
    return 0;
}

B.托米的划分

思路:这题感觉更复杂了,什么乱七不糟向下取整公式,读题劝退系列。然而,仔细分析不难发现,每次均分就是最好的贪心思路了,由于担心被卡时间,1000一下记忆化一下,然后注意一下数据范围,long long成功ac

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <map>
#include <cmath>
#include <string>
#include <queue>
#include <stack>

using namespace std;

const int maxn = 1e3+10;

long long re[maxn];

long long getScore(long long a)
{
    long long temp,temp1,temp2;
    temp = a/2;
    //cout << a << "*" << endl;
    if(a<=1e3)
    {
        if(re[a] == -1)
        {
            if(a%2==0)
            {
                temp1 = getScore(temp);
                re[a] = temp1 + temp1 + temp*temp;
            }
            else
            {
                temp1 = getScore(temp);
                temp2 = getScore(temp + 1);
                re[a] = temp1 + temp2 + temp*(temp+1);
            }
        }
        return re[a];
    }
    else
    {
        if(a%2==0)
        {
            temp1 = getScore(temp);
            return temp1 + temp1 + temp*temp;
        }
        else
        {
            temp1 = getScore(temp);
            temp2 = getScore(temp + 1);
            return temp1 + temp2 + temp*(temp+1);
        }
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(re,-1,sizeof re);
        re[1] = 0;
        long long n;
        scanf("%lld",&n);
        printf("%lld\n",getScore(n));
        //cout << re[5]<<"*"<<re[3]<<"*"<<re[2]<<"*"<<re[1]<< endl;
    }
    return 0;
}

C.托米的位运算

思路:这题的关键就是读题了,一开始将题意读为各数异或,结果题目完全不可做,直接跳过。后来又将题意读为各数或,导致写完代码突然发现样例都过不掉。不过这题感觉也是一道读题题。

可以将题意转化为:随意选k个数,使这k个数相与后的结果二进制低位连续最多 

因为是与,可以将转化为0,那么就暴力枚举了,枚举最多低位连续0的个数,然后枚举该位为1的所有数,看能否实现这种方案。这个主要就是位运算的书写问题了

时间复杂度O(31*1e5)完美

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <map>
#include <cmath>
#include <string>
#include <queue>
#include <stack>

using namespace std;

const int maxn = 1e5+10;

int a[maxn];
bool in[maxn];
//int ti[maxn];

int main()
{
    int n;
    scanf("%d",&n);
    //int maxt = 0,k=0;
    int k=0;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    /*    int temp = a[i];
        ti[i] = 0;
        while(temp)
        {
            if(temp%2==1)   break;
            temp/=2;
            ti[i]++;
        }
        if(maxt<ti[i])  maxt = ti[i];
        in[i] = false;
    }*/
    for(int v=30;v>=0;v--)
    {
        int re = (1LL<<31)-1;
        //cout << re << endl;
        //cout << re +1 << endl;
        for(int i=0;i<n;i++)
        {
            if((a[i]>>v)&1==1)
            {
                in[i] = true;
                re &= a[i];
                //cout << a[i] << "**" << endl;
            }
            else
            {
                in[i] = false;
            }
        }
        //cout << re << "*" << (1<<v) <<endl;
        if((re >= (1<<v))&&((re % (1<<v)) == 0))
        {
            break;
        }
    }
    for(int i=0;i<n;i++)
        if(in[i])
            k++;
    printf("%d\n",k);
    bool st = true;
    for(int i=0;i<n;i++)
    {
        if(in[i])
        {
            if(st)
            {
                printf("%d",a[i]);
                st = false;
            }
            else
            {
                printf(" %d",a[i]);
            }
        }
    }
    printf("\n");
    //cout << (4&5) <<endl;
    //cout << (18&10&13)<<endl;
    return 0;
}

D.托米的咒语

思路:其实这题可以说是读懂题意后最不好思考的了。一开始想的是统计每个字母个数,但不同位置组合也能产生不同效果,所以这种情况肯定不行。后来突然想到暴力枚举,因为刚好只有9位,枚举所有排列情况,看能不能实现,复杂度刚好是O(9!*3000),感觉差不多。事实证明方法没有问题,过了80%的数据,最后加个小小剪枝,因为感觉不可能3000串长都需要,于是统计每个字母剩余个数,若字母剩余不足,就直接退出,于是成功ac,完成四题伟业

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <map>
#include <cmath>
#include <string>
#include <queue>
#include <stack>

using namespace std;

const int maxn = 3e3+10;

char ss[maxn],s[maxn];
int num[9];
int nnum[9];
bool in[9];

int main()
{
    char a[] = "abcdefghi" ;
    for(int i=0;i<9;i++)    num[i] = 0;
    int tnum=0;
    scanf("%s",ss);
    int l = strlen(ss);
    int len = 1;
    s[0] = ss[0];
    num[s[0]-'a']++;
    if(num[s[0]-'a'] == 1)  tnum++;
    for(int i=1;i<l;i++)
        if(ss[i] != ss[i-1])
        {
            s[len] = ss[i];
            num[s[len]-'a']++;
            if(num[s[len]-'a'] == 1)  tnum++;
            len++;
        }
    if(tnum<9)
    {
        printf("0\n");
    }
    else
    {
        int sum = 0;
        //int num = 0;
        do
        {
            //num++;
            for(int i=0;i<9;i++)    nnum[i] = num[i],in[i] = true;
            int pos=0;
            for(int i=0;i<len&&pos<9;i++)
            {
                if(nnum[s[i]-'a']==0) break;
                else    nnum[s[i]-'a']--;
                if(a[pos] == s[i])
                {
                    in[a[pos]-'a'] = false;
                    pos++;
                }
                if(nnum[s[i]-'a']==0&&in[s[i]-'a']) break;
            }
            if(pos == 9)    sum++;
        }while(next_permutation(a,a+9));
        printf("%d\n",sum);
        //cout << num << endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值