Codeforces Round #828 (Div. 3) A - E

A.Number Replacement

void solve() {
    int n; cin >> n;
    vector<int> a(n), vis(n);
    for (int i = 0; i < n;i++) cin >> a[i];
    string s; cin >> s;
	map<int, char> mp;
	bool ok = true;
	for (int i = 0;i < n;i++) {
		if (!mp[a[i]]) {
			mp[a[i]] = s[i];
		}	
		else if (mp[a[i]] != s[i]) ok = false;
	}

	cout << (ok ? "YES\n" : "NO\n");
}

B.Even-Odd Increments

注意奇偶性的变化。

void solve() {
    int n, q; cin >> n >> q;
    vector<ll> a(n + 1);
    ll ans = 0, odd = 0, even = 0;
    for (int i = 1; i<= n;i++) {
    	cin >> a[i];
        ans += a[i];
        if (a[i] & 1) odd ++;
    }
    even = n - odd;
    while (q --) {
    	int k; ll d; cin >> k >> d;
    	if (k == 0) {
            ans += even * d;
            if (d & 1) {
                even = 0;
                odd = n;
            }
        }
        else {
            ans += odd * d;
            if (d & 1) {
                even = n;
                odd = 0;
            }
        }
        cout << ans << endl;
    }
}

C.Traffic Light

模拟。

void solve() {
    int n; char c; cin >> n >> c;
    string s; cin >> s; s = ' ' + s + s + ' ';
    if (c == 'g') {
        cout << 0 << endl;
        return;
    }
    int ans = 0;
    for (int i = 1;i <= n;i++) {
        if (s[i] == c) {
            for (int j = i + 1;j <= 2 * n;j++) {
                if (s[j] == 'g') {
                    ans = max(ans, j - i);
                    i = j;
                    break;
                }
            }
        }
    }
    cout << ans << endl;
}

D.Divisibility by 2^n

从大到小贪心乘上2的幂次。

void solve() {
    int n; cin >> n;
    int cnt = 0;
    for (int i = 1; i <= n;i++) {
    	int x; cin >> x;
    	while (x % 2 == 0) {
    		x /= 2;
    		cnt ++;
    	}
    }
    if (cnt >= n) cout << 0 << endl;
    else {
        int res = n - cnt;
        vector<int> s;
        for (int i = 2;i <= n;i += 2) {
            int j = i, k = 0;
            while (j % 2 == 0) {
                k ++;
                j /= 2;
            }
            if (k > 0) s.push_back(k);
        }
        int ans = 0;
        sort(s.rbegin(), s.rend());
        for (int i = 0; i < sz(s);i++) {
            if (res > 0) {
                ans ++;
                res -= s[i];
            }
        }
        if (res > 0) ans = -1;
        cout << ans << endl;
    }
}

E.Divisible Numbers (hard version)

枚举约数。\(10^9\)次方以内的数,约数个数最多为\(1344\)个。
https://www.cnblogs.com/ubospica/p/10392523.html

void solve() {
    ll a, b, c, d, ab; cin >> a >> b >> c >> d; ab = a * b;

    vector<ll> p1, p2;
    auto get = [&](ll x, vector<ll> &p)->void {
        for (ll i = 1;i * i <= x;i++) {
            if (x % i == 0) {
                p.push_back(i);
                if (i * i < x) p.push_back(x / i);
            }
        }
    };
    
    set<ll> s;
    get(a, p1); get(b, p2);
    for (auto x : p1) 
        for (auto y : p2) s.insert((ll)x * y);

    for (auto x : s) {
    	auto y = ab / x;
        x = c / x * x;
        y = d / y * y;
        if (x > a and y > b) {
            cout << x << ' ' << y << endl;
            return;
        } 
    }

    cout << "-1 -1\n";
}
posted @ 2022-10-17 16:17  Coldarra  阅读(68)  评论(0)    收藏  举报