首页 > 其他分享 >Codeforces Round 915 (Div. 2)

Codeforces Round 915 (Div. 2)

时间:2023-12-17 09:45:03浏览次数:27  
标签:cin int rep Codeforces st 最小值 915 Div mex

Codeforces Round 915 (Div. 2)

唉,菜狗。

A - Cover in Water

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> m;
        cout << max(n, m) << '\n';
    }
    return 0;
}

B - Begginer's Zelda

除了最后一次操作,每次操作会稳定减少两个叶子节点。

const int N = 1e5 + 5, M = N << 1;
 
int n, m, _, k, cas;
int h[N], to[M], ne[M], tot;
 
void init(int n) {
    tot = 0;
    memset(h, 0, sizeof(int) * (n + 1));
}
 
void add(int u, int v) {
    ne[++tot] = h[u];
    to[h[u] = tot] = v;
}
 
int dfs(int x, int f) {
    int ans = 0, c = 0;
    for (int i = h[x], y = to[i]; i; y = to[i = ne[i]]) if (y ^ f) {
        ans += dfs(y, x);
        ++c;
    }
    return x == 1 && c < 2 ? ans + 1 : max(ans, 1);
}
 
int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n;
        init(n);
        rep (i, 2, n) {
            int x, y; cin >> x >> y;
            add(x, y); add(y, x);
        }
        cout << (dfs(1, 0) + 1 >> 1) << '\n';
    }
    return 0;
}

C - Largest Subsequence

烦,一开始读成字串了,写了半天,发现样例都过不去。

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n >> s + 1;
        VI a;
        rep (i, 1, n) {
            while (!a.empty() && s[a.back()] < s[i]) a.pop_back();
            a.pb(i);
        }
        int len = 0;
        for (int i = 0; i < a.size() && s[a[i]] == s[a[0]]; ++i, ++len);
        for (int i = 0, j = a.size() - 1; i < j; ++i, --j) swap(s[a[i]], s[a[j]]);
        bool f = 1;
        rep (i, 2, n) if (s[i] < s[i - 1]) f = 0;
        cout << (f ? int(a.size()) - len : -1) << '\n';
    }
    return 0;
}

D - Cyclic MEX

这题考察的是 mex。

考虑对于一个排列,如何区间 (l, r) 求 mex 呢?

考虑到 mex 性质,显然是 \(min(min(a_1 ... a_{l - 1}), min(a_{r + 1} ... a_n))\) (下标从 1 开始)

这道题是循环排列,即把第一个数不断的放到最后或把一个数从最后一个不断放到最前面,即只需考虑前缀最小值或后缀最小值。

考虑一般都是 1 到 n,所以这里选择维护前缀最小值,维护前缀最小值,经典的就是单调栈。

把一个数从队首移到队末,对于维护前缀最小值来说,就是把不断弹栈并把贡献不断减去,再加上移动后的贡献即可。

单调栈首先塞个 -1 以防栈空,对于序列最后一个数,mex 一定是 n, 由于最后一个数之后不会在加入,会导致最后一个数位置的 mex 没有计算,所以记得加 n.

这题要拆环,取数是要取模,为了方便,下表从 0 开始

int main() {
    IOS;
    for (cin >> _; _; --_) {
        cin >> n;
        rep (i, 0, n - 1) cin >> a[i];
        stack<int> st; st.emplace(-1);
        ll res = n, ans = 0;
        rep (i, 0, n * 2 - 1) {
            while (st.size() > 1 && a[i % n] < a[st.top() % n]) {
                int x = st.top(); st.pop();
                res -= 1ll * a[x % n] * (x - st.top());
            }
            res += 1ll * a[i % n] * (i - st.top());
            st.emplace(i);
            if (i >= n) umax(ans, res);
        }
        cout << ans << '\n';
    }
    return 0;
}

标签:cin,int,rep,Codeforces,st,最小值,915,Div,mex
From: https://www.cnblogs.com/2aptx4869/p/17908782.html

相关文章

  • Educational Codeforces Round 159 (Rated for Div. 2) C. Insert and Equalize (贪心+
    EducationalCodeforcesRound159(RatedforDiv.2)C.InsertandEqualize思路:首先对\(a\)进行排序,然后对所有差值取gcd,获得可用的最大因子\(gc\),答案有两种情况:一种是\(a_{n+1}\)在$a_1\(~\)a_n$范围内,这时要获得最大的位置一种情况是$a_1\(~\)a_n$......
  • [Codeforces] CF1774B Coloring
    CF1774BColoring题意Cirno_9baka的纸条上有\(n\)个格子,他觉得空白的纸条看着有点无趣,于是想在纸条的格子上涂上\(m\)种颜色。同时,他认为第\(i\)种颜色必须要用\(a_i\)次,且每连续\(k\)个格子里涂的颜色必须互不相同。Cirno_9baka想知道有没有这样的一种涂色方案能......
  • [Codeforces] CF1760F Quests
    CF1760FQuests题意有\(n\)个任务,你每一天都可以选择其中的一个任务完成或不选。当你完成了第\(i\)个任务,你将获得\(a_i\)元。但是如果你今天完成了一个任务,那么你之后\(k\)天内都不能再完成这个任务。给出两个数\(c\),\(d\),要求求出满足在\(d\)天内可以收集至少\(c......
  • [Codeforces] CF1744E1 Divisible Numbers (easy version)
    CF1744E1DivisibleNumbers(easyversion)题意给你四个数\(a,b,c,d\),你需要找出一组\(x,y\)使得\(a<x\leqc,b<y\leqd\)并且\(x\cdoty\)能被\(a\cdotb\)整除,如果没有输出-1-1。思路最暴力的思路肯定是枚举,更肯定的一点是会TLE但是注意到,如果\(x\)一定,那么他......
  • [Codeforces] CF1740D Knowledge Cards
    CF1740DKnowledgeCards题意有一个\(n\timesm\)的棋盘。现在\((1,1)\)中有一个栈,你可以按照一定的顺序进行出栈操作,每次都可以移动一个卡片到一个相邻的空白位置,但是卡片不能重合。问,能否通过若干次操作,将\((1,1)\)中全部的卡片移动到\((n,m)\)的栈中并使得这个栈按照从栈......
  • [Codeforces] CF1722G Even-Odd XOR
    CF1722GEven-OddXOR题意给定一个正整数\(n\),请你找出一个长度为\(n\)数组\(a\),满足数组是由互不相同的非负且小于\(2^{31}\)的整数组成,并且该数组中奇数项上元素的异或值与偶数项上元素的异或值要相等。思路根据异或的交换律,可以发现:奇偶位异或值相等,那么全局异或值位......
  • 如何用JS判断div中内容为空,当为空时隐藏div
    <div class="right_con_div" id="nodiv"><h2>标题1</h2><ul class="id_inner"></ul></div><div class="right_con_div" id="nodiv"><h2>标题2</h2><ul class=......
  • CodeForces 838D Airplane Arrangements
    洛谷传送门CF传送门考虑加入第\(n+1\)个位置,这样座位构成了一个环。每个位置被覆盖的概率相等,为\(\frac{m}{n+1}\),然后算出概率再乘方案数就行了。code//Problem:D.AirplaneArrangements//Contest:Codeforces-IndiaHacks2ndElimination2017(unofficial,......
  • Codeforces Round 787 (Div. 3)D. Vertical Paths
    题目链接题意:给定一棵树,将这棵树划分成几天互不相交的链,要求最小化链的数量思路:每个叶子节点一定在一条链中,所以链的数量就是叶子节点的数量,从叶子节点往上跳直到根节点,边跳边标记,路径上所有点都属于这条链。坑:数据大时,不要轻易使用memset不然会t到起飞vector不要开太多就比......
  • Codeforces Round 814 (Div. 2)
    基本情况又是过了ABC。A、B思路更多的是从数据上分析出来的,过的很顺。C经典拿评测机来调试,甚至还RE了一次,最后终于过了。C.FightingTournamentProblem-C-Codeforces第一次改错这题我的思路是找到规律后,优先队列加二分查找。但是一直WA第二个点,这是我一开始的代码:......