Sdut vj individual competition on January 16, 2021

A - Abandoned Animal

Topic link

The idea of ​​the question is to buy items in the order of the input items, output impossible if not possible, output unique if there is a unique order, and output ambiguous if there are multiple cases.

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;

map<string , int> mp;
vector<int> a[N];
int b[N];
int n, k, m, ii, ans, buy1[N], buy2[N];
string s;

int main()
{
    
    
    memset(buy1, -1, sizeof buy1);
    memset(buy2, -1, sizeof buy2);
    int f = 0;
    cin >> n >> k;
    for(int i = 0; i < k; i ++)
    {
    
    
        cin >> ii >> s;
        if(mp.count(s)) a[ii].push_back(mp[s]);
        else
        {
    
    
            mp[s] = f;
            a[ii].push_back(f ++);
        }
    }
    cin >> m;
    for(int i = 0; i < m; i ++)
    {
    
    
        cin >> s;
        b[i] = mp[s];
    }
    ans = 0;
    int idx = 0;
    for(int i = 0; i < n && idx < m; i ++)
    {
    
    
        int len = a[i].size();
        for(int j = 0; j < len; j ++)
        {
    
    
            if(a[i][j] == b[idx])
            {
    
    
                buy1[idx] = i;
                idx ++;
                i --;
                break;
            }
        }
    }
    if(buy1[m - 1] != -1) ans ++;
    idx = m - 1;
    for(int i = n - 1; i >= 0 && idx >= 0; i --)
    {
    
    
        int len = a[i].size();
        for(int j = len - 1; j >= 0; j --) 
        {
    
    
            if(a[i][j] == b[idx])
            {
    
    
                buy2[idx] = i;
                idx --;
                i ++;
                break;
            }
        }
    }
    if(buy2[0] != -1) ans ++;
    if(ans == 0) cout << "impossible" <<endl;
    else if(ans == 1) cout << "unique" << endl;
    else
    {
    
    
        int flag = 1;
        for(int i = 0; i < m; i ++)
        {
    
    
            if(buy1[i] != buy2[i])
            {
    
    
                flag = 0;
                break;
            }
        }
        if(flag) cout << "unique" << endl;
        else cout << "ambiguous" << endl;
    }
}

D - Disastrous Doubling

Topic link

Title:

A cell grows by a factor of 2 starting from 1, and it becomes 2 on the first day, 4 on the second day, and 8 on the third day. Every day the researcher will take away a few, and if the cells are not enough in the middle, an error will be output, otherwise, the last remaining cells will be output.

answer:

This question has a large amount of data, and it is necessary to take the modulus of 1e9+7. By looking for rules, it can be found that once the number of cells exceeds twice the maximum value of cells taken by the researcher. Then the following cells must be enough, so you can use num = (num * 2% mod-a[i]% mod + mod)% mod to calculate, but there must be an error to determine that the number of cells is negative Happening.

Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
ll a[N];
int flag = 0;
ll num = 1;

int main() {
    
    
    int n;
    cin >> n;
    ll maxx = 0;
    for (int i = 0; i < n; i++) {
    
    
        cin >> a[i];
        maxx = max(maxx, a[i]);
    }
    maxx *= 2;
    for (int i = 0; i < n; i++) {
    
    
        if (flag) {
    
    
            num = (num * 2 % mod - a[i] % mod + mod) % mod;
        } else {
    
    
            num *= 2;
            if (num >= maxx) flag = 1;
            num -= a[i];
            if (num < 0) {
    
    
                cout << "error" << endl;
                return 0;
            }
        }
    }
    cout << num % mod << endl;
    return 0;
}

E - Envious Exponents

Topic link The main
idea of ​​the topic is to find the smallest number x greater than n, and 1 number in the binary representation method is equal to k.
There are three cases:
(1) If ans> k, traverse the binary representation of x from the low bit, if it is 1, it becomes 0 until ans = k. Turn to the second case.
(2) If ans = k, change the first non-leading 0 to 1, and then traverse from this position to the lower position. If it is 1, it becomes 0.
(3) If ans <k, traverse from low to high, if it is 0, it becomes 1 until ans = k.

#include <bits/stdc++.h>

using namespace std;
int q[100];
int main() {
    
    
    long long N, M;
    int k;
    int top = 0, ans = 0;
    cin >> N >> k;
    M = N;
    while (M != 0) {
    
    
        q[++top] = M % 2;
        if (M % 2 == 1) ans++;
        M /= 2;
    }
    if (ans > k)
    {
    
    
        for (int i = 1; i <= top; i++) {
    
    
            if (q[i] == 1) {
    
    
                q[i] = 0;
                ans--;
            }
            if (ans == k) break;
        }
    }
    int cnt = 0;
    int flag = 0;
    if (ans == k)
    {
    
    
        for (int i = 1; i <= 100; i++) {
    
    
            if (q[i] == 1) {
    
    
                flag = 1;
            }
            if (q[i] == 0 && flag == 1) {
    
    
                cnt = i;
                q[i] = 1;
                ans++;
                break;
            }
        }
        for (int i = cnt - 1; i >= 1; i--) {
    
    
            if (q[i] == 1) {
    
    
                q[i] = 0;
                ans--;
            }
        }
        for (int i = 1; i <= cnt; i++) {
    
    
            if (ans == k)
                break;
            else if (ans < k) {
    
    
                q[i] = 1;
                ans++;
            }
        }
    }
    if (ans < k)
    {
    
    
        for (int i = 1;; i++) {
    
    
            if (q[i] == 0) {
    
    
                q[i] = 1;
                ans++;
            }
            if (ans == k) break;
        }
    }
    long long sum = 0;
    long long x = 1;
    for (int i = 1; i <= 100; i++) {
    
    
        if (q[i] == 1) {
    
    
            sum = sum + x;
        }
        x = x * 2;
    }
    cout << sum << endl;
    return 0;
}

H - Horror Film Night

Topic link

Title:

The meaning of the title: Two people watch a movie together and watch one movie a day. Enter the first line n, a1, a2...an; it represents the total number of movies that the first person likes to watch, n, followed by the number of the movie that they like to watch. Enter the second line m, b1, b2...bn; it represents the total number m of the movies that the second person likes to watch, followed by the number of the movies that the second person likes to watch. Two people can only watch movies that both people like or one of them likes movies that the other doesn't like, and can't watch movies that one person likes continuously, find the maximum number of movies that can be watched.

answer:

book[i] represents the circumstance that they like to watch movies on the ith day, book[i]=0 means neither of them like it, book[i]=1 means the first person likes it, book[i]=2 means the second person Like, book[i]=3 means that both people like it. Use the tag variable to mark what happened last time. Then judge that this time that situation is feasible. If possible, the tag variable will be updated and the variable ans++ will be counted.

#include <bits/stdc++.h>
using namespace std;
int a[1000010];
int b[1000010];
int book[1000010];
int main() {
    
    
    int n, m;
    cin >> n;
    for (int i = 0; i < n; i++) {
    
    
        cin >> a[i];
        book[a[i]] += 1;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
    
    
        cin >> b[i];
        book[b[i]] += 2;
    }
    sort(a, a + n);
    sort(b, b + m);
    int ans = 0;
    int cur = 0;
    int maxx = max(a[n - 1], b[m - 1]);
    for (int i = 0; i <= maxx; i++) {
    
    
        if (book[i] == 0) {
    
    
            continue;
        } else if (book[i] == 1) {
    
    
            if (cur == 0 || cur == 2 || cur == 3) {
    
    
                ans++;
                cur = 1;
            } else if (cur == 1) {
    
    
                continue;
            }
        } else if (book[i] == 2) {
    
    
            if (cur == 0 || cur == 1 || cur == 3) {
    
    
                ans++;
                cur = 2;
            } else if (cur == 2) {
    
    
                continue;
            }
        } else if (book[i] == 3) {
    
    
            ans++;
            cur = 3;
        }
    }
    cout << ans << endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_47783181/article/details/112754936