CodeForces Round #621 ABC (1307A+1307B+1307C) 题解

news/2024/10/4 9:24:41

A. Cow and Haybales

题面

The USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of n haybale piles on the farm. The \(i\)-th pile contains \(a_i\) haybales.

However, Farmer John has just left for vacation, leaving Bessie all on her own. Every day, Bessie the naughty cow can choose to move one haybale in any pile to an adjacent pile. Formally, in one day she can choose any two indices \(i\) and \(j\) (\(1\le i,j\le n\)) such that \(|i−j|=1\) and \(a_i>0\) and apply \(a_i=a_i−1\), \(a_j = a_j + 1\). She may also decide to not do anything on some days because she is lazy.

Bessie wants to maximize the number of haybales in pile \(1\) (i.e. to maximize \(a_1\)), and she only has \(d\) days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile \(1\) if she acts optimally!

输入

The input consists of multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 100\)) — the number of test cases. Next \(2t\) lines contain a description of test cases — two lines per test case.

The first line of each test case contains integers \(n\) and \(d\) (\(1\le n,d\le 100\)) — the number of haybale piles and the number of days, respectively.

The second line of each test case contains \(n\) integers \(a_1,a_2,\dots,a_n\) \((0\le a_i\le 100)\) — the number of haybales in each pile.

输出

For each test case, output one integer: the maximum number of haybales that may be in pile \(1\) after \(d\) days if Bessie acts optimally.

样例

输入

3
4 5
1 0 3 2
2 2
100 1
1 8
0

输出

3
101
0

注释

In the first test case of the sample, this is one possible way Bessie can end up with \(3\) haybales in pile \(1\):

  • On day one, move a haybale from pile \(3\) to pile \(2\)
  • On day two, move a haybale from pile \(3\) to pile \(2\)
  • On day three, move a haybale from pile \(3\) to pile \(1\)
  • On day four, move a haybale from pile \(2\) to pile \(1\)
  • On day five, do nothing

In the second test case of the sample, Bessie can do nothing on the first day and move a haybale from pile \(2\) to pile \(1\) on the second day.

解题思路

贪心算法:先把第二堆的移到第一堆,再把第三堆的移到第一堆,……
题目中的样例解释有点误导(😦)

代码

#include <cstdio>
#define maxn 105
using namespace std;inline int min(int a, int b)
{return a < b? a: b;
}int main(int argc, char** argv)
{int T;scanf("%d", &T);while(T--){int n, d, first;scanf("%d%d%d", &n, &d, &first);for(int i=1; i<n; i++){int x;scanf("%d", &x);if(d >= i){int num = min(d / i, x); // The max number of haybales bessie can take to pile 1d -= num * i;first += num;}}printf("%d\n", first);}return 0;
}

B. Cow and Friend

题面

Bessie has way too many friends because she is everyone's favorite cow! Her new friend Rabbit is trying to hop over so they can play!

More specifically, he wants to get from \((0,0)\) to \((x,0)\) by making multiple hops. He is only willing to hop from one point to another point on the 2D plane if the Euclidean distance between the endpoints of a hop is one of its n favorite numbers: \(a_1,a_2,…,a_n\). What is the minimum number of hops Rabbit needs to get from \((0,0)\) to \((x,0)\)? Rabbit may land on points with non-integer coordinates. It can be proved that Rabbit can always reach his destination.

Recall that the Euclidean distance between points \((x_i,y_i)\) and \((x_j,y_j)\) is \(\sqrt{(x_i−x_j)^2+(y_i−y_j)^2}\).

For example, if Rabbit has favorite numbers 1 and 3 he could hop from \((0,0)\) to \((4,0)\) in two hops as shown below. Note that there also exists other valid ways to hop to \((4,0)\) in \(2\) hops (e.g. \((0,0)\to(2, \sqrt 5)\to(4,0)\)).

Here is a graphic for the first example. Both hops have distance \(3\), one of Rabbit's favorite numbers.

In other words, each time Rabbit chooses some number \(a_i\) and hops with distance equal to \(a_i\) in any direction he wants. The same number can be used multiple times.

输入

The input consists of multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 1000\)) — the number of test cases. Next \(2t\) lines contain test cases — two lines per test case.

The first line of each test case contains two integers \(n\) and \(x\) (\(1\le n\le 10^5\), \(1\le x\le 10^9\)) — the number of favorite numbers and the distance Rabbit wants to travel, respectively.

The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1\le a_i \le 10^9\)) — Rabbit's favorite numbers. It is guaranteed that the favorite numbers are distinct.

It is guaranteed that the sum of \(n\) over all the test cases will not exceed \(10^5\).

输出

For each test case, print a single integer — the minimum number of hops needed.

样例

输入

4
2 4
1 3
3 12
3 4 5
1 5
5
2 10
15 4

输出

2
3
1
2

注释

The first test case of the sample is shown in the picture above. Rabbit can hop to \((2, \sqrt 5)\), then to \((4,0)\) for a total of two hops. Each hop has a distance of \(3\), which is one of his favorite numbers.

In the second test case of the sample, one way for Rabbit to hop \(3\) times is: \((0,0) → (4,0) → (8,0) → (12,0)\).

In the third test case of the sample, Rabbit can hop from \((0,0)\) to \((5,0)\).

In the fourth test case of the sample, Rabbit can hop: \((0,0) → (5,10\sqrt2) → (10,0)\).

解题思路

纯数学!!!

代码

#include <cstdio>
#define maxn 100005
using namespace std;int fav[maxn], n, d;int calc(int x)
{int tm = d / x, leftdist = d % x;if(leftdist == 0) return tm;if(tm == 0){for(int i=0; i<n; i++)if(fav[i] == leftdist)return 1;return 2;}return tm + 1;
}int main(int argc, char** argv)
{int T;scanf("%d", &T);while(T--){scanf("%d%d", &n, &d);int maxf = -1;for(int i=0; i<n; i++){scanf("%d", fav + i);if(fav[i] > maxf) maxf = fav[i];}printf("%d\n", calc(maxf));}return 0;
}

C. Cow and Message

题面

Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is sure that there is a secret message hidden inside.

The text is a string \(S\) of lowercase Latin letters. She considers a string \(t\) as hidden in string \(S\) if \(t\) exists as a subsequence of \(S\) whose indices form an arithmetic progression. For example, the string aab is hidden in string aaabb because it occurs at indices \(1\), \(3\), and \(5\), which form an arithmetic progression with a common difference of \(2\). Bessie thinks that any hidden string that occurs the most times is the secret message. Two occurrences of a subsequence of \(S\) are distinct if the sets of indices are different. Help her find the number of occurrences of the secret message!

For example, in the string aaabb, a is hidden \(3\) times, b is hidden \(2\) times, ab is hidden \(6\) times, aa is hidden \(3\) times, bb is hidden \(1\) time, aab is hidden \(2\) times, aaa is hidden \(1\) time, abb is hidden \(1\) time, aaab is hidden \(1\) time, aabb is hidden \(1\) time, and aaabb is hidden \(1\) time. The number of occurrences of the secret message is \(6\).

输入

The first line contains a string \(S\) of lowercase Latin letters (\(1\le |S|\le 105\)) — the text that Bessie intercepted.

输出

Output a single integer — the number of occurrences of the secret message.

样例

输入 #1

aaabb

输出 #1

6

输入 #2

usaco

输出 #2

1

输入 #3

lol

输出 #3

2

注释

In the first example, these are all the hidden strings and their indice sets:

  • a occurs at \((1), (2), (3)\)
  • b occurs at \((4), (5)\)
  • ab occurs at \((1,4), (1,5), (2,4), (2,5), (3,4), (3,5)\)
  • aa occurs at \((1,2), (1,3), (2,3)\)
  • bb occurs at \((4,5)\)
  • aab occurs at \((1,3,5), (2,3,4)\)
  • aaa occurs at \((1,2,3)\)
  • abb occurs at \((3,4,5)\)
  • aaab occurs at \((1,2,3,4)\)
  • aabb occurs at \((2,3,4,5)\)
  • aaabb occurs at \((1,2,3,4,5)\)
    Note that all the sets of indices are arithmetic progressions.
    In the second example, no hidden string occurs more than once.

In the third example, the hidden string is the letter l.

解题思路

动态规划

代码

#include <cstdio>
#define maxn 100005
#define let 26
using namespace std;typedef long long LL;char s[maxn], c;
LL cnt1[let][maxn], cnt2[let][let];int main(int argc, char** argv)
{cnt1[(s[0] = getchar()) - 'a'][0] ++;int slen = 1;while((c = getchar()) != '\n'){s[slen] = c;for(int i=0; i<let; i++)cnt1[i][slen] = cnt1[i][slen - 1];cnt1[c - 'a'][slen++] ++;}int last = slen - 1;for(int i=0; i<let; i++) if(cnt1[i][last]){cnt2[i][i] = cnt1[i][last] * (cnt1[i][last] - 1) >> 1;for(int j=0; j<slen; j++)if(s[j] != i + 'a')cnt2[i][s[j] - 'a'] += cnt1[i][last] - cnt1[i][j];}LL maxcnt = -1;for(int i=0; i<let; i++){if(cnt1[i][last] && cnt1[i][last] > maxcnt) maxcnt = cnt1[i][last];for(int j=0; j<let; j++)if(cnt2[i][j] && cnt2[i][j] > maxcnt)maxcnt = cnt2[i][j];}printf("%lld\n", maxcnt);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/58060.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Python函数之*[参数名]和**[参数名]的用处

一、*[参数名] 调用 合法调用 普通调用 *参数名一般写成*args, 如: def func(*args):print(args)可以试着调用func: >>> func(1) (1,) >>> func() () >>> func(1, 2, 3) (1, 2, 3) >>> func(dict(), set(), str(), int()) ({}, set(), ,…

discuz3.4文件包含漏洞

首先查看修复:可以看到新增代码preg_match("/^[\w-]+\.php$/i", $parse[path])) 来验证path是否为php文件,这个应该是修复路径遍历导致的文件读取漏洞。还有require ./.$_ENV[curapp]..php;这里应该是另外一个漏洞,因为$parse[path]和$_ENV[curapp]没有关联。 然后…

web 开发(4)- 数据库sql

sql创建数据库sudo mysql 进入 mysql> create database book_01安装 mysqlclient sudo apt-get install libmysqlclient-dev sudo apt-get update 远程控制SQL得到远程密码 sudo cat /etc/mysql/debian.cnf 获取IP地址 ifconfig sudo mysql 问题一,不允许远程控制 先进入本…

隧道视频监控智能分析系统

隧道视频监控智能分析系统是道路交通方式不可缺少的监管手段,隧道视频监控智能分析系统有效进行交通违法和紧急事件的全自动识别和交通出行流量的全自动数据分析,并依据城市路口、城市道路、高速路、道路、公安机关监控、隧道、公路桥梁、地下停车场等各类实际路面生态环境开…

煤矿皮带急停报警监测系统 煤矿皮带运行监控系统

煤矿皮带急停报警监测系统运用煤矿地底现场已有摄像头的视频监控画面图像,赋能现场传统摄像机具备Ai识别分析报警、监管和鉴别工作人员、机器设备、自然环境等使用标准、皮带锚索、煤矸石砖、堆煤、非法运输等异常现象、工作人员没戴安全头盔、擅自离岗、路面浓烟、水、影片等…

个人项目-论文查重

这个作业属于哪个课程 班级链接这个作业要求在哪里 个人项目 - 作业 - 计科22级12班 - 班级博客 - 博客园 (cnblogs.com)这个作业的目标 准备、创建、开发、管理、测试个人项目GitHub项目链接 https://github.com/chocohQL/3122004348-01 可运行 jar 已发布在最新 releases 项目…

加油站视频监控智能识别分析

加油站视频监控智能识别分析根据AI视频识别的加油站智能监控解决方案:依据加油站现场已经存在的高清摄像头搜集加油站视频在此基础上加油站视频监控智能识别加油站监控画面中的人的行为或者车的视频图象。智能识别工作人员行为状态,是否存在违规操作,系统自动识别员工,不戴…

Windows NoiLinux

在 Windows 下使用 NoiLinux ubuntu-noi-v2.0.iso下载 ubuntu-noi-v2.0.iso打开 VMWare,创建新的虚拟机 -> 自定义(高级)-> 下一步 -> 下一步 -> 安装程序光盘映像文件(iso),选择下载的 ubuntu-noi-v2.0.iso后面直接跳过就行了,可能需要你留意的是分配处理器内…