Codeforces Round #543
(比赛链接)[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的更多相关文章
- Codeforces Round #543 Div1题解(并不全)
Codeforces Round #543 Div1题解 Codeforces A. Diana and Liana 给定一个长度为\(m\)的序列,你可以从中删去不超过\(m-n*k\)个元素,剩下 ...
- Codeforces Round #543 (Div. 2)B,C
https://codeforces.com/contest/1121 B 题意 给你n(<=1000)个数ai,找出最多对和相等的数,每个数只能用一次,且每个数保证各不相同 题解 重点:每个数 ...
- Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round)
A. Technogoblet of Fire 题意:n个人分别属于m个不同的学校 每个学校的最强者能够选中 黑客要使 k个他选中的可以稳被选 所以就为这k个人伪造学校 问最小需要伪造多少个 思路:记 ...
- Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希
https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...
- Codeforces Round #543 (Div. 2) D 双指针 + 模拟
https://codeforces.com/contest/1121/problem/D 题意 给你一个m(<=5e5)个数的序列,选择删除某些数,使得剩下的数按每组k个数以此分成n组(n*k ...
- 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 ...
- 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],那么需要要 ...
- Codeforces Round #539Ȟȟȡ (Div. 1) 简要题解
Codeforces Round #539 (Div. 1) A. Sasha and a Bit of Relax description 给一个序列\(a_i\),求有多少长度为偶数的区间\([l ...
- 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 ...
随机推荐
- The openssl extension is required for SSL/TLS protection but is not available
今天使用composer update发现报错:The openssl extension is required for SSL/TLS protection but is not availabl ...
- 了解一下 - Base64
Base64编码是最常见的编码方式(使用64个字符表示任意8bit字节序列),是一种基于64个可打印字符来表示任意二进制数据的方法,是从二进制转换到可见字符的过程. 使用场景 数据加密后通过Base6 ...
- 将传统 WPF 程序迁移到 DotNetCore 3.0
介绍 由于历史原因,基于 Windows 平台存在着大量的基于 .NetFramework 开发的 WPF 和 WinForm 相关程序,如果将这些程序全部基于 DotNetCore 3.0 重写一遍 ...
- IDEA中的.iml文件和.idea文件夹
.iml文件 iml文件是IntelliJ IDEA自动创建的模块文件,用于Java应用开发,存储一些模块开发相关的信息,比如一个Java组件,插件组件,Maven组件等等,还可能存储一些模块路径信息 ...
- docker 集群 zookeeper 碰到 java.net.NoRouteToHostException: Host is unreachable (Host unreachable)
最近在学 zookeeper ,按照 docker 官网的方式集群 zookeeper , 然后发现有路由找不到.最后问题解决了,随手记录下来. 原因是 firewalld 的没有信任 docker ...
- .net core下Redis帮助类
0.引入.net core环境下Redis的NuGet包,StackExchange.Redis,现目前最新的2.0.519. 帮助类Code: using System; using Syste ...
- CentOS 7 MySql 解压版安装配置
下载 访问www.mysql.com 点击DOWNLOADS-->Community-->MySQL Community Server 选择要下载的版本,目前可选择的有:5.5.5.6.5 ...
- UOJ 275. 【清华集训2016】组合数问题
UOJ 275. [清华集训2016]组合数问题 组合数 $C_n^m $表示的是从 \(n\) 个物品中选出 \(m\) 个物品的方案数.举个例子,从$ (1,2,3)(1,2,3)$ 三个物品中选 ...
- SQL insert into select 语句
遇到权限数据变更的需要批量到别的平台, 在175平台添加一个权限需要, 批量到别的现有平台, 以后的建站, 会把sql放到自动建站里面; 权限的 insert into select 表一: `ous ...
- pycharm 远程调试代码
我们在本地开发的时候,有时候需要使用到远程服务器的环境,如我们在调试微信或支付宝支付的时候. 那我们如何通过本地pycharm环境连接远程服务器进行调试呢? 1.pycharm和远程服务器连接 1)点 ...