目录

  • RGB Triplets
    • 题目描述
    • 输入
    • 输出
    • 样例输入 Copy
    • 样例输出 Copy
    • 提示
  • Select Half
    • 题目描述
    • 输入
    • 输出
    • 样例输入 Copy
    • 样例输出 Copy
    • 提示
  • 心灵的抚慰
    • 题目描述
    • 输入
    • 输出
    • 样例输入 Copy
    • 样例输出 Copy
    • 提示

RGB Triplets

题目描述

We have a string S of length N consisting of R, G, and B.
Find the number of triples (i, j, k) (1≤i<j<k≤N) that satisfy both of the following conditions:
·Si≠Sj, Si≠Sk, and Sj≠Sk.
·j−i≠k−j.

Constraints
·1≤N≤4000
·S is a string of length N consisting of R, G, and B.

输入

Input is given from Standard Input in the following format:
N
S

输出

Print the number of triplets in question.

样例输入 Copy

【样例1】

4
RRGB

【样例2】

39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB

样例输出 Copy

【样例1】
1
【样例2】
1800

提示

样例1解释:
Only the triplet (1, 3, 4) satisfies both conditions. The triplet (2, 3, 4) satisfies the first condition but not the second, so it does not count.

组合数学
从只含有RGB三个字符的字符串中挑选出满足条件的三元组数量满足 (i, j, k) (1≤i<j<k≤N)
·Si≠Sj, Si≠Sk, and Sj≠Sk.
·j−i≠k−j.

typedef int itn;
int n, m;
ll a[maxn];
ll pre[maxn];
ll dp[maxn];
int main() {cin >> n;string s;cin >> s;ll cntr = 0, cntg = 0, cntb = 0;for (int i = 0; i < n; i++) {if (s[i] == 'R') cntr ++;else if (s[i] == 'G') cntg ++;else cntb ++;}ll ans = cntb * cntg * cntr;for (int i = 0; i <= n - 3; i++) { // len == 3   3-3==0   1, 2for (int j = i + 1; j <= n - 2; j++) {// len == 3  3-2 == 1  0 1 2 int p = 2 * j - i;if (p >= n) break;if (s[i] != s[j] && s[i] != s[p] && s[j] != s[p]) --ans;}}cout << ans << endl;return 0;
}
/*
4
RRGB39
RBRBGRBGGBBRRGBBRRRBGGBRBGBRBGBRBBBGBBB
*/

Select Half

题目描述

Given is an integer sequence A1,…,AN of length N.
We will choose exactly ⌊N/2⌋ elements from this sequence so that no two adjacent elements are chosen.
Find the maximum possible sum of the chosen elements.
Here ⌊x⌋ denotes the greatest integer not greater than x.

Constraints
·2≤N≤2×105
·|Ai|≤109
·All values in input are integers.

输入

Input is given from Standard Input in the following format:
N
A1 … AN

输出

Print the maximum possible sum of the chosen elements.

样例输入 Copy

【样例1】

6
1 2 3 4 5 6

【样例2】

5
-1000 -100 -10 0 10

【样例3】

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

【样例4】

27
18 -28 18 28 -45 90 -45 23 -53 60 28 -74 -71 35 -26 -62 49 -77 57 24 -70 -93 69 -99 59 57 -49

样例输出 Copy

【样例1】
12
【样例2】
0
【样例3】
5000000000
【样例4】
295

提示

样例1解释:Choosing 2, 4, and 6 makes the sum 12, which is the maximum possible value.

在n个数中选择n/2个数,下取整,选择的数不能相邻,比如选择了i号位置的数字之后,i-1以及i+1都不能选择
dp前缀和优化

typedef int itn;
int n, m;
ll a[maxn];
ll pre[maxn];
ll dp[maxn];
int main() {cin >> n;for (int i = 1; i <= n; i++) {a[i] = read;if (i == 1) pre[i] = a[i];else pre[i] = pre[i - 2] + a[i];}for (int i = 2; i <= n; i++) {if (i % 2) dp[i] = max(dp[i - 1], dp[i - 2] + a[i]);else dp[i] = max(dp[i - 2] + a[i], pre[i - 1]);}cout << dp[n] << endl;return 0;
}

心灵的抚慰

题目描述

病毒问题解决后,神牛们的心灵久久不能平静。他可以从一个程序联想到一些相似的程序。比如从程序1联想到2,从2联想到4,从4联想到6,从6联想到9……就像搜索一样一步一步越陷越深。不过同一种联想他只会联想一次。比如1、2之间他进行了一次联想,那么他不会再重新联想1到2,或2到1。如果他刚开始时想到的程序能够经过联想若干次后联想回到原程序,那不就乱回来了吗?由于神牛马上就要开乱,请在1秒内告诉他,他需要想哪个程序,以便乱回来。
给出一些程序和他们互相联想的关系(如果两个程序A、B有联系,神牛可以从A联想到B,也可以从B联想到A,但A、B之间神牛最多联想一次),请告诉神牛他需要想哪个程序,以便在最短的时间内乱回来,并输出这个最短时间。

输入

第一行有两个正整数N,M,分别表示程序个数和有多少对程序可以被神牛直接互相联想。
以下M行,每行三个正整数,分别表示一种联想的两端的程序的编号(从1开始),以及进行这种联想所需要的最短时间(≤3500)。

输出

如果神牛无论如何都再也乱不回来了,输出“He will never come back.”。
如果神牛能够乱回来,请输出神牛会乱多长时间。

样例输入 Copy

4 3
1 2 10
1 3 20
1 4 30

样例输出 Copy

He will never come back.

提示

对于100% 的数据,n≤250。

弗洛伊德求最小环

int n, m;
ll dis[307][307];
ll val[307][307];
ll ans = INF;
int main() {cin >> n >> m;for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) dis[i][j] = INF, val[i][j] = INF;}for (int i = 1; i <= m; i++) {ll u = read, v = read, w = read;val[u][v] = min(val[u][v], w);val[v][u] = min(val[v][u], w);dis[u][v] = min(dis[u][v], val[u][v]);dis[v][u] = min(dis[v][u], val[v][u]);}for (int k = 1; k <= n; k++) {for (int i = 1; i <= k - 1; i++) {for (int j = 1 + i; j <= k - 1; j++) {ans = min(ans, dis[i][j] + val[j][k] + val[k][i]);}}for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);}}}if (ans == INF) puts("He will never come back.");else cout << ans << endl;return 0;
}
/*
4 3
1 2 10
1 3 20
1 4 30
*/

UPC-2021个人训练赛第20场-部分题解相关推荐

  1. UPC 2021个人训练赛第10场

    问题 A: No Problem! 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Programming contests are being arranged so frequent ...

  2. 2021年度训练联盟热身训练赛第三场赛后补题

    2021年度训练联盟热身训练赛第三场赛后补题 A Circuit Math [题目分析] [代码展示] B Diagonal Cut [题目分析] [代码展示] C Gerrymandering [题 ...

  3. 2021年度训练联盟热身训练赛第四场 H - Rock Paper Scissors(字符串匹配,FFT)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 2021年度训练联盟热身训练赛第四场 H - Rock Paper Scissors(字符串匹配,FF ...

  4. 2021年度训练联盟热身训练赛第五场

    2021年度训练联盟热身训练赛第五场 链接:https://ac.nowcoder.com/acm/contest/13926 A Binary Seating #include<bits/st ...

  5. 2021年度训练联盟热身训练赛第八场

    目录 2021年度训练联盟热身训练赛第八场 A-Fire on Field 题意 思路 代码 B-Gene Tree 题意 思路 代码 I-Thread Knots 题意 思路 代码 J-Triang ...

  6. 2021年度训练联盟热身训练赛第三场(待补)

    文章目录 前言 一.Circuit Math(后缀表达式---栈&&fgets) 二.Diagonal Cut(gcd最大公因数,数论) 三.expected primary-expr ...

  7. 19级算法训练赛第七场

    19级算法训练赛第七场 传送门:https://vjudge.net/contest/362412#problem/J A - 程序设计:合并数字 蒜头君得到了 n 个数,他想对这些数进行下面这样的操 ...

  8. 2021UPC个人训练赛第47场

    个人训练赛第47场 A: 加工零件(最短路) 问题 A: 加工零件时间限制: 1 Sec 内存限制: 128 MB 题目描述 凯凯的工厂正在有条不紊地生产一种神奇的零件,神奇的零件的生产过程自然也很神 ...

  9. 2021百度之星初赛第一场部分题解

    写在前面 几个家长要求我写一些2021百度之星初赛第一场的题解. 1003 鸽子 原题链接 https://acm.hdu.edu.cn/showproblem.php?pid=6998 http:/ ...

最新文章

  1. STM32单片机SIM800C创客GSM短信GPRS可编程模块SDK二次开发DIY
  2. python下采样_python + opencv 如何在上采样下采样之后导出图片?
  3. 前后端分离系统使用Nginx代理https地址
  4. log4j 2.x 架构(源码)
  5. 给硬件工程师介绍SAP Cloud for Customer
  6. 汇编语言(二十九)之数值的二进制和十进制
  7. vscode有时候不能注释_给大伙儿盘几个提效/创意的 vscode 插件
  8. 命令查看mysql 是否安装_验证mysql是否安装成功的方法
  9. ❤️《大前端—模块化》
  10. VMware 虚拟机运行卡慢的解决办法
  11. Cesium和thingjs有哪些关系?
  12. php怎么获取图片信息,PHP 获取图片信息exif
  13. 原生javascript实现拖拽改变table表格行高(html)
  14. java微信群自动回复_社群运营,你该如何选择一款好用的微信群管理工具?
  15. 西南大学网络与继续教育学院课程考试试题卷
  16. python快速排名seo代码_seo评价机制图(python快速排名seo)
  17. Liunx操作-Record20—MMAP共享映射区相关的操作
  18. python抠图太模糊_【图】为什么抠图后像素变差?解决在线抠图分辨率太低的办法...
  19. c语言题模板大全,考试c语言题库
  20. TP-link WR740N 升级版本备忘

热门文章

  1. (收藏)刘德华获奖全记录“经典”
  2. 进程管理及ps的简单使用
  3. 深刻理解空间(线性空间,度量空间,赋范空间,线性赋范空间,内积空间,巴拿赫空间以及希尔伯特空间)
  4. C#生成含数字字母的随机字符串
  5. 14年至22年,我与世界杯的那些二三事
  6. C++ 算法篇 广度(宽度)优先搜索(BFS)
  7. 软件需求工程 高校教学平台 用户手册
  8. 上班人员必读:“五险一金”详解!
  9. 二分频电路Verilog设计
  10. Java实现伪造邮件发信人