码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #653 (Div. 3)题解

时间:2020-06-29 17:00:21      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:vector   struct   比赛   pre   queue   round   nod   als   eof   

A.Required Remainder

#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int t,x,y,n;


int main()
{
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--){
        cin>>x>>y>>n;
        int tmp=n/x;
        if(tmp*x+y>n){
            tmp--;
        }
        tmp=tmp*x+y;
        cout<<tmp<<"\n";
    }
    return 0;
}

B. Multiply by 2, divide by 6

  • 传送门
  • 就依次用n/6,如果遇到不能整除的情况就看看能不能被3整除,如果也不能就输出-1
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int t,n;


int main()
{
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--){
        cin>>n;
        if(n==1){
            cout<<"0"<<"\n";
            continue;
        }
        int ans=0;
        int f=0;
        while(n!=1){
            if(n%6==0){
                n/=6;
                ans++;
            }else{
                if(n%3==0){
                    n*=2;
                    ans++;
                }else{
                    cout<<"-1"<<"\n";
                    f=1;
                    break;
                }
            }
        }
        if(!f){
            cout<<ans<<"\n";
        }
    }
    return 0;
}

C. Move Brackets

#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int t,n;
string s;
queue<char>q;

int main()
{
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--){
        cin>>n;
        cin>>s;
        int tmp=0;
        for(int i=0;i<s.size();++i){
            if(s[i]==‘(‘){
                q.push(s[i]);
            }
            if(s[i]==‘)‘){
                if(q.size()!=0){
                    q.pop();
                }else{
                    tmp++;
                }
            }
        }
        cout<<tmp<<"\n";
        while(!q.empty()) q.pop();
    }
    return 0;
}

D. Zero Remainder Array

  • 传送门
  • 比赛的时候有点被绕晕了,想的没问题,写假了,小改一下就行了
  • 大概就是先把a[i] mod k 然后sort一下,遍历,如果有一样的那么就依次+(k*p) (p=0,1,2,3...) 然后a[i]最大值
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
ll t, n, k, a[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin >> t;
    while (t--) {
        cin >> n >> k;
        memset(a, 0, sizeof(a));
        for (int i = 1; i <= n; ++i) {
            cin >> a[i];
            a[i] %= k;
        }
        sort(a + 1, a + 1 + n);
        ll r = 0;
        for (int i = 1; i <= n; ++i) if (!a[i]) r++;
        if (r == n) {
            cout << "0" << "\n";
            continue;
        }
        ll ans = 0;
        ll tmp = 0;
        a[n + 1] = 888;
        for (int i = 1; i <= n ; ++i) {
            if (a[i]) {
                if (a[i] == a[i - 1]) {
                    ans = max(ans, (a[i] / k + 2 + tmp) * k - a[i]);
                    tmp++;
                }
                else {
                    tmp = 0;
                    ans = max(ans, (a[i] / k + 1 + tmp) * k - a[i]);
                }
            }
        }
        cout << ans + 1 << "\n";
    }
    return 0;
}

E1. Reading Books (easy version)

  • 传送门
  • 桶+贪心就好了,开了三个vector来当桶
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int maxn = 2e5 + 10;
int n, k;
struct node {
    int t, a, b;
    //node(int a1,int a2,int a3){t=a1,a=a2,b=a3;}
};

vector<node>rec[3];

bool cmp(node a, node b)
{
    return a.t > b.t;
}


int main()
{
    ios::sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        node w;
        w.t = a, w.a = b, w.b = c;
        if (b == 1 && c == 1) rec[0].push_back(w);
        else {
            if (b == 1) rec[1].push_back(w);
            if (c == 1) rec[2].push_back(w);
        }
    }
    sort(rec[0].begin(), rec[0].end(), cmp);
    sort(rec[1].begin(), rec[1].end(), cmp);
    sort(rec[2].begin(), rec[2].end(), cmp);
    if (rec[0].size() + min(rec[1].size() , rec[2].size() ) < k) {
        cout << "-1";
        return 0;
    }
    int res = 0, ans = 0;
    while (res < k) {
        node a1, a2, a3;
        if (rec[0].size() != 0)  a1 = rec[0].back();
        if(rec[1].size()!=0)  a2 = rec[1].back();
        if (rec[2].size() != 0)  a3 = rec[2].back();
        if (rec[0].size() == 0) {
            rec[1].pop_back();
            rec[2].pop_back();
            res++;
            ans += (a2.t + a3.t);
            continue;
        }
        if (rec[1].size() == 0 || rec[2].size() == 0 ||a1.t < a2.t + a3.t) {
            rec[0].pop_back();
            res++;
            ans += a1.t;
        }
        else {
            rec[1].pop_back();
            rec[2].pop_back();
            res++;
            ans += (a2.t + a3.t);
        }
    }
    cout << ans;
    return 0;
}
  • 太菜了太菜了

Codeforces Round #653 (Div. 3)题解

标签:vector   struct   比赛   pre   queue   round   nod   als   eof   

原文地址:https://www.cnblogs.com/DrumWashingMachine-Lhy-NoobInCsu/p/13208778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!