Google Code Jam 2018: Qualification Round

这篇博客回顾了Google Code Jam 2018资格赛,重点讨论了如何解决Saving The Universe Again、Trouble Sort、Go, Gopher!和Cubic UFO这四道题目。Saving The Universe Again使用贪心策略,尽可能减少伤害;Trouble Sort通过奇偶分治简化排序;Go, Gopher!构建3x3正方形组合;Cubic UFO利用数学理论优化解题过程。" 112925557,10537485,Arduino舵机驱动:实现速度控制与正弦变化,"['Arduino', '伺服电机', '驱动程序', '调速控制', '脉冲信号']
摘要由CSDN通过智能技术生成

资格赛,除了最后一题比较有趣以外,其他都比较简单

Saving The Universe Again

贪心
一开始想着尽可能将S往前移,但是想了想觉得这样移动好多次,可能才等于后面的S往前移一位的成果。
所以就变成尽可能地将C往末尾移(末尾一段连续的C就不用考虑了),即,每一次移动减少的伤害最大。

int T,n;
vi a1,a2;

int main()
{
    cin>>T;
    int cases = 1;
    while(T--)
    {
        cin>>n;
        a1.clear();
        a2.clear();
        for(int i = 0, x; i < n; i++)
        {
            cin>>x;
            if(i&1)
                a2.pb(x);
            else
                a1.pb(x);
        }
        sort(a1.begin(),a1.end());
        sort(a2.begin(),a2.end());
        int ans = -1;
        for(int i = 0; i < n-1; i++)
        {
            if(i&1)
            {
                if(a2[i/2]>a1[i/2+1] && ans==-1)
                {
                    ans = i;
                    break;
                }
            }
            else
            {
                if(a1[i/2]>a2[i/2] && ans==-1)
                {
                    ans = i;
                    break;
                }
            }
        }
        if(ans == -1)
            printf("Case #%d: OK\n", cases);
        else
            printf("Case #%d: %d\n", cases, ans);
        cases++;
    }
    return 0;
}

Trouble Sort

想法一:

实现两种排序,比较,复杂度 O(N2) O ( N 2 )

想法二:

因为Trouble Sort仅会在奇数位和奇数位或偶数位和偶数位交换,所以将奇偶分开来排序,按顺序比较大小即可。

int main()
{
    speed
    int T, cases = 1;
    ll D, a[33];
    string str;
    cin >> T;
    while (T--)
    {
        cin >> D >> str;
        int n = str.size();
        ll sum = 0;
        int c = 0;
        ll tot = 0;
        for(int i = 0; i < n; i++)
        {
            a[i] = -1;
            if(str[i] == 'C')
                c++;
            else
            {
                a[i] = (1ll << c);
                sum += a[i];
                tot++;
            }
        }
        cout << "Case #" << cases++ << ": ";
        if(tot > D)
        {
            cout << "IMPOSSIBLE" << endl;
        }
        else
        {
            int ans = 0;
            while(sum > D)
            {
                for(int i = n-1; i > 0; i--)
                {
                    if (a[i-1] == -1 && a[i] > -1)
                    {
                        a[i] >>= 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值