Educational Codeforces Round 147 (Rated for Div. 2)
前言:
本场是贪心场,前三题签的巨快,第四题卡到最后,第四题一直以为是动态规划,想到快结束的时候,突然发现是贪心qwq,最后和室友聊了一下贪心思路,只可惜没有时间写代码了,早知道签完3题直接上床睡觉。
D. Black Cells
题意:
有 个从左到右的有序格子,给你 个可涂色的区间,初始位置在格子 ,有三种操作:
1、从格子 移动到格子 ;
2、开始涂色,即此操作后移动到的格子将涂上色;
3、结束涂色,即此操作后移动到的格子将不涂色;
要求在满足不能在区间外涂色的情况下,问涂完 个格子最小的操作数。
题解:
可以发现有时候选区间长度为 的区间时,效益太低,需要3步操作,但是当选择区间长度大于等于 的区间时,就一定要选。
先找到如果无脑选所有区间到达的点,然后判断,如果不去选择前面的 区间,我们会多出两次操作,然后每次后移取最小值。
具体代码如下:
#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
题意:有一个括号序列,每次你只能删相邻的两个左括号和右括号,且每次删除的花费是当前右括号右边的右括号数量,给你你有最多 次修改操作,可以让一个左或右括号改变位置,问最后把括号删完最小的花费。
题解:
对于括号序列((())()),我们观察发现对于一对相邻的括号(),它的花费就是它外面有几层括号包着它。对于加粗括号((())()) 的花费是1, 因为它外面只有一层括号。对于((())()) 它的花费是2 ,因为它外层有两层括号。对于((())())的花费是1,因为它外面只有一层括号。
我们考虑建树, 对于((())())给每个左括号一个标号 1 2 3 ))4 )),我们建树,儿子与父亲表示包含关系。可以用stack实现这个过程。
通过建树,不难发现贡献就是深度。
接下来我们考虑操作。每次可以移动一个括号的位置。也就是说我们每次可以解除一层括号。那我们肯定是解除拥有节点数最多的树。对答案的贡献就是 ;我们可以用优先队列来每次找出 最大的树。
具体代码如下:
#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;
}