Codeforces Round #590 (Div. 3)

Codeforces Round #590 (Div. 3)

A. Equalize Prices Again

  • 思路:水题

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

int q, n, a, sum, ans;

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> q;
    while (q -- ){
        sum = 0;
        cin >> n;
        for (int i = 1; i <= n; i ++ ){
            cin >> a;
            sum += a;
        }
        if (sum % n == 0)
            ans = sum / n;
        else
            ans = sum / n + 1;
        cout << ans << "
";
    }
    return 0;
}

B. Social Network

  • 思路:模拟搞搞即可

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

int n, k, x;
deque<int> q;
set<int> st;

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    for (int i = 1; i <= n; i ++ ){
        cin >> x;
        if (st.count(x))
            continue;
        q.push_front(x);
        st.insert(x);
        if (q.size() == k + 1){
            st.erase(q.back());
            q.pop_back();
        }
    }
    cout << q.size() << "
";
    while (!q.empty()){
        cout << q.front() << " ";
        q.pop_front();
    }
    return 0;
}

C. Pipes

  • 思路:照着题意做

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

const int N = 2e5 + 10;

int q, n;
int mp[N][2];
string s;

bool dfs(int x, int y, int k, int n){
    if (x == n && y == 0)
        return false;
    else if (x == n && y == 1)
        return true;
    if (k <= 2 && mp[x][y] <= 2)
        return dfs(x + 1, y, 2, n);
    else if (k <= 2 && mp[x][y] > 2){
        if (y == 0)
            return dfs(x, 1 - y, 4, n);
        else
            return dfs(x, 1 - y, 5, n);
    }
    else if (k == 3){
        if (mp[x][y] > 2)
            return dfs(x, 1 - y, 4, n);
        else
            dfs(x + 1, y, 1, n);
    }
    else if (k == 4){
        if (mp[x][y] > 2)
            return dfs(x + 1, y, 6, n);
        else
            return false;
    }
    else if (k == 5){
        if (mp[x][y] > 2)
            return dfs(x + 1, y, 3, n);
        else
            return false;
    }
    else if (k == 6){
        if (mp[x][y] > 2)
            return dfs(x, 1 - y, 5, n);
        else
            return dfs(x + 1, y, 1, n);
    }
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> q;
    while (q -- ){
        cin >> n;
        cin >> s;
        for (int i = 0; i < n; i ++ )
            mp[i][0] = s[i] - '0';
        cin >> s;
        for (int i = 0; i < n; i ++ )
            mp[i][1] = s[i] - '0';
        if (dfs(0, 0, 1, n))
            cout << "YES
";
        else
            cout << "NO
";
    }
    return 0;
}

D. Distinct Characters Queries

  • 思路:用树状数组搞

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

const int N = 1e5 + 10;
const int M = 26;

int C[M][N];

inline int lowbit(int x){
    return x & (-x);
}

inline void add(int pos, int x, int val){
    while (x < N){
        C[pos][x] += val;
        x += lowbit(x);
    }
}

inline int sum(int pos, int x){
    int ans = 0;
    while (x > 0){
        ans += C[pos][x];
        x -= lowbit(x);
    }
    return ans;
}

inline int query(int l, int r, int pos){
    return sum(pos, r) - sum(pos, l - 1);
}

int len, q, op, pos, l, r, ans;
char c;
string s;

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> s;
    s.insert(0, " ");
    len = s.length() - 1;
    for (int i = 1; i <= len; i ++ )
        add(int(s[i] - 'a'), i, 1);
// debug    cout << s << "
";
    cin >> q;
    while (q -- ){
        cin >> op;
        if (op == 1){
            cin >> pos >> c;
            add(int(s[pos] - 'a'), pos, - 1);
            s[pos] = c;
            add(int(s[pos] - 'a'), pos, 1);
        }
        else{
            ans = 0;
            cin >> l >> r;
            for (int i = 0; i < M; i ++ )
                if (query(l, r, i) > 0)
                    ans ++ ;
            cout << ans << "
";
        }
    }
    return 0;
}

/*
abacaba
5
2 1 4
1 4 b
1 5 b
2 4 6
2 1 7

3
1
2
*/

/*
dfcbbcfeeedbaea
15
1 6 e
1 4 b
2 6 14
1 7 b
1 12 c
2 6 8
2 1 6
1 7 c
1 2 f
1 10 a
2 7 9
1 10 a
1 14 b
1 1 f
2 1 11

5
2
5
2
6
*/
原文地址:https://www.cnblogs.com/Misuchii/p/11801514.html