Codeforces Round #653 (Div. 3)

58 篇文章 0 订阅
13 篇文章 1 订阅

A. Required Remainder

题意:给x,y,n,要找一个0-n之内的最大的数使得 k%x == y。

思路:n/x*x 就是 最接近n的,且被x整除的数。 加上y就行了。如果超过n了,还要减去一个x。

AC代码:

#include <bits/stdc++.h>
using namespace std;
 
signed main(){
    int t = 1;
    cin>>t;
    while(t--){
        int x,y,n;
        cin>>x>>y>>n;
        int ans = n/x*x+y;
        if(ans > n) ans -= x;
        cout<<ans<<endl;
    }
}

B. Multiply by 2, divide by 6

题意:给一个n,两种操作,1: n *= 2,2: n /= 6。 最后使得n为1。求最少操作次数。

思路:首先,这个数,必须只有2,3这两个因子,才能被6整除。所以如果不是,则无解。然后要统计2有多少个,3 有多少个。因为只能让2的数量增加,如果2的数量已经比 3 多了,则无解。反之,让2加到和3一样多,再除6把他们减掉。

AC代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
 
signed main(){
    int t = 1;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int cnt2 = 0;
        int cnt3 = 0;
        while(n != 1){
            if(n%2 == 0){
                cnt2 ++;
                n /= 2;
            }
            if(n%3 == 0){
                cnt3 ++;
                n /= 3;
            }
            if(n != 1 && n%2 != 0 && n%3 != 0)
                break;
        }
        if(n!=1 || cnt2 > cnt3){
            cout<<"-1"<<endl;
        }else{
            cout<<cnt3+(cnt3-cnt2)<<endl;
        }
 
    }
}

C. Move Brackets

题意:给定一个括号序列。可以移动任意一个到开头或者末尾。问最少移动多少次。可以变成合法括号序列。

思路:其实就是算有多少个不合法的。把不合法的移动到两边就行了。用cnt记录括号数量。碰到左括号+1,碰到右括号-1。答案就是变化过程中,cnt的绝对值的最大值就是答案。因为如果有x个不合法的左括号,移动末尾就好了,同理如果是右括号,移动到开头就好了。

AC代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
 
signed main(){
    int t = 1;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        string s;
        cin>>s;
        int res = 0;
        int cnt = 0;
        for(int i = 0 ; i < n ; i ++){
            if(s[i] == '(') cnt ++;
            else cnt --;
            if(cnt < 0){
                res = max(res,-cnt);
            }
        }
        if(cnt > 0 ){
            res = max(res,cnt);
        }
        cout<<res<<endl;
 
    }
}

D. Zero Remainder Array

题意:给定一个数组,然后有一个初始为0的x。每次一次操作有两种选择,把a[i]加上x,然后x自增1。或者x直接自增1。求最少操作次数,使得所有元素能够整除k。每个元素最多只能加一次x。

思路:将数组元素对k取模然后计数。记录最大的出现次数,因为x每次都会增加1。答案就是次数*k -(k- 最大的那个模数)

AC代码:

#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#define int long long
const int N = 4e5+10;
const int mod = 1e9+7;
using namespace std;
int a[N];
 
signed main(){
    int t = 1;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        map<int,int> mp;
        for(int i = 0 ; i < n ; i ++){
            cin>>a[i];
            a[i] = (k-(a[i]%k))%k;
            if(a[i]){
                mp[a[i]] ++;
            }
        }
        sort(a,a+n);
        int maxx = 0;
        int now = 0;
        for(int i = 0 ; i < n ; i ++){
            if(a[i]){
                if(mp[a[i]] >= maxx){
                    maxx = mp[a[i]];
                    now = a[i];
                }
            }
        }
        if(maxx == 0)
            cout<<0<<endl;
        else{
            //cout<<maxx<<" "<<now<<endl;
            cout<<maxx*k-(k-now-1)<<endl;
        }
 
    }
}

E1 - Reading Books (easy version)

题意:n本书,有的a喜欢,有的b喜欢。每本书阅读时间为ti。求选出一些书 使得 a,b喜欢的书数量大于k。且阅读时间和最小。 有的a,b都不喜欢!!

思路:简单贪心。交了一小时才发现有a,b都不喜欢的书。。。

AC代码:

#include <iostream>
#include <bits/stdc++.h>
#define int long long
#define mk make_pair
#define gcd __gcd
#define pb push_back
using namespace std;
const double eps = 1e-10;
const int mod = 1e9+7;
const int N = 1e6+7;
int n,m,k,t = 1,cas = 1;
int a[N],b[N],c[N];
int cnt1=0,cnt2=0,cnt3=0;

signed main(){
    cin>>n>>k;
    for(int i = 0 ; i < n ; i ++) {
        int x,y,z;
        cin>>x>>y>>z;
        if(y && z){
            c[cnt3++] = x;
        }else if(y){
            b[cnt2++] = x;
        }else if(z){
            a[cnt1++] = x;
        }
    }
    sort(a,a+cnt1);
    sort(b,b+cnt2);
    sort(c,c+cnt3);
    int p1 = 0,p2 = 0 , p3 = 0;
    int res = 0;
    int flag = 1;
    for(int i = 0 ; i < k ; i ++){
        if(p3 < cnt3){
            if((p1 >= cnt1 || p2 >= cnt2) || c[p3] < a[p1]+b[p2]){
                res += c[p3++];
            }else if(p1 < cnt1 && p2 < cnt2){
                res += a[p1++];
                res += b[p2++];
            }else{
                flag = 0;
                break;
            }
        }else if(p1 < cnt1 && p2 < cnt2){
            res += a[p1++];
            res += b[p2++];
        }else{
            flag = 0;
            break;
        }
        //cout<<p1<<" "<<p2<<" "<<p3<<endl;
    }
    if(flag) cout<<res<<endl;
    else cout<<-1<<endl;
}

/**
**/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值