寒假训练——贪心构造做题记录
前言
这几天在补 whk,做一点轻松的题目
尝试不看题解自己先写
完成进度(10/10)
贪心(Finished)
P5521 [yLOI2019] 梅深不见冬
等价于问一个最优的访问子树的顺序,使得花费最少
先考虑当前节点只有两个孩子的情况
容易发现放满它的孩子的花费变低,在这个节点上放梅花的花费不会增加
令
我们考虑放满所有孩子的花费:
先放
先放
可以发现
进一步发现这个结论在所有情况下的普适性
于是考虑对孩子排序,然后模拟计算
然后再将左右的土地删除,这里可以使用双向链表
妙处在于,这样做不仅保证了不会有相邻土地被种树的正确性,还保证你能有效地“后悔”——把中间格子种树改为两边格子种树
细想一下,若有格子被多次合并,这个做法也依然正确
剩余部分就与正常的后悔贪心一样,用大根堆维护
附代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, m, ans = 0, w[N], vis[N], pre[N], nxt[N];
struct node{
int val, id;
bool operator < (const node& b) const{
return val < b.val;
}
};
priority_queue<node> pq;
void Update(int x){
vis[x] = 1;
nxt[pre[x]] = nxt[x];
pre[nxt[x]] = pre[x];
nxt[x] = pre[x] = 0;
}
int main(){
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> w[i];
if ((n >> 1) < m)
return puts("Error!"), 0;
for (int i = 1; i < n; i++)
nxt[i] = i + 1;
nxt[n] = 1;
for (int i = 2; i <= n; i++)
pre[i] = i - 1;
pre[1] = n;
for (int i = 1; i <= n; i++)
pq.push({w[i], i});
for (int i = 1; i <= m; i++){
while (vis[pq.top().id])
pq.pop();
int val = pq.top().val;
int id = pq.top().id;
pq.pop();
ans += val;
int l = pre[id], r = nxt[id];
w[id] = w[l] + w[r] - val;
pq.push({w[id], id});
Update(l);
Update(r);
}
cout << ans << endl;
return 0;
}
AT_agc018_c Coins
常见的套路是将三元组转化为二元组
定义这样的二元组
问题转化为选 然后想不下去了
考虑邻项交换法
发现的性质是,当
于是排序
对排序后的数组预处理每个前缀前
枚举分割点
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n, x, y, z, ans, l[N], r[N];
struct node{
int e, f;
}p[N];
priority_queue<int, vector<int>, greater<int> > pq;
bool cmp(node x, node y){
return x.e - x.f > y.e - y.f;
}
signed main(){
cin >> x >> y >> z;
n = x + y + z;
for (int i = 1, a, b, c; i <= n; i++){
cin >> a >> b >> c;
ans += a;
p[i] = {b-a, c-a};
}
sort(p + 1, p + n + 1, cmp);
for (int i = 1; i <= n; i++){
l[i] = l[i-1] + p[i].e;
pq.push(p[i].e);
if (pq.size() > y){
l[i] -= pq.top();
pq.pop();
}
}
while (!pq.empty())
pq.pop();
for (int i = n; i >= 1; i--){
r[i] = r[i+1] + p[i].f;
pq.push(p[i].f);
if (pq.size() > z){
r[i] -= pq.top();
pq.pop();
}
}
int maxn = -9e18;
for (int i = y; i <= n-z; i++)
maxn = max(maxn, l[i] + r[i+1]);
ans += maxn;
cout << ans << endl;
return 0;
}
CF521D Shop
发现不需要输出精确计算值,考虑一些误差比较大的乱搞算法
我们发现可以把=强转为+,将+强转为*
对于同一个位置的赋值,只保留最大的一个并强转为加法
对于同一个位置的加法,将加的值从大到小排序,再以依次强转为乘
由于在选择加法操作时,必然先选择加的值大的操作,所以这样的贪心是正确的
最后把所有转化后的乘法操作从大到小排序,选出
对于一个子序列,最优的操作方式必然是先=,再+,最后*
所以再对这
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int k, n, m, a[N], op[N], p[N], b[N];
pair<int, int> fz[N];
vector<pair<int, int>> add[N];
vector<pair<long double, int>> mul;
bool cmp(pair<long double, int> a, pair<long double, int> b){
return op[a.se] < op[b.se];
}
int main(){
cin >> k >> n >> m;
for (int i = 1; i <= k; i++)
cin >> a[i];
for (int i = 1; i <= n; i++){
cin >> op[i] >> p[i] >> b[i];
switch(op[i]){
case 1:
fz[p[i]] = max(fz[p[i]], mp(b[i], i));
break;
case 2:
add[p[i]].push_back(mp(b[i], i));
break;
case 3:
mul.push_back(mp(b[i], i));
}
}
for (int i = 1; i <= k; i++)
if (fz[i].fi > a[i])
add[i].push_back(mp(fz[i].fi - a[i], fz[i].se));
for (int i = 1; i <= k; i++){
sort(add[i].begin(), add[i].end(), greater<pair<int, int> >());
ll now = a[i];
for (auto x : add[i]){
mul.push_back(mp(1.0L * (now+x.fi) / now, x.se));
now += x.fi;
}
}
sort(mul.begin(), mul.end(), greater<pair<long double, int> >());
int t = min(m, (int)mul.size());
sort(mul.begin(), mul.begin() + t, cmp);
cout << t << endl;
for (int i = 0; i < t; i++)
cout << mul[i].se << " " ;
cout << endl;
return 0;
}
P8170 [eJOI2021] Waterfront
查了一些资料,深入学习了一下这类题型
比较好想的写法是二分答案并贪心,但细想发现有太多细节要考虑,复杂度也不优
我们进一步思考,发现可以假设所有树都涨完了,然后开始砍
砍树的贪心思路很好想,就是先砍高的树
为了不出现将高度小于
我们定义函数
这很好求,细致思考即可
此外,为了不让一天砍超过
维护序列连通性,容易想到使用并查集
附代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
int n, m, k, x, cnt[N], H[N], a[N];
int Calc(int h, int d, int c){
if (d == 0)
return h - c*x < x ? m+1 : 1;
return max(1, ((c+1)*x-h - 1) / d + 1);
}
struct node{
int id, val, day, cut;
node() {}
node(int id_, int val_, int day_, int cut_){
id = id_;
val = val_;
day = day_;
cut = cut_;
}
bool operator < (const node& b) const{
return val < b.val;
}
};
priority_queue<node> pq;
int fa[N];
void Get_Pre(){
for (int i = 1; i <= m+1; i++){
fa[i] = i;
if (i != m+1)
cnt[i] = k;
}
}
int find(int x){
if (fa[x] == x)
return x;
return fa[x] = find(fa[x]);
}
int main(){
cin >> n >> m >> k >> x;
for (int i = 1; i <= n; i++)
cin >> H[i] >> a[i];
Get_Pre();
for (int i = 1; i <= n; i++)
pq.push(node(i, H[i] + a[i]*m, min(m+1, Calc(H[i], a[i], 0)), 0));
while (!pq.empty()){
int id = pq.top().id;
int val = pq.top().val;
int day = pq.top().day;
int cut = pq.top().cut;
pq.pop();
day = find(day);
if (day == m+1){
cout << val << endl;
return 0;
}
cnt[day]--;
if (!cnt[day])
fa[day] = find(day+1);
val -= x;
cut++;
day = min(m+1, Calc(H[id], a[id], cut));
pq.push(node(id, val, day, cut));
}
return 0;
}
P8793 [蓝桥杯 2022 国 A] owo
串内的 owo 数量很好统计,我们考虑如何最大化拼接后产生的 owo 数量
由于题目限制,字符串中只会出现 o 和 w,考虑分类讨论拼接后可以贡献新的 owo 的前后缀
发现当前缀是 o、wo,后缀是o、ow 时都可以拼接
而如果有前缀或后缀是 ww 时,相当于这个字符串前端或后端被封闭了,无法贡献新的 owo
最后考虑一种特殊情况——这个字符串是 w,这种情况下若要贡献新的 owo,必须前后两个串都符合要求,显然不优
我们贪心地先将两个前后缀都能做贡献的字符串拼接,这样不仅能做贡献,拼接后的大字符串依旧是前后缀都能做贡献的,显然是最优的
接着是将一个前后缀都能做贡献的字符串和一个一端被封闭的字符串拼接,在做出贡献之余拼接后的大字符串依旧有前或后缀能做贡献
接着是将两个一端封闭的字符串拼接
然后将 将两个前后缀都能做贡献的字符串拼接 这一步中剩余的 o-o 、wo-ow 串合并
最后是最劣的 w 的合并
考虑三位一段的对应,为了不浪费位置,可以将不被干扰的段储存两位二进制的信息,有一位被干扰的段存一位,其他不存
在这样的情况下,一个
考虑对应,先考虑有干扰的特殊情况:
我们令 100 、011 表示一位二进制 1 ,不管什么情况,这两种表示法总有一种能被表示
如何保存一位 0 较麻烦,我们考虑先构造其他方案,并且尽量在构造时解决这个问题
对于每个不受干扰的段:
001 -> 00、110 -> 01、101 -> 10、111 -> 11
再回过头来考虑保存一位 0,发现只剩余 010 一种表示法
若中间位置未损坏时,明显可行
若中间位置损坏时,观察我们对于每个不受干扰的段设计的构造,发现表示二位的末位为 0 的二进制的表示法,中间一位都为 0
所以,若中间位置损坏时,假设它不受干扰,直接构造即可
有了构造方式,解密就很简单了,算是一道很好玩的通信题
附代码:
#include "Broken_device_lib.h"
#define ll long long
void Anna(int N, ll X, int K, int P[]){
int cnt = 0, b[3];
ll now = X;
for (int i = 0; i < N/3; i++){
cnt = b[0] = b[1] = b[2] = 0;
for (int j = 0; j < K; j++){
if (P[j] == i*3)
b[0] = 1, cnt++;
else if (P[j] == i*3 + 1)
b[1] = 1, cnt++;
else if (P[j] == i*3 + 2)
b[2] = 1, cnt++;
}
if (cnt >= 2){
Set(i*3, 0);
Set(i*3 + 1, 0);
Set(i*3 + 2, 0);
continue;
}
if (cnt == 1){
int t = now % 2;
if (!t){
if (b[1] != 1){
Set(i*3 + 1, 1);
Set(i*3, 0);
Set(i*3 + 2, 0);
now >>= 1;
}else{
t = now % 4;
if (!t){
Set(i*3 + 2, 1);
Set(i*3, 0);
Set(i*3 + 1, 0);
}else if (t == 2){
Set(i*3, 1);
Set(i*3 + 2, 1);
Set(i*3 + 1, 0);
}
now >>= 2;
}
}else{
if (b[0] != 1){
Set(i*3, 1);
Set(i*3 + 1, 0);
Set(i*3 + 2, 0);
}else{
Set(i*3 + 1, 1);
Set(i*3 + 2, 1);
Set(i*3, 0);
}
now >>= 1;
}
}
if (cnt == 0){
int t = now % 4;
if (t == 0){
Set(i*3 + 2, 1);
Set(i*3, 0);
Set(i*3 + 1, 0);
}else if (t == 1){
Set(i*3, 1);
Set(i*3 + 1, 1);
Set(i*3 + 2, 0);
}else if (t == 2){
Set(i*3, 1);
Set(i*3 + 2, 1);
Set(i*3 + 1, 0);
}else{
Set(i*3, 1);
Set(i*3 + 1, 1);
Set(i*3 + 2, 1);
}
now >>= 2;
}
}
}
ll Bruno(int N, int A[]){
ll res = 0;
for (int i = N/3 - 1; i >= 0; i--){
int t = (A[i*3+2] << 2) + (A[i*3+1] << 1) + A[i*3];
if (t == 0)
;
else if (t == 1)
res <<= 1, res |= 1;
else if (t == 2)
res <<= 1;
else if (t == 3)
res <<= 2, res |= 1;
else if (t == 4)
res <<= 2;
else if (t == 5)
res <<= 2, res |= 2;
else if (t == 6)
res <<= 1, res |= 1;
else res <<= 2, res |= 3;
}
return res;
}
AT_agc012_c Tautonym Puzzle
神仙题
其实数据范围有提示,1 2 3 4 5 6 7 ... 98 99 100 的序列
考虑构造前半部分,发现好序列个数就等价于前半部分最长上升子序列个数
将
若将数添加前半部分末尾,则 LIS 个数增加一倍再加一
将构造转化为对
附代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 210;
int n, cnt = 0, fr[N], bk[N];
void Solve(int x){
if (!x)
return;
if (!(x & 1))
fr[++fr[0]] = cnt++, Solve(x - 1);
else bk[++bk[0]] = cnt++, Solve(x >> 1);
}
signed main(){
cin >> n;
Solve(n);
cout << cnt + 100 << endl;
for (int i = 1; i <= fr[0]; i++)
cout << cnt - fr[i] << " " ;
for (int i = bk[0]; i >= 1; i--)
cout << cnt - bk[i] << " " ;
for (int i = 1; i <= 100; i++)
cout << i << " " ;
return 0;
}
P8326 [COCI2021-2022#5] Fliper
神奇题目,首先要了解无向图上欧拉回路的性质
熟练掌握Hierholzer 算法后,再来分析这道题目
将此题转化为图论问题,按照挡板的到达关系建图
考虑只要染两种颜色的特殊情况
由于我们考虑的是环内颜色,其实可以将每个环缩点,将每个挡板设为一条边,连接它正反两个面所属的环,边权为挡板的颜色
我们于是把题目转化为黑白染色问题,进一步分析,可以将无向图转化为有向图,设黑边指向当前点、白边从当前点出发
重新审视题目,发现求的是一种染色方案,使得每个点出入度相等
分析题目,无解的情况必然是有点总度数
有解的情况便是欧拉回路 Hierholzer 算法的经典应用
考虑如何处理染四种颜色
我们发现,对于染白的与染黑的边,我们可以再分别跑一遍欧拉回路,考虑位运算处理最终解
附代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
char typ;
int n, m, tp = 0, circle = 0, flg = 0;
int st[N<<1], bel[N<<1], cnt[N<<1], col[N], ans[N];
int vis1[N<<1], vis2[N<<1], vis3[N<<1];
struct node{
int id, typ, x, y;
}pt[N];
struct e{
int to, nxt;
};
struct Graph{
int tot, head[N<<1], deg[N<<1];
e edge[N<<1];
Graph() {tot = 0;}
void add(int a, int b){
edge[++tot].to = b;
deg[a]++;
edge[tot].nxt = head[a];
head[a] = tot;
}
}g1, g2;
void Init(int x, int y){
flg = 0;
for (int i = 0; i <= x; i++){
cnt[i] = g2.head[i];
vis2[i] = 0;
}
for (int i = 1; i <= y; i++)
vis3[i] = 0;
}
bool cmp1(node a, node b){
if (a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
bool cmp2(node a, node b){
if (a.y != b.y)
return a.y < b.y;
return a.x < b.x;
}
void dfs(int u, int fno){
if (vis1[u]){
++circle;
while (tp)
bel[st[tp--]] = circle;
return;
}
vis1[u] = 1;
st[++tp] = u;
for (int i = g1.head[u]; i; i = g1.edge[i].nxt){
int to = g1.edge[i].to;
if (to != fno)
dfs(to, u);
}
}
void Hierholzer1(int u){
vis2[u] = 1;
for (int i = cnt[u]; i; i = g2.edge[cnt[u]].nxt){
int id = (i+1) / 2;
cnt[u] = i;
if (!vis3[id]){
vis3[id] = 1;
Hierholzer1(g2.edge[i].to);
col[id] = flg;
flg ^= 1;
}
}
}
void Hierholzer2(int u, int x){
vis2[u] = 1;
for (int i = cnt[u]; i; i = g2.edge[cnt[u]].nxt){
int id = (i+1) / 2;
cnt[u] = i;
if (col[id] == x && !vis3[id]){
vis3[id] = 1;
Hierholzer2(g2.edge[i].to, x);
ans[id] = ((x << 1) | flg) + 1;
flg ^= 1;
}
}
}
int main(){
cin >> n;
m = n << 1;
for (int i = 1; i <= n; i++){
cin >> pt[i].x >> pt[i].y >> typ;
if (typ == '/')
pt[i].typ = 0;
else pt[i].typ = 1;
pt[i].id = i;
}
sort(pt + 1, pt + n + 1, cmp1);
for (int i = 1; i <= n; ){
int nxt = i;
while (nxt < n && pt[i].x == pt[nxt+1].x)
++nxt;
for (int j = i; j < nxt; j++){
g1.add(pt[j].id + n, pt[j+1].id);
g1.add(pt[j+1].id, pt[j].id + n);
}
i = nxt + 1;
}
sort(pt + 1, pt + n + 1, cmp2);
for (int i = 1; i <= n; ){
int nxt = i;
while (nxt < n && pt[i].y == pt[nxt+1].y)
++nxt;
for (int j = i; j < nxt; j++){
if (!pt[j].typ){
if (!pt[j+1].typ){
g1.add(pt[j].id, pt[j+1].id + n);
g1.add(pt[j+1].id + n, pt[j].id);
}else{
g1.add(pt[j].id, pt[j+1].id);
g1.add(pt[j+1].id, pt[j].id);
}
}else{
if (!pt[j+1].typ){
g1.add(pt[j].id + n, pt[j+1].id + n);
g1.add(pt[j+1].id + n, pt[j].id + n);
}else{
g1.add(pt[j].id + n, pt[j+1].id);
g1.add(pt[j+1].id, pt[j].id + n);
}
}
}
i = nxt + 1;
}
for (int i = 1; i <= m; i++)
if (!vis1[i]){
tp = 0;
dfs(i, 0);
}
for (int i = 1; i <= n; i++){
g2.add(bel[i], bel[i+n]);
g2.add(bel[i+n], bel[i]);
}
for (int i = 1; i <= circle; i++)
if (g2.deg[i] % 8)
return puts("-1"), 0;
while (g2.deg[0] % 8)
g2.add(0, 0);
Init(circle, n);
for (int i = 0; i <= circle; i++)
if (!vis2[i])
Hierholzer1(i);
Init(circle, n);
for (int i = 0; i <= circle; i++)
if (!vis2[i])
Hierholzer2(i, 0);
Init(circle, n);
for (int i = 0; i <= circle; i++)
if (!vis2[i])
Hierholzer2(i, 1);
for (int i = 1; i <= n; i++)
cout << ans[i] << " " ;
cout << endl;
return 0;
}
AT_agc001_d Arrays and Palindrome
回文的性质可以使许多位置的字符必定为一类,性质类似并查集
考虑转化为图上模型,并集化为连边,分类讨论,找找规律
以下黑边表示
- 开头的回文段
- 中间的回文段
- 末尾的回文段
容易找到的规律是,对于合法的连接,中间段长为偶数,这是构造
于是当且仅当
对于构造
开头加一,末尾减一,即可
最后特判
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m, tot = 0, a[N], cnt[2], ans[N];
int main(){
cin >> n >> m;
for (int i = 1; i <= m; i++){
cin >> a[i];
cnt[a[i]&1]++;
}
if (m == 1){
if (a[1] == 1)
cout << "1\n1\n1\n" ;
else cout << a[1] << endl << 2 << endl << 1 << " " << a[1]-1 << endl;
return 0;
}
if (cnt[1] >= 3)
return puts("Impossible"), 0;
sort(a + 1, a + m + 1, [](int x, int y){return x%2 > y%2;});
cout << a[1] << " " ;
for (int i = 3; i <= m; i++)
cout << a[i] << " " ;
cout << a[2] << endl;
ans[++tot] = a[1] + 1;
for (int i = 3; i <= m; i++)
ans[++tot] = a[i];
if (a[2] >= 2)
ans[++tot] = a[2] - 1;
cout << tot << endl;
for (int i = 1; i <= tot; i++)
cout << ans[i] << " " ;
cout << endl;
return 0;
}