Educational Codeforces Round 45 Editorial

A. Commentary Boxes

#include<bits/stdc++.h>
#define mem(arry, in) memset(arry, in, sizeof(arry))
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main()
{
    ll n, m, a, b;
    cin >> n >> m >> a >> b;
    n = n % m;
    ll res1 = (m - n) * a;
    ll res2 = n * b;
    cout << min(res1, res2) << endl;
}

B. Micro-World

题解:从小到大枚举,观察是否能被前一个(  i + 1 )数消掉。注意相同的数。

#include<bits/stdc++.h>
#define mem(arry, in) memset(arry, in, sizeof(arry))
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int maxn = 200005;

int n, K;
int a[maxn], use[maxn * 10];
int main() { cin >> n >> K; for(int i = 1; i <= n; i++){ int tp; cin >> tp; use[tp]++; } int m = 0; for(int i = 1; i <= 1000000; i++) if(use[i]) a[++m] = i; //for(int i = 1; i <= m; i++) cout << a[i] << " "; //cout << endl; int tp = a[1]; int cnt = 0; for(int i = 2; i <= m; i++){ if(a[i] <= tp + K) cnt += use[tp]; tp = a[i]; } cout << n - cnt << endl; return 0; }

C. Bracket Sequences Concatenation Problem

题解:能匹配的情况分别统计一下,比如"(" 和 ")","((" 和 "))",、、、、

#include<bits/stdc++.h>
#define mem(arry, in) memset(arry, in, sizeof(arry))
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int maxn = 300005;

int n, cnt, m;
int a[maxn], b[maxn];

stack<char> q;

int main()
{
    while(!q.empty()) q.pop();

    cin >> n;
    cnt = 0;
    for(int i = 0; i < n; i++){
        char s[maxn];
        scanf("%s", s);
        m = strlen(s);

        q.push(s[0]);
        for(int j = 1; j < m; j++){
            if(q.empty()) q.push(s[j]);
            else{
                if(s[j] == ')'){
                    if(q.top() == '(') q.pop();
                    else q.push(s[j]);
                }
                else q.push(s[j]);
            }
        }

        if(q.empty()) cnt++;
        else{
            int g = 0, t = 0;
            while(!q.empty()){
                if(q.top() == '(') g++;
                else t++;
                q.pop();
            }
            if(g && !t) a[g]++;
            if(!g && t) b[t]++;
        }
    }

    ll res = (ll)cnt * (ll)cnt;
    for(int i = 0; i <= maxn; i++) if(a[i] && b[i]) res += (ll)a[i] * (ll)b[i];
    cout << res << endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zgglj-com/p/9172891.html