【Codeforces Round #653 (Div. 3)】 A-D 详解

手速还是慢了点,小细节bug没注意到浪费了一点时间

A.Required Remainder

题意:找出n以内最大的k使得k%x==y

思路:贪心地找靠近n的解,先看看n % x余数是否大于等于y,是的话就用(n/x)*x,不是的话就往前一位(n/x-1)*x。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e6+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

int main()
{
    ll kase;
    cin>>kase;
     ll x, y , n;
     while(kase--)
     {
         cin>>x>>y>>n;
         ll cur = n / x;
         ll m = n % x;
         ll ans;
         if(m>=y) ans = cur*x + y;
         else ans = (cur-1)*x + y;
         cout<<ans<<'\n';
     }
    return 0;
}

B.Multiply by 2, divide by 6

题意:每步要么乘2要么除6(模0的话),求n->1的最小步数

思路:先考虑怎么样的数才可能有解,因为我最后的1肯定是除6得到的(本身1除外),那最后一步就一定要是6的倍数,换句话说,能凑得到6的倍数的才能到1,而我又有个2可以乘,那质因子里面只有3和2就有可能。那就先将数分解求出3和2的个数。然后先把已有的2用3消掉(除6),再用3乘2凑6 。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e6+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll Map[10];

int main()
{
    int kase;
    cin>>kase;
    while(kase--)
    {
        ll n; mem(Map,0);
        n = read();
        if(n==1)
        {
            cout<<0<<'\n';
            continue;
        }
        while(n%2==0)
        Map[2]++, n/=2;
        while(n%3==0) Map[3]++, n/=3;
        if(n==2) Map[2]++, n = 1; if(n==3) Map[3]++, n = 1;
        if(n!=1)
        cout<<-1<<'\n';
        else
        {
            if(Map[2]>Map[3])
            cout<<-1<<'\n';
            else
            {
                ll ans = 0;
                ans += Map[2];
                Map[3] -=  Map[2];
                ans += Map[3] *2;
                cout<<ans<<'\n';
            }
        }
    }
    return 0;
}

C.Move Brackets

题意:左右括号串,问最少操作(把一个字符丢到头或者尾)是的括号匹配

这题放C题就很水了。用栈模拟一下,左括号不用管进栈,右括号发现不匹配就丢到后面去就好了。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e6+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

int main()
{
    ll kase;
    cin>>kase;
    while(kase--)
    {
        ll n; string s; n = read();
        cin>>s;
        stack<char> s1;
        ll ans = 0;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='(')
            {
                s1.push(s[i]);
            }
            else if(s[i]==')')
            {
                if(s1.size()==0)
                {
                    ans++;
                    s += s[i];
                }
                else s1.pop();
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}

D. Zero Remainder Array

题意:每步同时a[i]+x,x+1,或者只x+1,问最少多少步可以使得序列模k为0

思维题,直观看不太好判断。思路:
1.先将a[i]转化成模完k之后的,现在要把a[i]凑到k。
2.因为x也在变,不好直接给a[i]大的。观察每次x操作完一个a[i],x就会变成k- a[i] +1 (如对 2 凑到4, x=2时加进去后,x++到3)。这个结果是唯一确定的。
3.那我们就相当于知道了x在处理完n个数后变成了什么了。这里面就会有周期产生了。比如第一个样例1 1 2 模 3, x的处理完后状态分别是 3 3 2,因为我x是单增的,所以出现相同数的时候肯定就是走了一个周期。这个时候我们就看最大周期以及最后跑到的那个数就行了,比如这里跑了一个周期,外加多跑了3次,3*1+3=6。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll a[maxn];
ll b[maxn];
map<ll,ll> Map;


int main()
{
    ll kase;
    kase = read();
    while(kase--)
    {
        ll n, k; ll cnt = 0;  Map.clear();
        n = read(), k = read();
        rep(i,1,n)
        {
            a[i] = read(), a[i] = a[i]%k;
            if(a[i]==0) cnt++;
        }
        if(cnt==n)
        {
            cout<<0<<'\n';
            continue;
        }
        ll pos = 0;
        rep(i,1,n)
        {
            if(a[i]==0) continue;
            else b[pos++] = (k- a[i] + 1);
        }
        ll ans = 0; ll res= -1;
        ll idx;
        rep(i,0,pos-1)
        {
            Map[b[i]] ++;
            if(res<Map[b[i]]||res==Map[b[i]]&&idx<b[i])
            {
                res = Map[b[i]];
                idx = b[i];
            }
        }
        ans = (res-1)*k + idx;
        cout<<ans<<'\n';
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值