首页 > 其他分享 >2024 Xiangtan University Summer Camp-Div.2

2024 Xiangtan University Summer Camp-Div.2

时间:2024-09-04 19:36:34浏览次数:9  
标签:Summer Xiangtan i32 University cin long i64 int using

A. 二度树上的染色游戏

因为题目保证了是二叉树,所以每次至多只需要选择一个子节点染成红色。所以可以贪心的选择红色权值小的子树即可。

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;
using ldb = long double;

const i32 inf = INT_MAX / 2;
const i64 INF = LLONG_MAX / 2;

#define int i64

using vi = vector<int>;


i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n;
    cin >> n;
    vi a(n + 1);
    int sum = 0;
    for (int i = 1; i <= n; i++)
        cin >> a[i], sum += a[i];
    vector<vi> e(n + 1);
    for (int i = 1, u, v; i < n; i++) {
        cin >> u >> v;
        e[u].push_back(v), e[v].push_back(u);
    }
    auto dfs = [&](auto &&self, int x, int fa) -> int {
        int cnt = 0;
        for (int y: e[x])
            cnt += y != fa;
        if (cnt < 2) return a[x];
        int ans = inf;
        for (int y: e[x]) {
            if (y == fa) continue;
            ans = min(ans, self(self, y, x));
        }
        return ans + a[x];
    };
    int red = dfs(dfs, 1, -1);
    cout << sum - red - red;
    return 0;
}

B. 小文的排列

我们可以把序列分层若干个子段,这样的话,除了开头的子段和结尾的子段,剩下的字段必须都是完整的\(m\)的排列,并且开头和结尾必须是\(m\)排列的子段。

因此我们可以用双指针扫描出所有的合法子段,然后做一个简单的dp 就好了。

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;
using ldb = long double;

const i32 inf = INT_MAX / 2;
const i64 INF = LLONG_MAX / 2;

#define int i64

using vi = vector<int>;
using pii = pair<int, int>;


i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vi a(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    if (ranges::max(a) > m) {
        cout << "NO\n";
        return 0;
    }
    vector<vi> e(n + 1);
    set<int> vis;
    for (int l = 1, r = 0; l <= n; l++) {
        while (r + 1 <= n and vis.count(a[r + 1]) == 0) {
            r++, vis.insert(a[r]);
            if (l == 1) e[r].push_back(1);
        }
        if (r == n or vis.size() == m)
            e[r].push_back(l);
        vis.erase(a[l]);
    }
    vi f(n + 1);
    f[0] = 1;
    for (int r = 1; r <= n; r++)
        for (int l: e[r])
            if (f[l - 1]) {
                f[r] = 1;
                break;
            }

    if (f[n]) cout << "YES";
    else cout << "NO";
    return 0;
}

C. gcd hard version

区间最大公约数可以用ST表求,区间和可以用前缀和求。

对于当前的区间\([l,r]\),如果\(r\)变大,最大公约数是不增的,区间和是不减的,因此如果\([l,r]\)满足,\([l,r+1]\)一定满足。

所以我们可以枚举左端点,然后直接二分出右端点。

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;
using ldb = long double;

const i32 inf = INT_MAX / 2;
const i64 INF = LLONG_MAX / 2;

#define int i64

using vi = vector<int>;


i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n;
    cin >> n;
    int lg2N = log2(n);
    vi lg2(n + 1);
    vector f(n + 1, vi(lg2N + 1));
    lg2[0] = -1;
    for (int i = 1; i <= n; i++) {
        cin >> f[i][0];
        lg2[i] = lg2[i / 2] + 1;
    }
    for (int j = 1; j <= lg2N; j++)
        for (int i = 1; i + (1 << j) - 1 <= n; i++)
            f[i][j] = gcd(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
    auto query = [=](int l, int r) {
        assert(l <= r);
        int s = lg2[r - l + 1];
        return gcd(f[l][s], f[r - (1 << s) + 1][s]);
    };

    vi pre(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> pre[i];
        pre[i] += pre[i - 1];
    }
    int res = 0;
    for (int i = 1; i <= n; i++) {
        int l = i, r = n, ans = -1;
        while (l <= r) {
            int mid = (l + r) / 2;
            if (query(i, mid) <= pre[mid] - pre[i - 1]) ans = mid, r = mid - 1;
            else l = mid + 1;
        }
        if (ans == -1) continue;
        res += n - ans + 1;
    }
    cout << res;
    return 0;
}

D. 战至终章

挑战的顺序一定是一种拓扑序。有多个可以挑战的情况下挑战\(a\)最小的一定最优。因此我们在求拓扑序的时候使用优先队列即可。

#include <bits/stdc++.h>

using namespace std;

using i32 = int32_t;
using i64 = long long;
using ldb = long double;

const i32 inf = INT_MAX / 2;
const i64 INF = LLONG_MAX / 2;

#define int i64

using vi = vector<int>;
using pii = pair<int, int>;


i32 main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n, p;
    cin >> n >> p;
    vi a(n + 1), b(n + 1), deg(n + 1);
    vector<vi> e(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> b[i];
    priority_queue<pii> heap;
    for (int i = 1; i <= n; i++) {
        cin >> deg[i];
        if (deg[i] == 0) heap.emplace(-a[i], i);
        for (int j = 0, x; j < deg[i]; j++)
            cin >> x, e[x].push_back(i);
    }
    vi res;
    while (not heap.empty()) {
        auto [_, x] = heap.top();
        heap.pop();
        if (a[x] > p) continue;
        res.push_back(x), p += b[x];
        for (auto y: e[x])
            if (--deg[y] == 0) {
                heap.emplace(-a[y], y);
            }
    }
    ranges::sort(res);
    cout << res.size() << "\n";
    for (auto i: res) cout << i << " ";

    return 0;
}

标签:Summer,Xiangtan,i32,University,cin,long,i64,int,using
From: https://www.cnblogs.com/PHarr/p/18397230

相关文章

  • THE UNIVERSITY OF MANCHESTER-NUMERICAL ANALYSIS 2Final Exam2021
    2.(a)BrieydescribehoworthogonalpolynomialscanbeusedtofindthenodesofGaussianquadra-turerulesforaweightedintegral∫a......
  • SDKD 2024 Summer Training Contest F2(The 13th Shandong ICPC Provincial Collegiat
    A-Orders题意每天能生产k个产品的工厂有n个订单,第i个订单是在a_i天交b_i个产品,问能否交付。思路订单按日期排序,记录剩下的商品.代码#define_CRT_SECURE_NO_WARNINGS#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongconstintmxn=1e6+5......
  • The American University in Cairo CSEA End of Winter Break Contest 2023
    链接:https://codeforces.com/gym/104168\(\\\)ADivisorDifference签到,输出\(n-1\)即可,复杂度\(O(1)\)。点击查看代码#pragmaGCCoptimize("unroll-loops,Ofast")#include<bits/stdc++.h>usingnamespacestd;usingi64=longlong;#defineendl&......
  • EPIC Institute of Technology Round Summer 2024 (Div. 1 + Div. 2) VP记录
    EPICInstituteofTechnologyRoundSummer2024(Div.1+Div.2)VP记录A一眼\((n-1)m+1\)。B最后的数列是固定的,每个数与最后数列的数相减后,对差值求和再加上最大值即可。C唐诗C题,获得\(3\)发罚时。只有一个数右边的数归零了,它才会归零。右往左扫,如果右边......
  • SDKD 2024 Summer Training Contest E2补题
    SDKD2024SummerTrainingContestE2A-PaperWatering题意对x进行至多k次操作(平方或开方后向下取整),求可以得到多少不同的数。思路平方完一定不同,且平方完后一定能开方出整数,所以只用额外考虑开方后平方的情况。若开方再平方与原来不同,则答案加上当前变化数的次数,直到变......
  • 2024 Summer_Camp 做题总结 下
    CloseVertices思路很明显,这是一道点分治题目,但有两个限制条件,考虑将两个条件排序起来,双指针找第一个条件,树状数组维护第二个条件,但是同一个子树内不能重复统计,所以将答案减去每个子树内的答案。代码#include<iostream>#include<algorithm>#defineintlonglongusingnam......
  • 2024 NVIDIA Summer Camp Day1:构建RAG多模态AI Agent
    下载材料和课件等课程相关资料下载链接:https://pan.baidu.com/s/15Y-gmsfeYCgKF-M3TJZVgg?pwd=fafe提取码:fafe 1.课件链接:https://pan.baidu.com/s/15JTy9CqnesXSlPiwwrUmjA?pwd=1111 提取码:1111 2.phi3量化大模型链接:https://pan.baidu.com/s/10HqxpkJmSyg-Bb......
  • 跟《经济学人》学英文:2024年08月03日这期 Investors beware: summer madness is here
    Investorsbeware:summermadnessishereThisyear’shottestmonthsareshapinguptobeespeciallywildshapingup:看起来,逐渐变成Shapingup:这个短语的意思是逐渐形成或变得某种样子。在这个上下文中,指的是今年最热的几个月看起来将会特别狂野或极端。例子:T......
  • 托福暑假学习的计划与目标[Plan and Goal of TOEFL Learning in Summer]
    时间即日起至8.31日,共计25天任务二十套听力+阅读=听力lecture3*20=60听力conversation2*20=40阅读2*20=40计划分为五个部分进行阅读每日:长难句分析五句话特殊情况,当日完成了一篇托福阅读可以免除长难句分析,但是必须要分析题目听力每日:边词边......
  • summer2024_机器码
    shellcode5include<string.h>include<stdio.h>include<stdlib.h>include<inttypes.h>include<capstone/capstone.h>include<sys/mman.h>intupkeep(){setvbuf(stdin,NULL,_IONBF,0);setvbuf(stdout,NULL,_IONBF,......