Educational Codeforces Round 147 (Rated for Div. 2)部分题解

Educational Codeforces Round 147 (Rated for Div. 2)

前言:

本场是贪心场,前三题签的巨快,第四题卡到最后,第四题一直以为是动态规划,想到快结束的时候,突然发现是贪心qwq,最后和室友聊了一下贪心思路,只可惜没有时间写代码了,早知道签完3题直接上床睡觉

D. Black Cells

题意:

有 10^{18} 个从左到右的有序格子,给你 n 个可涂色的区间,初始位置在格子 0 ,有三种操作:

1、从格子 x 移动到格子 x+1 ;

2、开始涂色,即此操作后移动到的格子将涂上色;

3、结束涂色,即此操作后移动到的格子将不涂色;

要求在满足不能在区间外涂色的情况下,问涂完 k 个格子最小的操作数。

题解:

可以发现有时候选区间长度为 1 的区间时,效益太低,需要3步操作,但是当选择区间长度大于等于 2 的区间时,就一定要选。

先找到如果无脑选所有区间到达的点,然后判断,如果不去选择前面的 1 区间,我们会多出两次操作,然后每次后移取最小值。

 具体代码如下:

#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <unordered_map>
#define int long long
#define pi acos(-1)
#define quick_cin() cin.tie(0),ios::sync_with_stdio(false)
#define endl "\n"
#define pb push_back
#define all(x) x.begin(), x.end()
#define ul (u << 1)
#define ur (u << 1 | 1)

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<char, char> PCC;
const int N = 200010, M = 20, P = 131;
const int mod = 1e9 + 7, Mod = 998244353;
const int INF = 1e18, inf = 0x3f3f3f3f;
const int base = 1e9 + 7;
const double esp = 1e-6;

int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int n, m, k;
int T = 1;
int res;

PII q[N];
int s[N];//记录前i个区间的可涂色格子数前缀和
int c[N];//记录前i个区间区间长度为1的个数的前缀和
int w[N];//记录第i个区间的长度
int ans;

void solve()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i ++ ) cin >> q[i].first;
    for (int i = 1; i <= n; i ++ ) cin >> q[i].second;

    for (int i = 1; i <= n; i ++ )
    {
        w[i] = q[i].second - q[i].first + 1;
        s[i] = s[i - 1] + w[i];
        c[i] = c[i - 1] + (w[i] == 1);
    }

    if (s[n] < m) cout << "-1\n";
    else
    {
        res = INF;
        int pos = lower_bound(s + 1, s + 1 + n, m) - s;
        int cnt1 = c[pos];
        
        ans = q[pos].first + (pos - 1) * 2 + 1 + m - s[pos - 1];
        res = min(res, ans);
        
        int idx = q[pos].first + m - s[pos - 1] - 1;
        
        for (int i = 1; i <= n; i ++ )
        {
            if (w[i] == 1 && i < pos)
            {
                int value = 0;
                idx ++ ;
                if (idx <= q[pos].second) value = 1;
                else
                {
                    pos ++ ;
                    if (pos > n) break;
                    idx = q[pos].first;
                    value += 2;
                    value += q[pos].first - q[pos - 1].second;
                }
                
                ans -= 2;
                ans += value;
                res = min(res, ans);
            }
        }
        cout << res << endl;
    }
}

signed main()
{
    quick_cin();
    cin >> T;
//    getchar();

    while (T -- )
    {
        solve();
    }

    return 0;
}

E. Rearrange Brackets

题意:有一个括号序列,每次你只能删相邻的两个左括号和右括号,且每次删除的花费是当前右括号右边的右括号数量,给你你有最多 k 次修改操作,可以让一个左或右括号改变位置,问最后把括号删完最小的花费。

题解:

对于括号序列((())()),我们观察发现对于一对相邻的括号(),它的花费就是它外面有几层括号包着它。对于加粗括号((())()) 的花费是1, 因为它外面只有一层括号。对于((())()) 它的花费是2 ,因为它外层有两层括号。对于((())())的花费是1,因为它外面只有一层括号。

我们考虑建树, 对于((())())给每个左括号一个标号 1 2 3 ))4 )),我们建树,儿子与父亲表示包含关系。可以用stack实现这个过程。

通过建树,不难发现贡献就是深度。

接下来我们考虑操作。每次可以移动一个括号的位置。也就是说我们每次可以解除一层括号。那我们肯定是解除拥有节点数最多的树。对答案的贡献就是 ans -= sz[v] - 1 ;我们可以用优先队列来每次找出 sz[v] 最大的树。

具体代码如下:

#pragma GCC optimize(2)
#pragma GCC diagnostic error "-std=c++11"
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <unordered_map>
#define int long long
#define pi acos(-1)
#define quick_cin() cin.tie(0),ios::sync_with_stdio(false)
#define endl "\n"
#define pb push_back
#define all(x) x.begin(), x.end()
#define ul (u << 1)
#define ur (u << 1 | 1)

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
const int N = 200010, M = 60, P = 131;
const int mod = 1e9 + 7, Mod = 998244353;
const int INF = 1e18, inf = 0x3f3f3f3f;
const int base = 1e9 + 7;
const double esp = 1e-6;

int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int n, m, k;
int T = 1;
int res;
char op[N];
int cnt;
int h[N], e[N * 2], ne[N * 2], idx;
int sz[N];
int st[N];

void reset(int len)
{
    for (int i = 1; i <= len; i ++ )
    {
        h[i] = -1;
        st[i] = false;
        sz[i] = 0;
    }
    res = cnt = idx = 0;
}

void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

void dfs(int u, int fa, int depth)
{
    sz[u] = 1;
    res += depth;
    for (int i = h[u]; ~i; i = ne[i])
    {
        int j = e[i];
        if (j == fa) continue;
        dfs(j, u, depth + 1);
        sz[u] += sz[j];
    }
}

void solve()
{
    cin >> k;
    cin >> op + 1;
    n = strlen(op + 1);
    reset((n + 1) / 2);
    priority_queue<PII, vector<PII>, less<PII> > heap;
    stack<int> sta;
    for (int i = 1; i <= n; i ++ )
    {
        if (op[i] == '(')
        {
            int b = ++ cnt;
            if (sta.empty())
            {
                sta.push(b);
            }
            else
            {
                int a = sta.top();
                add(a, b), add(b, a);
                sta.push(b);
            }
        }
        else
        {
            int root = sta.top();
            sta.pop();
            if (sta.empty())
            {
                dfs(root, -1, 0);
                heap.push({sz[root], root});
            }
        }
    }

    while (k -- )
    {
        PII t = heap.top();
        if (t.first == 1) break;
        heap.pop();
        res -= t.first - 1;
        st[t.second] = true;
        for (int i = h[t.second]; ~i; i = ne[i])
        {
            int j = e[i];
            if (st[j]) continue;
            heap.push({sz[j], j});
        }
    }

    cout << res << endl;

}

signed main()
{
    quick_cin();
    cin >> T;
//    getchar();

    while (T -- )
    {
        solve();
    }

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值