Codeforces Round #694 (Div. 2) A~D、E

比赛链接:Here

1471A. Strange Partition

题意:

给一个数组,数组中的所有元素可以任意合并,求数组的每个元素除以x上取整的和,求结果的最大值和最小值。

思路:

瞎猜。最小值肯定是都合并在一起,最大值是分开。

【AC Code】

const int N = 1e5 + 10;
ll a[N];
int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int _; for (cin >> _; _--;) {
        ll n, x;
        cin >> n >> x;
        ll s1 = 0, s2 = 0;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
            s1 += a[i];
            s2 += (a[i] + x - 1) / x;
        }
        cout << (s1 + x - 1) / x << " " << s2 << "
";
    }
}

1471B. Strange List

题意:

给你一数组,一个x,从前往后遍历数组(也会遍历后面加进来的元素),如果当前元素a可以被x除尽,则在数组末尾加上x个a/x。如果不能被除尽则停止,问数组的元素之和。

思路:

可以很显然看出一个性质如果a/x也能被x除尽,那么结果增加a,因为一共有x个a/x。 模拟,如果能除尽,则结果加上当前元素,不能除尽则跳出。

【AC Code】

const int N = 1e5 + 10;
ll a[N], b[N];
int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int _; for (cin >> _; _--;) {
        ll n, x;
        cin >> n >> x;
        ll s = 0;
        for (int i = 0; i < n; ++i) {
            cin >> a[i], b[i] = a[i];
            s += a[i];
        }
        for (int i = 0; i < n; i = (i + 1) % n) {
            if (b[i] % x == 0) s += a[i], b[i] = b[i] / x;
            else break;
        }
        cout << s << "
";
    }
}

1471C. Strange Birthday Party

题意:

商品按价钱从小到大排序,每个商品只能拿1次。 n个客人,每个客人有一个权值,只能拿下标比权值小的商品或者给客人下标为权值的商品对应的现金。

思路:

排序贪心,权值大的先拿前面的,序号小的后拿,如果不存在了,就拿现金。

【AC Code】

const int N = 3e5 + 10;
ll k[N], c[N], st[N];
int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int _; for (cin >> _; _--;) {
        ll n, m;
        cin >> n >> m;
        for (int i = 1; i <= n; ++i) cin >> k[i];
        for (int i = 1; i <= m; ++i) st[i] = 0;
        for (int i = 1; i <= m; ++i) cin >> c[i];
        sort(k + 1, k + 1 + n);
        ll ans = 0;
        int j = 1;
        for (int i = n; i >= 1; i -= 1) {
            if (c[k[i]] > c[j] && !st[j] && j <= m) {
                ans += c[j];
                st[j] = 1;
                j++;
            } else ans += c[k[i]];
        }
        cout << ans << "
";
    }
}

1471D. Strange Definition

题意:

如果 (lcm(x,y)/gcd(x,y)) 为一个完全平方数,那么 (x)(y) 相邻。给你一个数组,从 (0) 秒开始,每秒钟每个元素都会被其和与其互为完全平方数的乘积替换,设(d_i) 为数组中第 $i $ 个元素共有几个互为完全平方数的数(包含本身)。 (q) 个询问,每个询问询问第 (i) 秒时 (d) 的最大值。

思路:

有一个明显的推理:(x*y) 如果是完全平方数,那么x和y相邻。 那么我们把所有元素的偶数质因子都筛掉,设为core数组,那么如果core[x]=core[y],那么x和y相邻。 当询问为0,我们只需要输出出现次数最多的core。 当询问不为0,比如为1,那么core出现为偶数次的下次都会为1,如果出现奇数次下次不变,特例当core为1时无论奇偶都不变。所以重新统计。 当询问大于1,我们可以推一下,结果与1相同。

【AC Code】

ll primes[1001000];
int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    primes[1] = 1;
    for (int i = 2; i <= 1000100; i++) {
        if (!primes[i]) {
            primes[i] = i;
            for (int j = i * 2; j <= 1000100; j += i)
                if (!primes[j]) primes[j] = i;
        }
    }
    int _; for (cin >> _; _--;) {
        unordered_map<ll, ll> hash;
        int n;
        cin >> n;
        hash[1] = 0;
        for (int i = 1; i <= n; i++) {
            int h = 1, x;
            cin >> x;
            while (primes[x] != 1) {
                int t = 0;
                int j = primes[x];
                while (x % j == 0) {
                    x /= j;
                    t++;
                }
                if (t % 2) h *= j;
            }
            hash[h]++;
        }

        ll cnt1 = 0, cnt2 = 0;
        for (auto i : hash) cnt1 = max(cnt1, i.second);

        for (auto &i : hash)
            if (i.first != 1 && i.second % 2 == 0) {
                hash[1] += i.second;
                i.second = 0;
            }

        for (auto i : hash) cnt2 = max(cnt2, i.second);

        int q;
        cin >> q;
        while (q--) {
            ll x;
            cin >> x;
            if (x == 0) cout << cnt1 << endl;
            else cout << cnt2 << endl;
        }
    }
}

1471F. Strange Housing

没写出来...

题意:

给你节点和边,染色,染色的住老师,未染色的住学生。 要求:相邻的节点不能同时染色,只有染色的节点和不染色的节点相邻的边才成立,要求图联通。

思路:

暴力贪心即可。如果与当前节点相邻的所有节点没老师,那当前节点就放老师,否则不放。 如果有节点未遍历到,则输出NO。 证明:老师一定不相邻,一定联通,放学生时周围一定有老师。

【AC Code】

#define int long long
const int maxn = 300010;
vector<int> g[maxn];
int cnt;
int st[maxn];

void dfs(int u) {
    int sum1 = 0, sum2 = 0;

    for (int i = 0; i < g[u].size(); i++)
        if (st[g[u][i]]) {
            if (st[g[u][i]] == 1) sum1++;
            else sum2++;
            // cout << st[g[u][i]] << endl;
            // cout << "---" << endl;
        }
    // cout << sum1 << endl;
    if (sum1 == 0) st[u] = 1;
    else st[u] = 2;

    for (int i = 0; i < g[u].size(); i++)
        if (!st[g[u][i]]) dfs(g[u][i]);
}

void work() {
    cnt = 0;
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) st[i] = 0, g[i].clear();
    while (m--) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    dfs(1);

    int cnt = 0, flag = 0;
    for (int i = 1; i <= n; i++) {
        if (st[i] == 1) cnt++;
        if (!st[i]) flag = 1;
    }

    if (flag) cout << "NO" << endl;
    else {
        cout << "YES" << endl;
        cout << cnt << endl;
        for (int i = 1; i <= n; i++)
            if (st[i] == 1) cout << i << " ";
        cout << endl;
    }
}

The desire of his soul is the prophecy of his fate
你灵魂的欲望,是你命运的先知。

原文地址:https://www.cnblogs.com/RioTian/p/15219432.html