(比赛链接)[http://codeforces.com/contest/1120]

A

给出一个长度为m的序列a 可以删除一些数

使得最后从没删的第一个数开始 每k个一截

截出的n个多重集合中 至少有一个包含所给的长度为s的多重集b

\(n, m, k, s, 两个序列中的数 \leq 5e5\)

考虑每个位置r 找到满足[l, r]中包含b的区间的最大的l 显然l是单调递增的

然后我们需要讨论r - l + 1大于等于k 还是小于k

update : 不过其实不用 强行r - l + 1 >= k显然可行

然后前l - 1个数删除\((l - 1) % k\)个 [l, r]中删除r - l + 1 - k个 就可以啦

维护每一个数的出现位置 在后面出现了就更新

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
using namespace std;
typedef multiset<int> :: iterator SIT;
const int N = 5e5 + 5;
int n, m, s, k, lim, tot;
int a[N], b[N], c[N];
int main(){
scanf("%d%d%d%d", &m, &k, &n, &s); lim = m - n * k;
for(int i = 1; i <= m; ++i) scanf("%d", &a[i]);
for(int i = 1, x; i <= s; ++i) scanf("%d", &x), tot += (!b[x]), ++b[x];
for(int r = 1, l = 1; r <= m; ++r){
++c[a[r]]; tot -= (c[a[r]] == b[a[r]]);
while(!tot && l < r && r - l + 1 > k && c[a[l]] > b[a[l]]) --c[a[l]], ++l;
int tt = (l - 1) % k;
if(!tot && r - l + 1 >= k && tt + (r - l + 1 - k) <= lim){
printf("%d\n", tt + (r - l + 1 - k));
for(int i = 1; i <= tt; ++i) printf("%d ", i);
for(int i = l, j = (r - l + 1 - k); i <= r && j; ++i) if(c[a[i]] > b[a[i]]){
printf("%d ", i); --c[a[i]]; --j;
}
return 0;
}
}
printf("-1");
return 0;
}

B

给出长度为n的数a和数b

在保证每一位置在整个过程中取值[0, 9]且第一个数取值[1, 9]的情况下

每次可以选择把相邻的两个数同时加一或减一

如果a不能变成b的话 输出-1

\(n \leq 1e5\)

首先可以推出: 操作的先后顺序不影响结果, 所以如果一位一位地做 最后一位不相等 那么就不成立

那么 直接一位一位移动就好了。。如何解决超过范围的问题呢?

198 -> 297 现在要把第一位加一 但是第二位就超了 所以我们先把第二位减一

那么如果第二位也不满足呢?1902 -> 2903 那么递归下去就好了。。

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define mp(x, y) make_pair(x, y)
using namespace std;
typedef multiset<int> :: iterator SIT;
typedef pair<int, int> PII;
const int N = 5e5 + 5;
const long long LIM = 1e5;
char sa[N], sb[N];
int n, a[N], b[N], c[N];
PII opt[N];
int top;
long long ans;
void solve(int x, int type){
if(ans <= 0) return ;
if(x == n){printf("-1\n"); exit(0);}
if(a[x + 1] + type > 9 || a[x + 1] + type < 0) solve(x + 1, -type);
a[x] += type, a[x + 1] += type;
if(ans > 0) --ans, printf("%d %d\n", x, type);
}
int main(){
scanf("%d%s%s", &n, sa + 1, sb + 1);
for(int i = 1; i <= n; ++i) a[i] = sa[i] - '0', b[i] = sb[i] - '0', c[i] = a[i];
for(int i = 1; i <= n - 1; ++i){
int d = b[i] - c[i]; c[i] += d, c[i + 1] += d, ans += abs(d);
}
if(c[n] != b[n]){printf("-1\n"); return 0;}
printf("%lld\n", ans); ans = min(ans, LIM);
for(int i = 1; i <= n && ans > 0; ++i){
while(a[i] < b[i] && ans > 0) solve(i, 1);
while(a[i] > b[i] && ans > 0) solve(i, -1);
}
return 0;
}

C

现在你要构造一个指定的字符串S

如果你目前建出了str 那么你有两种操作

在末尾添加一个字符 花费为a

在末尾添加一个str的子串 花费为b

\(a, b, len \leq 5000\)

边建字符串SAM边dp

每插入一个字符 就先把它放入SAM

然后从起点跑一边[i + 1, n] 用子串更新f

即f[i] = min(f[i], f[i - 1] + a), f[j] = min(f[j], f[i] + b) (S[i + 1, j]是str的子串)

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define mp(x, y) make_pair(x, y)
using namespace std;
const int N = 5005;
const int Sig = 26;
int n, a, b, f[N];
char str[N];
struct SAM{
int len[N << 1], fa[N << 1], ch[N << 1][Sig], last, ss;
void init(){ss = last = 1;}
void ins(int c){
int p = last, np = ++ss; last = ss;
len[np] = len[p] + 1;
while(p && !ch[p][c]) ch[p][c] = np, p = fa[p];
if(!p) fa[np] = 1;
else {
int q = ch[p][c];
if(len[q] == len[p] + 1) fa[np] = q;
else {
int nq = ++ss;
for(int i = 0; i < Sig; ++i) ch[nq][i] = ch[q][i];
fa[nq] = fa[q], fa[q] = fa[np] = nq, len[nq] = len[p] + 1;
while(p && ch[p][c] == q) ch[p][c] = nq, p = fa[p];
}
}
}
}sam;
int main(){
scanf("%d%d%d%s", &n, &a, &b, str + 1);
memset(f, 0x3f, sizeof(f)); f[0] = 0; sam.init();
for(int i = 1; i<= n; ++i){
sam.ins(str[i] - 'a'), f[i] = min(f[i], f[i - 1] + a);
for(int j = i + 1, cur = 1; j <= n; ++j){
cur = sam.ch[cur][str[j] - 'a'];
//printf("%d %d %d\n", cur, i, j);
if(!cur) break;
f[j] = min(f[j], f[i] + b);
}
}
printf("%d", f[n]);
return 0;
}

D

有一颗n个节点的树 每个节点有一个权值

要求钦定一些节点 将其标记

使得每个叶子到根的路上第一个遇到的被标记节点各不相同

\(n \leq 2e5\)

\(f[u][1]=\min_{v} {f[v][1]+\sum_{w≠v} f[w][0]}\)

\(f[u][0]=\min {\sum f[v][0],f[u][1]+c[u]}\)

orz yyb

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define mp(x, y) make_pair(x, y)
using namespace std;
const int N = 2e5 + 5;
const long long inf = 1e18;
struct Edge{int v, next;}edge[N << 1];
int head[N], esize;
inline void addedge(int x, int y){
edge[++esize] = (Edge){y, head[x]}, head[x] = esize;
}
int n, m; long long a[N], f[N][2]; void dfs(int x, int ff){
long long cnt = 0; bool lf = 1;
for(int i = head[x], vv; ~i; i = edge[i].next){
vv = edge[i].v; if(vv == ff) continue;
dfs(vv, x), cnt += f[vv][0], lf = 0;
}
if(lf){f[x][1] = 0, f[x][0] = a[x]; return ;} f[x][0] = f[x][1] = inf;
for(int i = head[x], vv; ~i; i = edge[i].next){
vv = edge[i].v; if(vv == ff) continue;
f[x][1] = min(f[vv][1] + cnt - f[vv][0], f[x][1]);
}
f[x][0] = min(cnt, f[x][1] + a[x]);
}
int stk[N], top;
bool vis[N][2];
inline void find(int x, int ff){
if(vis[x][0]){
if(f[x][0] == f[x][1] + a[x])
stk[++top] = x, vis[x][1] = 1; long long cnt = 0;
for(int i = head[x]; ~i; i = edge[i].next){
int vv = edge[i].v; if(vv == ff) continue;
cnt += f[vv][0];
}
//printf("%d %lld %lld %lld\n", x, f[x][0], f[x][1], cnt);
if(f[x][0] == cnt)
for(int i = head[x], vv; ~i; i = edge[i].next)
if(edge[i].v != ff) vis[edge[i].v][0] = 1; }
if(vis[x][1]){
long long tmp = inf, cnt = 0;
for(int i = head[x]; ~i; i = edge[i].next){
int vv = edge[i].v; if(vv == ff) continue;
tmp = min(tmp, f[vv][1] - f[vv][0]);
}
for(int i = head[x]; ~i; i = edge[i].next){
int vv = edge[i].v; if(vv == ff) continue;
cnt += (tmp == (f[vv][1] - f[vv][0]));
}
for(int i = head[x]; ~i; i = edge[i].next){
int vv = edge[i].v; if(vv == ff) continue;
if(cnt > 1 || tmp < f[vv][1] - f[vv][0])
vis[vv][0] = 1;
if(tmp == f[vv][1] - f[vv][0])
vis[vv][1] = 1;
}
}
for(int i = head[x], vv; ~i; i = edge[i].next)
if(edge[i].v != ff) find(edge[i].v, x);
} int main(){
memset(head, -1, sizeof(head));
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
for(int i = 1, x, y; i < n; ++i)
scanf("%d%d", &x, &y), addedge(x, y), addedge(y, x);
dfs(1, 0);
printf("%lld ", f[1][0]); vis[1][0] = 1;
find(1, 0);
printf("%d\n", top);
sort(stk + 1, stk + top + 1);
for(int i = 1; i <= top; ++i) printf("%d ", stk[i]);
return 0;
}

E 不会欸。。【雾

F

终于想明白了。。

如果有一封信放在R那里 那么之后R那里一定一直有信

连续是一个人寄信的时候 第一个一定要取 不然的话

上一次另一个人放入R的信一定会把这部分花费花掉

你放进去一封W的 再拿出来一封P的 R处的数量不变 还多寄了一封信

要是不拿出来的话 就还是P那封 一直等到下次W寄信时才能取

不过只约束了第一张 后面几张同样的就随便了

如果前面没有放入R的怎么办? 那就全是d啊

从n到1枚举什么时候第一张信放入R即可

好妙的题。。

#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <set>
#define id(x) (x - 'a')
using namespace std;
const int N = 1e5 + 5;
int n, a[N], type[N];
long long c, d, ans, cnt;
int main(){
scanf("%d%lld%lld", &n, &c, &d);
for(int i = 1; i <= n; ++i){
char opt[5];
scanf("%d%s", &a[i], opt);
type[i] = (opt[0] == 'W');
}
scanf("%d", &a[n + 1]); ans = d * n; type[n + 1] = -1;
for(int i = n, pre; i >= 1; --i){
if(type[i] == type[i + 1]) cnt += min(d, c * (pre - a[i + 1]));
else pre = a[i + 1];
ans = min(ans, cnt + c * (a[n + 1] - a[i]) + d * (i - 1));
}
printf("%lld", ans);
return 0;
}

Codeforces Round #543的更多相关文章

  1. Codeforces Round #543 Div1题解(并不全)

    Codeforces Round #543 Div1题解 Codeforces A. Diana and Liana 给定一个长度为\(m\)的序列,你可以从中删去不超过\(m-n*k\)个元素,剩下 ...

  2. Codeforces Round #543 (Div. 2)B,C

    https://codeforces.com/contest/1121 B 题意 给你n(<=1000)个数ai,找出最多对和相等的数,每个数只能用一次,且每个数保证各不相同 题解 重点:每个数 ...

  3. Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round)

    A. Technogoblet of Fire 题意:n个人分别属于m个不同的学校 每个学校的最强者能够选中 黑客要使 k个他选中的可以稳被选 所以就为这k个人伪造学校 问最小需要伪造多少个 思路:记 ...

  4. Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希

    https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...

  5. Codeforces Round #543 (Div. 2) D 双指针 + 模拟

    https://codeforces.com/contest/1121/problem/D 题意 给你一个m(<=5e5)个数的序列,选择删除某些数,使得剩下的数按每组k个数以此分成n组(n*k ...

  6. cf 20190307 Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round)

    B. Mike and Children time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. Codeforces Round #543 (Div. 1, based on Technocup 2019 Final Round) 题解

    题面戳这里 A. Diana and Liana 首先如果s>ks>ks>k一定无解,特判一下.那么我们考虑找恰好满足满足题目中的要求的区间[l,r][l,r][l,r],那么需要要 ...

  8. Codeforces Round #539&#542&#543&#545 (Div. 1) 简要题解

    Codeforces Round #539 (Div. 1) A. Sasha and a Bit of Relax description 给一个序列\(a_i\),求有多少长度为偶数的区间\([l ...

  9. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

随机推荐

  1. Ocelot + Consul + Registrator 基于Docker 实现服务发现、服务自动注册

    目录 1. Consul集群搭建 1.1 F&Q Consul官方推荐的host网络模式运行 2. Registrator服务注册工具 2.1 F&Q Registrator悬挂服务 ...

  2. nlp词性标注

    nlp词性标注 与分词函数不同,jieba库和pyltp库词性标注函数上形式相差极大. jieba的词性标注函数与分词函数相近,jieba.posseg.cut(sentence,HMM=True)函 ...

  3. 数据库原理 - 序列5 - 事务是如何实现的? - Undo Log解析

    本文节选自作者书籍<软件架构设计:大型网站技术架构与业务架构融合之道>.作者微信公众号:架构之道与术.公众号底部菜单有书友群可以加入,与作者和其他读者进行深入讨论.也可以在京东.天猫上购买 ...

  4. JS的正则表达式及回文

    function palindrome(str) { str = str.replace(/\s/g,"").replace(/[^a-zA-Z0-9]/g,"" ...

  5. Delphi 数据转换

    指针转换   Pointer——string string:=PChar(Pointer);{ Pointer指向的数据要以#0结尾.使用System.AllocMem(Size)分配的内存是用#0填 ...

  6. 对Link Map File的初步认识

    什么是Link Map File Link Map File中文直译为链接映射文件,它是在Xcode生成可执行文件的同时生成的链接信息文件,用于描述可执行文件的构造部分,包括了代码段和数据段的分布情况 ...

  7. day20 hashlib、hmac、subprocess、configparser模块

    hashlib模块:加密 import hashlib# 基本使用cipher = hashlib.md5('需要加密的数据的二进制形式'.encode('utf-8'))print(cipher.h ...

  8. Jsp监听器

    监听器: 监听器就是对内置对象的状态或者属性变化进行监听,并且做出反应的特殊的servlet,在web.xml文件中对监听器进行的配置. 内置对象有两种状态变化:产生.销毁,就是当产生内置对象的时候能 ...

  9. 通知实战 设置通知图片(iOS10以后的)

    解释两个基本扩展(Notification Content.Notification Service) Notification Content其实是用来自定义长按通知显示通知的自定义界面 Notif ...

  10. Python----逻辑回归

    逻辑回归 1.逻辑函数 sigmoid函数就出现了.这个函数的定义如下: sigmoid函数具有我们需要的一切优美特性,其定义域在全体实数,值域在[0, 1]之间,并且在0点值为0.5. 那么,如何将 ...