Codeforces Round #653 (Div. 3) A~D

题目链接

A Required Remainder

这个题用循环做超时好多次,最后发现用不到循环……

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int t;
    cin >> t;
    ll x,n,k,y;
    while(t--)
    {
        bool flag = 0;
        cin >> x >> y >> n;
        int tt = n%x;
        if(tt>=y)
        {
            cout << n-tt+y <<endl;
        }else{
            cout << n-tt-x+y <<endl;
        }
    }
    return 0;
}

B Multiply by 2, divide by 6

因为对数的操作只能×2或者÷6,所以只需要分析能否被3整除即可,后续再进行判断即可。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int t;
    cin >> t;
    ll x,n,k,y;
    while(t--)
    {
        ll ans = 0;
        cin >> n;
        if(n==1)
            cout << 0 <<endl;
        else if(n%3!=0)
            cout << -1 <<endl;
        else{
            while(n%6==0)
            {
                ans++;
                n/=6;
            }
            while(n%3==0)
            {
                ans += 2;
                n/=3;
            }
            if(n==1)
                cout << ans <<endl;
            else
                cout << -1 <<endl;
        }
    }
    return 0;
}

C Move Brackets

括号匹配问题,用stack做的

#include<bits/stdc++.h>
#include<stack>
using namespace std;
#define ll long long
int main()
{
    int t;
    cin >> t;
    int n;
    string s;
    while(t--)
    {
        stack<int>st;
        cin >> n >> s;
        int ans=0;
        for(int i=0;i<n;i++)
        {
            if(s[i]=='(')
                st.push(1);
            else{
                if(st.empty())
                    ans++;
                else
                    st.pop();
            }
        }
        cout << ans <<endl;
    }
    return 0;
}

D Zero Remainder Array

这个题就是查找、记录重复的问题。
首先记录数组中每个元素的操作数k− a i ​ a_i​ ai%k,若有重复,则找重复次数最多的那个操作数,操作的次数即[ (次数 - 1) × k + 该操作数 ].

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2e5+10;
ll a[maxn],k,b[maxn],num[maxn],top;
int main()
{
    ll t,n;
    cin>>t;
    while(t--)
    {
  	top=0;
  	cin>>n>>k;
  	int flag=1;
  	for(int i=1;i<=n;i++)
  	{
  	    cin >> a[i];
   	    a[i]%=k;
   	    if(a[i]) flag=0;
   	    a[i] = k-a[i];
  	}
  	if(flag)
 	 {
  	     cout<<0<<endl;
   	     continue;
 	 }
  	sort(a+1,a+1+n);
 	 ll last=a[1],temp=1,maxx=0;
  	for(int i=2;i<=n;i++)
  	{
  	    if(a[i]==k) break;
    	    if(a[i]==last) temp++;
  	    else
  	    {
   		 maxx=max(maxx,temp);
   		 b[++top]=last,num[top]=temp;
    		 last=a[i],temp=1;
   	    }
  	}
 	b[++top]=last,num[top]=temp;
  	maxx=max(maxx,temp);
  	ll ans=0;
 	 for(int i=1;i<=top;i++)
  	     if(num[i]==maxx)
                ans=max(ans,(maxx-1)*k+b[i]);
 	 cout<<ans+1<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浅梦曾倾

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

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

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

打赏作者

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

抵扣说明:

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

余额充值