Codeforces Round #748 (Div. 3) A/B/C/D1

题单


目录

A. Elections

B. Make it Divisible by 25

C. Save More Mice

D1. All are Same

总结:


A. Elections

思路:

分类讨论。

代码:

#include <bits/stdc++.h>
typedef  long long int  LL;
using namespace std;
LL max(LL a,LL b,LL c)
{
    LL maxn=max(a,b);
    maxn=max(maxn,c);
    return maxn;
}
int main()
{
    LL t;
    cin>>t;
    while(t--)
    {
        LL a,b,c;
        cin>>a>>b>>c;
        int maxn=max(a,b,c);
        if(a>b&&a>c)
        {
            cout<<0<<" "<<maxn-b+1<<" "<<maxn-c+1<<endl;
        }
        else if(b>a&&b>c)
        {
            cout<<maxn-a+1<<" "<<0<<" "<<maxn-c+1<<endl;
        }
        else if(c>a&&c>b)
        {
            cout<<maxn-a+1<<" "<<maxn-b+1<<" "<<0<<endl;
        }
        else
        {
            cout<<maxn-a+1<<" "<<maxn-b+1<<" "<<maxn-c+1<<endl;
        }
    }
}


B. Make it Divisible by 25

思路:

字符串处理。

reverse反转一下,然后找00,05,52,57 (正向的 00 50 25 75)

代码:

#include <bits/stdc++.h>
#define bbn 200005
#define maxint 2147483647
#define maxLLint 9223372036854775807
#define mod 1000000007 //1e9+7
const double eps=1e-7;
typedef  long long int  LL;
using namespace std;
int t;

void work()
{
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;

        int n=s.size();
        int minn=maxint;
        reverse(s.begin(),s.end());
        for(int i=0; i<=n-1; i++)
        {
            if(s[i]=='0')
            {
                for(int j=i+1; j<=n-1; j++)
                {
                    if(s[j]=='0'||s[j]=='5')
                    {
                        minn=min(minn,j-1);
                    }
                }
            }
            if(s[i]=='5')
            {
                for(int j=i+1; j<=n-1; j++)
                {
                    if(s[j]=='2'||s[j]=='7')
                    {
                        minn=min(minn,j-1);
                    }
                }
            }
        }
        cout<<minn<<'\n';
    }
}

int main()
{
    work();
    return 0;
}
 


C. Save More Mice

思路:

贪心,每次救最靠近洞穴的老鼠。

预先sort排序一下老鼠。

代码:

#include <bits/stdc++.h>
#define bbn 400005
#define maxint 2147483647
#define maxLLint 9223372036854775807
#define mod 1000000007 //1e9+7
const double eps=1e-7;
typedef  long long int  LL;
using namespace std;
int t;

void work()
{
    cin>>t;
    while(t--)
    {
        int p,k;
        int cat=0;
        int mouse=0;
        int a[bbn]= {};

        cin>>p>>k;
        for(int i=1; i<=k; i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+k+1);

        for(int i=k; i>=1; k--)
        {
            if(cat>=a[k])
            {
                break;
            }
            cat+=p-a[k];
            mouse++;
        }
        cout<<mouse<<'\n';
    }
}

int main()
{
    work();
    return 0;
}
 

D1. All are Same

思路:

求差值(差值>0)的最大公因数。

如果所有数相同,输出-1。

方便起见,用set去除重复数。

代码:

#include <bits/stdc++.h>
#define bbn 400005
#define maxint 2147483647
#define maxLLint 9223372036854775807
#define mod 1000000007 //1e9+7
const double eps=1e-7;
typedef  long long int  LL;
using namespace std;
int t;
int gcd(int a,int b)
{
    return b!=0?gcd(b,a%b):a;
}
void work()
{
    cin>>t;
    while(t--)
    {
        int n;
        set<int>s;
        set<int>q;
        cin>>n;

        for(int i=1,x; i<=n; i++)
        {
            cin>>x;
            s.insert(x);
        }

        if(s.size()==1)
        {
            cout<<-1<<endl;
            continue;
        }
        for(auto i:s)
        {
            for(auto j:s)
            {
                if(i==j)
                {
                    continue;
                }
                else
                {
                    q.insert(abs(i-j));
                }
            }
        }
        int ans;
        for(int i:q)
        {
            ans=i;
            break;
        }
        for(int i:q)
        {
            ans=gcd(ans,i);
        }
        cout<<ans<<endl;
    }
}

int main()
{
    work();
    return 0;
}
 

总结:


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值