A. Detective Book

模拟题,有一些细节需要注意。

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
const int N = 10010;
int n, a[N], ans = 0;
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", a + i); int tot = 1;
while(tot <= n){
ans++;
int i = tot;
tot = max(tot, a[tot]);
while(++i <= tot){
tot = max(tot, a[i]);
}
tot++;
}
printf("%d\n", ans);
return 0;
}

B. Good String

贪心。

  1. 找到从前往后第一个\(>\),把后面全部删掉(如果不删掉,结果不会更优,前面的字符也无法处理)
  2. 找从后往前第一个\(<\),把前面全部删掉,理由同上

找到这两个操作中最优的即可。

#include <cstdio>
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const int N = 110;
int n;
char s[N];
int main(){
int T; scanf("%d", &T);
while(T--){
scanf("%d%s", &n, s + 1);
int l = 1, r = n;
while(s[l] != '>') l++;
while(s[r] != '<') r--;
printf("%d\n", min(l - 1, n - r));
}
return 0;
}

C. Playlist

贪心。

我们现在题解前设定\(cnt\)为选的歌曲总长度,\(min\)为选的歌曲里最小的美好程度,即\(ans = min * cnt\)

将每首歌曲按\(b[i]\)(美好程度)从大到小排序。即保证\(b[i] >= b[i + 1] (1 <= i < n)\)

我们首先考虑没有歌曲限制,可以选择无限个歌曲。

每次选的美好程度一定是连续的一段\([1, r]\),也就是\([1, r]\)区间内的歌曲必须全部选择。


证明:反证法

如果没有放满,则一定存在一个歌曲\(i\)满足\(b[i] >= min\)。如果选择了这个歌曲,那么\(minn\)不会产生变化,又题目规定\(t[i] >= 1\),即加入后\(cnt\)一定会变大,所 以答案一定会更优。


所以,假设没有限制,我们只需要从从大到小考虑每一个歌曲,把它放入,找到最优答案,由于顺次考虑,设当前考虑歌曲为\(i\),\(minn = b[i]\)(因为从大到小排序),\(cnt\)也是增加了\(t[i]\),可以顺利求解。


如果有限制呢?

我们可以从已选的歌曲中删,因为已选的歌曲满足\(b[i] >= minn\),即使删除\(minn\)值不会发生变化。所以我们只要从已选中选一个\(t[i]\)(长度)最小的即可,这个过程可以用小根堆维护。

PS:由于写的时候以为要动态维护最小值,所以写了两个小根堆,实际只用一个,另外一个排序即可,但是其实复杂度没有变化,因为快排也是\(O(nlogn)\)...

#include <cstdio>
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
const int N = 300010;
int n, k, t[N], b[N];
priority_queue<PII> q;
priority_queue<int, vector<int>, greater<int> > minn;
int main(){
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; i++){
scanf("%d%d", t + i, b + i);
q.push(make_pair(b[i], t[i]));
}
LL cnt = 0, ans = -1;
int tot = 0;
while(!q.empty()){
PII u = q.top(); q.pop(); tot++;
minn.push(u.second);
cnt += u.second;
ans = max(ans, cnt * u.first);
if(tot == k) cnt -= minn.top(), minn.pop(), tot --;
}
printf("%lld\n", ans);
return 0;
}

D. Minimum Triangulation

考虑到只能用边上的点做三角形的边。要分割成\(n - 2\)个三角形。

可以做两种选择可以达成要求:

  1. 找一个重心\(i\),然后连接另外每一个点即可。答案是\(i * x * y + i * y + z + ...\)
  2. 换根,每次转移中心,答案是\(x * y * z + y * z * a + ...\)

要求权值最小,选择方案\((1)\)且选重心为\(1\)即可,因为换根下所有根都是$ >= 1$,所以值必然会不优。因为本人太弱,做完题看题解才知道还有通项公式,作为蒟蒻的我根本不懂...

#include <cstdio>
#include <iostream>
using namespace std;
int n, res = 0;
int main(){
scanf("%d", &n);
for(int i = 2; i < n; i++)
res += i * (i + 1);
printf("%d\n", res);
return 0;
}

E. Palindrome-less Arrays

自闭,我给看懵了。

考虑到如果存在奇回文,必定存在长度为\(3\)的回文子串,那么我们只要保证\(a[i] != a[i + 2]\)即可。这时候我们把奇偶拆分,通过\(dp\)预处理一段\(-1\)连续字串的处理方案,(用\(dp\)处理)再相乘即可。

(以上全部理解至题解,萌新根本想不到...)

设\(f[i][0/1]\) 为中间连续\(i\)个\(-1\),两端数字相同\(/\)不同的方案数

边界:\(f[0][0] = 1, f[0][1] = 0\)

\(f[i][0] = (k - 2) * f[i - 1][0] + f[i - 1][1]\)

也就是说,可以从\(i - 1\)个连续\(-1\),再加上一个数字,只需满足这个字符\(!=\)开头和结尾即可,而开头和结尾不一样,所以是\(k - 2\)。也可以从\(i - 1\)个,两端相同的过来,方案是唯一的

\(f[i][1] = (k - 1) * f[i - 1][0]\)

同理。

#include <cstdio>
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 200010, MOD = 998244353;
int n, k, a[N], b[N], l1 = 0, l2 = 0, f[N][2];
int inline mul(int x, int y){
return ((LL)x * y) % MOD;
}
int inline add(int x, int y){
return ((LL)x + y) % MOD;
}
int power(int q, int m){
int res = 1;
while(m){
if(m & 1) res = (LL)res * q % MOD;
q = (LL)q * q % MOD;
m >>= 1;
}
return res;
}
int work(int w[], int len){
if(n == 0) return 1;
int l = 1, r = len;
while(l <= len && w[l] == -1) l++;
if(l == len + 1) return mul(k, power(k - 1, len - 1));
while(r && w[r] == -1) r--;
int res = mul(power(k - 1, l - 1), power(k - 1, len - r));
int first = l;
for(l = first + 1; l <= r; l++){
if(w[l] == -1) continue;
res = mul(res, f[l - first - 1][w[first] == w[l]]);
first = l;
}
return res;
}
int main(){
scanf("%d%d", &n, &k);
f[0][0] = 1;
for(int i = 1; i <= n; i++){
f[i][0] = add(mul(k - 2, f[i - 1][0]), f[i - 1][1]);
f[i][1] = mul(k - 1, f[i - 1][0]);
}
for(int i = 1; i <= n; i++) {
if(i & 1) scanf("%d", &a[++l1]);
else scanf("%d", &b[++l2]);
}
printf("%d\n", mul(work(a, l1), work(b, l2)));
return 0;
}

Codeforces Edu Round 62 A-E的更多相关文章

  1. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  2. Codeforces Global Round 3

    Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...

  3. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  4. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  5. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

  6. CodeForces Global Round 1

    CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...

  7. Codeforces Global Round 1 - D. Jongmah(动态规划)

    Problem   Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...

  8. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  9. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

随机推荐

  1. binary hacks读数笔记(共享库)

    共享库从文件结构上来讲,与共享对象没什么区别.Linux下,共享库就是普通的ELF共享对象. 1.共享库命名: libname.so.x.y.z :其中最前面使用前缀lib,中间是库的名字和后缀&qu ...

  2. netty简介2

    作者:知乎用户 链接:https://www.zhihu.com/question/24322387/answer/282001188 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业 ...

  3. 这 12 款 IDEA 插件你用过几款?

    搞 Java开发用什么软件,当然是神器idea了,那么,idea的插件对于你来说就是必不可少的了,不仅可以提高自己的编码效率,还可以减轻工作时的枯燥烦闷.接下来就来说说,我平时敲代码用的什么插件吧. ...

  4. vue-子组件为接受到父组件的数据

    问题描述: 父组件请求数据,正在处理,未处理完,子组件已经加载,传递的为对象,子组件打印接受的数据为空 解决方式:(数据处理完之后,再强行给子组件赋值) 1.在子组件中添加 :ref="ch ...

  5. Single Depth peeling 顺序无关渲染(OIT)

    什么是顺序无关渲染 在3D渲染中,物体的渲染是按一定的顺序渲染的,这也就可能导致半透明的物体先于不透明的物体渲染,结果就是可能出现半透明物体后的物体由于深度遮挡而没有渲染出来.对于这种情况通常会先渲染 ...

  6. Springboot整合163邮箱部署项目时发送不了邮件的解决方案

    部署到腾讯云服务器时项目报错 Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: sm ...

  7. ubuntu安装php的 mongodb扩展

    wget https://pecl.php.net/get/mongodb-1.2.6.tgztar -zxvf mongodb-1.2.6.tgz cd mongodb-1.2.6 /usr/bin ...

  8. Java之 函数(五)

    第一部分 : IDEA开发工具 1.数组 1.1 数组介绍 ​ 数组就是存储数据长度固定的容器,存储多个数据的数据类型要一致. 1.2 数组的定义格式 1.2.1 第一种格式 ​ 数据类型[] 数组名 ...

  9. 抓包工具fiddler使用-初级

    参考 https://kb.cnblogs.com/page/130367/#introduce

  10. 【初等数论】费马小定理&欧拉定理&扩展欧拉定理(暂不含证明)

    (不会证明--以后再说) 费马小定理 对于任意\(a,p \in N_+\),有 \(a^{p-1} \equiv 1\pmod {p}\) 推论: \(a^{-1} \equiv a^{p-2} \ ...