A. Setting up Camp

题目描述

The organizing committee plans to take the participants of the Olympiad on a hike after the tour. Currently, the number of tents needed to be taken is being calculated. It is known that each tent can accommodate up to \(3\) people.

Among the participants, there are \(a\) introverts, \(b\) extroverts, and \(c\) universals:

  • Each introvert wants to live in a tent alone. Thus, a tent with an introvert must contain exactly one person — only the introvert himself.
  • Each extrovert wants to live in a tent with two others. Thus, the tent with an extrovert must contain exactly three people.
  • Each universal is fine with any option (living alone, with one other person, or with two others).

The organizing committee respects the wishes of each participant very much, so they want to fulfill all of them.

Tell us the minimum number of tents needed to be taken so that all participants can be accommodated according to their preferences. If it is impossible to accommodate the participants in a way that fulfills all the wishes, output \(-1\).

Input

Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. This is followed by the descriptions of the test cases.

Each test case is described by a single line containing three integers \(a\), \(b\), \(c\) (\(0 \le a, b, c \le 10^9\)) — the number of introverts, extroverts, and universals, respectively.

Output

For each test case, output a single integer — the minimum number of tents, or \(-1\) if it is impossible to accommodate the participants.

Example

10
1 2 3
1 4 1
1 4 2
1 1 1
1 3 2
19 7 18
0 0 0
7 0 0
0 24 0
1000000000 1000000000 1000000000
3
-1
3
-1
3
28
0
7
8
1666666667

题目分析

这道题题目写起来很长,但是想描述的内容很简单,就是三种类型的人去住帐篷,帐篷最多容纳3人。内向的人想一个人住一个帐篷,外向的人想住在三个人的帐篷里,普通人怎么住都可以,也就是任由学校分配。学校想要满足每一个人的需求,至少需要多少个帐篷。

那我们的方案可以选择为:内向的人一人一个帐篷,外向的人和外向的人住在一起,如果最后外向的人不够住满3人的话,用普通人补齐3人,普通人都分配为3人一个帐篷,可能有帐篷没住满3人,不过普通人不受限制。

参考代码

for _ in range(int(input())):
a, b, c = map(int, input().split())
cnt = a + (b+3-1) // 3
if b % 3 != 0:
c -= (b//3 + 1) * 3 - b
cnt += (c + 3 - 1)//3
if c < 0: print(-1)
else: print(cnt)

B. Fireworks

题目描述

One of the days of the hike coincided with a holiday, so in the evening at the camp, it was decided to arrange a festive fireworks display. For this purpose, the organizers of the hike bought two installations for launching fireworks and a huge number of shells for launching.

Both installations are turned on simultaneously. The first installation launches fireworks every \(a\) minutes (i.e., after \(a, 2 \cdot a, 3 \cdot a, \dots\) minutes after launch). The second installation launches fireworks every \(b\) minutes (i.e., after \(b, 2 \cdot b, 3 \cdot b, \dots\) minutes after launch).

Each firework is visible in the sky for \(m + 1\) minutes after launch, i.e., if a firework was launched after \(x\) minutes after the installations were turned on, it will be visible every minute from \(x\) to \(x + m\), inclusive. If one firework was launched \(m\) minutes after another, both fireworks will be visible for one minute.

What is the maximum number of fireworks that could be seen in the sky at the same time?

Input

Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Then follow the descriptions of the test cases.

The first and only line of each test case contains integers \(a\), \(b\), \(m\) (\(1 \le a, b, m \le 10^{18}\)) — the frequency of launching for the first installation, the second installation, and the time the firework is visible in the sky.

Output

For each set of input data, output a single number — the maximum number of fireworks that can be seen simultaneously.

Example

6
6 7 4
3 4 10
7 8 56
5 6 78123459896
1 1 1
1 1 1000000000000000000
2
7
17
28645268630
4
2000000000000000002

题目分析

有两个烟花发生装置,分别隔不同的时间发射烟花,烟花在空中的停留时间都是一样的。题目就是要求我们求出在某一时刻内能看到最多的烟花次数。两个数之间一定有公倍数,也就是两个装置存在一个时刻同时发射烟花,我们再求出在第一发烟花发射到熄灭之间又有多少烟花被发射,在第一个烟花熄灭的那个时刻,我们看到的烟花数最多。

参考代码

t = int(input())
for _ in range(t):
a, b, m = map(int, input().split())
ans = m//a + m//b + 2
print(ans)

C. Left and Right Houses

题目描述

In the village of Letovo, there are \(n\) houses. The villagers decided to build a big road that will divide the village into left and right sides. Each resident wants to live on either the right or the left side of the street, which is described as a sequence \(a_1, a_2, \dots, a_n\), where \(a_j = 0\) if the resident of the \(j\)-th house wants to live on the left side of the street; otherwise, \(a_j = 1\).

The road will pass between two houses. The houses to the left of it will be declared the left-side, and the houses to the right will be declared the right-side. More formally, let the road pass between houses \(i\) and \(i+1\). Then the houses at positions between \(1\) and \(i\) will be on the left side of the street, and at positions between \(i+1\) and \(n\) will be on the right side. The road also may pass before the first and after the last house; in this case, the entire village is declared to be either the right or left side, respectively.

To make the design fair, it was decided to lay the road so that at least half of the residents on each side of the village are satisfied with the choice. That is, among \(x\) residents on one side, at least \(\lceil\frac{x}{2}\rceil\) should want to live on that side, where \(\lceil x \rceil\) denotes rounding up a real number \(x\).

To the left of the road, there will be \(i\) houses, among the corresponding \(a_j\) there must be at least \(\lceil\frac{i}{2}\rceil\) zeros. To the right of the road, there will be \(n-i\) houses, among the corresponding \(a_j\) there must be at least \(\lceil\frac{n-i}{2}\rceil\) ones.

Determine after which house \(i\) the road should be laid in order to satisfy the described condition and be as close to the middle of the village as possible. Formally, among all suitable positions \(i\), minimize \(\left|\frac{n}{2} - i\right|\).

If there are multiple suitable positions \(i\) with the minimum \(\left|\frac{n}{2} - i\right|\), output the smaller one.

Input

Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot 10^4\)). The description of the test cases follows.

The first line of each test case contains a single integer \(n\) (\(3 \le n \le 3\cdot 10^5\)). The next line of each test case contains a string \(a\) of length \(n\), consisting only of \(0\) and \(1\).

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

Output

For each test case, output a single number \(i\) — the position of the house after which the road should be laid (if it should be laid before the first house, output \(0\)). We can show that the answer always exists.

Example

7
3
101
6
010111
6
011001
3
000
3
110
3
001
4
1100
2
3
2
3
0
1
0

题目分析

题目的意思就是选一条路划分出左右部分,需要满足条件:左半部分的\(0\)要多于\(1\),右半部分的\(1\)要多于\(0\)。

这道题可以用前缀和优化,如果一个区间的前缀和大于区间的元素数量\(\div2\),则\(1\)多,反之则\(0\)多。把满足条件的路都存到数组中,选最靠近\(n/2\)的路作为答案。

参考代码

for _ in range(int(input())):
n = int(input())
a=[int(x) for x in input()]
b = [0]
for i in range(n):
b.append(b[-1] + a[i])
ans=[]
for i in range(n+1):
c=i-b[i]
d=b[-1]-b[i]
if c>=i/2 and d>=(n-i)/2:
ans.append(i) ans.sort(key=lambda x: abs((n/2)-x))
print(ans[0])

D. Seraphim the Owl

题目描述

The guys lined up in a queue of \(n\) people, starting with person number \(i = 1\), to ask Serafim the Owl about the meaning of life. Unfortunately, Kirill was very busy writing the legend for this problem, so he arrived a little later and stood at the end of the line after the \(n\)-th person. Kirill is completely dissatisfied with this situation, so he decided to bribe some people ahead of him.

For the \(i\)-th person in the queue, Kirill knows two values: \(a_i\) and \(b_i\). If at the moment Kirill is standing at position \(i\), then he can choose any position \(j\) such that \(j &lt; i\) and exchange places with the person at position \(j\). In this case, Kirill will have to pay him \(a_j\) coins. And for each \(k\) such that \(j &lt; k &lt; i\), Kirill will have to pay \(b_k\) coins to the person at position \(k\). Kirill can perform this action any number of times.

Kirill is thrifty, so he wants to spend as few coins as possible, but he doesn't want to wait too long, so Kirill believes he should be among the first \(m\) people in line.

Help Kirill determine the minimum number of coins he will have to spend in order to not wait too long.

Input

Each test consists of several sets of input data. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Then follows the description of the test case.

The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le m \le n \le 200\,000\)) — the number of people in the queue besides Kirill and the maximum allowable final position of Kirill, respectively.

The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) separated by spaces (\(1 \le a_i \le 10^9\)).

The third line contains \(n\) integers \(b_1, b_2, \dots, b_n\) separated by spaces (\(1 \le b_i \le 10^9\)).

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

Output

For each test case, output a single integer — the minimum number of coins Kirill needs to spend.

Example

4
4 2
7 3 6 9
4 3 8 5
6 2
6 9 7 1 8 3
5 8 8 1 4 1
7 7
7 2 9 2 6 5 9
9 1 10 7 1 4 9
2 1
2 3
1 1
14
22
9
3

题目分析

Kirill想要排到别人的前面,所以要贿赂前面的所有人,当然贿赂的钱是不一样的。如果是直接换的那个人要支付\(a_i\),但是在他后面以及自己前面的这些人都要支付\(b_k\)。

其实就是在\(a_i\)和\(b_i\)之间取最小值,如果取的是\(a_i\)表示直接换的,如果取的是\(b_i\)相当于付一个同意费。自己最后到达的位置必须取\(a_i\),需要注意的是可能换到前面的位置比后面的位置花费更少。

参考代码

for _ in range(int(input())):
n, m = map(int, input().split())
a = list(map(int, input().split()))[::-1]
b = list(map(int, input().split()))[::-1]
prex = [0]
for i in range(n):
prex.append(prex[-1] + min(a[i], b[i]))
mmin = prex[n-m] + a[n-m]
for i in range(n-m+1, n+1):
mmin = min(mmin, prex[i-1] + a[i-1])
print(mmin)

E. Binary Search

题目描述

Anton got bored during the hike and wanted to solve something. He asked Kirill if he had any new problems, and of course, Kirill had one.

You are given a permutation \(p\) of size \(n\), and a number \(x\) that needs to be found. A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).

You decided that you are a cool programmer, so you will use an advanced algorithm for the search — binary search. However, you forgot that for binary search, the array must be sorted.

You did not give up and decided to apply this algorithm anyway, and in order to get the correct answer, you can perform the following operation no more than \(2\) times before running the algorithm: choose the indices \(i\), \(j\) (\(1\le i, j \le n\)) and swap the elements at positions \(i\) and \(j\).

After that, the binary search is performed. At the beginning of the algorithm, two variables \(l = 1\) and \(r = n + 1\) are declared. Then the following loop is executed:

  1. If \(r - l = 1\), end the loop
  2. \(m = \lfloor \frac{r + l}{2} \rfloor\)
  3. If \(p_m \le x\), assign \(l = m\), otherwise \(r = m\).

The goal is to rearrange the numbers in the permutation before the algorithm so that after the algorithm is executed, \(p_l\) is equal to \(x\). It can be shown that \(2\) operations are always sufficient.

Input

Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 2\cdot 10^4\)) — the number of test cases. Then follow the descriptions of the test cases.

The first line of each test case contains two integers \(n\) and \(x\) (\(1 \le x \le n \le 2\cdot 10^5\)) — the length of the permutation and the number to be found.

The second line contains the permutation \(p\) separated by spaces (\(1 \le p_i \le n\)).

It is guaranteed that the sum of the values of \(n\) for all test cases does not exceed \(2\cdot 10^5\).

Output

For each test case, output an integer \(k\) (\(0 \le k \le 2\)) on the first line — the number of operations performed by you. In the next \(k\) lines, output \(2\) integers \(i\), \(j\) (\(1 \le i, j \le n\)) separated by a space, indicating that you are swapping the elements at positions \(i\) and \(j\).

Note that you do not need to minimize the number of operations.

Example

5
6 3
1 2 3 4 5 6
6 5
3 1 6 5 2 4
5 1
3 5 4 2 1
6 3
4 3 1 5 2 6
3 2
3 2 1
0
1
3 4
2
2 4
1 5
2
4 5
2 4
1
1 3

题目分析

其实每次需要操作的数都可以只是\(1\)。

先对这组排列数使用二分法,返回出\(l\)指针最后停留的位置。题目需要我们满足\(p_l = x\),也就是说我们把\(l\)指针指向的元素和\(x\)的元素换一下,那么\(l\)指针指向的元素就一定是\(x\)了。

参考代码

for _ in range(int(input())):
n, x = map(int, input().split())
p = [0] + list(map(int, input().split()))
def bin_search(x):
l, r = 1, n+1
while l + 1 != r:
m = (l + r) // 2
if p[m] <= x: l = m
else: r = m
return l print(1)
print(p.index(x), bin_search(x))

Codeforces Round 935 (Div. 3)的更多相关文章

  1. 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 ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  7. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  8. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  9. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

  10. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

随机推荐

  1. jeecg-boot中导出excel冲突问题

    jeecg-boot自带的库是autopoi,如果自定义导出excel引入poi,则需要POI版本要保持一致,否则会出现冲突的情况,导致这2个都用不了的情况. Autopoi底层用的是POI库,poi ...

  2. jeecgboot项目swagger2在线接口转word

    1.先找到接口文档地址 2.根据url获取接口数据 3.利用在线工具进行转换生成word 在线工具地址:在线swagger转word文档  生成的word文档如下:  

  3. Java面试题:@PostConstruct、init-method和afterPropertiesSet执行顺序?

    在Spring框架中,@PostConstruct注解.init-method属性.以及afterPropertiesSet()方法通常用于初始化Bean的逻辑.它们都提供了在Bean创建和初始化完成 ...

  4. 智能工作流:Spring AI高效批量化提示访问方案

    基于SpringAI搭建系统,依靠线程池\负载均衡等技术进行请求优化,用于解决科研&开发过程中对GPT接口进行批量化接口请求中出现的问题. github地址:https://github.co ...

  5. PageOffice6 实现 word 全文检索

    在文档服务器中存储有成千上万个文档的情况下,用户想要找到并打开包含特定关键字的文档,无疑是一项艰巨的任务.如何高效地管理和检索大量的Word文档呢? 在现有的技术解决方案中,许多方法都依赖于服务器端的 ...

  6. WPF使用事件聚合器,实现任意页面跨页通信

    前言:最近几天有好几个小伙伴玩WPF,遇到不同页面,不知道要怎么传递消息.于是,我今天就来演示一个事件聚合器的玩法,采用prism框架来实现.作为福利,内容附带了主页面打开对话框时候直接通过参数传递消 ...

  7. 【深度学习】c++部署onnx模型(Yolov5、PP-HumanSeg、GoogLeNet、UNet)

    这两天部署了好多模型,记录一下.代码链接. onnxruntime在第一张图的推理上比opencv快很多,但在后面的图上略微慢了一点. 不同的模型的部署时的输出不同,处理时需要对输出比较了解,下面分别 ...

  8. mescroll.js 使用

    mescroll.js 使用 附:点击查看中文文档 第一步:引入css和js // unpkg的CDN: <link rel="stylesheet" href=" ...

  9. Python 潮流周刊#54:ChatTTS 强大的文本生成语音模型

    本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...

  10. Linux Topicons Plus桌面工具安装

    Topicons Plus是Linux系统GNOME桌面环境的工具,方便于在工具栏显示应用小图标. 1.进入GNOME商店搜搜下载TopIcons Plus工具.下载路径:https://extens ...