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. C++如何实现多态

    1.   什么是多态多态是C++中的一个重要的基础,面向对象编程语言中,接口的多种不同的实现方式即为多态.2.   多态带来的好处多态带来两个明显的好处:一是不用记大量的函数名了,二是它会依据调用时的 ...

  2. SSY的队列 hash+记忆化

    题目描述 \(SSY\) 是班集体育委员,总喜欢把班级同学排成各种奇怪的队形,现在班级里有 \(N\) 个身高互不相同的同学,请你求出这 \(N\) 个人的所有排列中任意两个相邻同学的身高差均不为给定 ...

  3. 测试_QTP使用实例

    1. QTP简介 1.1QTP功能与特点 QTP是QuickTest Professional的简称,是一种自动化软件测试工具.在软件的测试过程中,QTP主要来用来通过已有的测试脚本执行重复的手动测试 ...

  4. 【转】AWVS扫描小技巧

    1.文件头加 Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html) 可以绕过狗,waf ...

  5. 打包错误:Failed to execute goal org.scala-tools:maven-scala-plugin:2.15.2:compile (default) on project MusicProject: wrap: org.apache.commons.exec.ExecuteException:

    错误:Failed to execute goal org.scala-tools:maven-scala-plugin:2.15.2:compile (default) on project Mus ...

  6. 应聘阿里,字节跳动美团90%会问到的JVM面试题! 史上最全系列!

    Java 内存分配 • 寄存器:程序计数器,是线程私有的,就是一个指针,指向方法区中的方法字节码.• 静态域:static 定义的静态成员.• 常量池:编译时被确定并保存在 .class 文件中的(f ...

  7. Vegas视频FX功能详解

    今天呢,小编就带大家走进Vegas(Win系统)视频FX的世界.那么什么是视频FX呢,就是视频制作软件Vegas中自带添加特效的地方,它可以用于添加模糊,黑白,镜像等滤镜效果,各种高大上的视频大片都需 ...

  8. Mybatis是如何封装Jdbc的?

    JDBC六个步骤 Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { //1. 加载驱动 C ...

  9. Java IDEA 根据mybatis-generator-core自动生成代码支持sqlserver获取备注(二)

    mybatis generator代码生成虽然好用,但是好像不支持sqlserver获取备注信息,这里我主要分享mybatis generator改写后支持sqlserver获取备注信息,mysql以 ...

  10. DC靶机1-9合集

    DC1 文章前提概述 本文介绍DC-1靶机的渗透测试流程 涉及知识点(比较基础): nmap扫描网段端口服务 msf的漏洞搜索 drupal7的命令执行利用 netcat反向shell mysql的基 ...