【无标题】QFNUACM2022暑假训练总结week-1二分

本周练习了二分算法,一周的训练下来,感到收获颇丰
首先,二分要满足的条件是,一段区间,一侧是全部满足条件的,一侧是全部不满足条件的,我们寻找的是最中间的临界值
二分的模板包括两种:
(1)找符合答案的最左侧值
while (l < r)
{
    int mid = l + r >> 1;
    if (arr[mid] >= x) r = mid;
    else l = mid + 1;
}
(2)找符合答案的最右侧值
while (l < r)
{
int mid = l + r + 1 >> 1;
if (arr[mid] <= x) l = mid;
else r = mid - 1;
}
浮点二分的模板
while (r - l > 1e-6)
{
double mid = (l + r) / 2;
if (mid * mid >= x) r = mid;
else l = mid;
}
if后的括号是check的内容

二分中对我来说最大的挑战还是check函数,在CF1612C中,因为check函数推导错误,花了很多时间debug, 还有在浮点二分中常常有精度问题,可以把while改成for(int i = 0; i < 100; i++), 使整个循环一百遍,提高精度。

CF1612C题面

# Chat Ban

## 题面翻译

现在小A想发送一个 $2n-1$ 的符号三角形,具体如下:

第 $1$ 行,输出 $1$ 个符号;

第 $2\to n$ 行,每行比前一行多输出一个符号;

第 $n+1\to 2n−1$ 行,每一行比前一行少输出一个符号。

如果小A再发完某条信息后,所发送的符号总数大于等于 $k$ ,小 A 将会被禁言。

现在请你求出在小 A 被禁言前最多能发送的信息个数

## 题目描述

You are a usual chat user on the most famous streaming platform. Of course, there are some moments when you just want to chill and spam something.

More precisely, you want to spam the emote triangle of size $ k $ . It consists of $ 2k-1 $ messages. The first message consists of one emote, the second one — of two emotes, ..., the $ k $ -th one — of $ k $ emotes, the $ k+1 $ -th one — of $ k-1 $ emotes, ..., and the last one — of one emote.

For example, the emote triangle for $ k=3 $ consists of $ 5 $ messages:

 ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1612C/2949e1c874315434d0b3c1f5e20dee7c9097dc7a.png)Of course, most of the channels have auto moderation. Auto moderator of the current chat will ban you right after you spam at least $ x $ emotes in succession (you can assume you are the only user in the chat). Now you are interested — how many messages will you write before getting banned? Or maybe you will not get banned at all (i.e. will write all $ 2k-1 $ messages and complete your emote triangle successfully)? Note that if you get banned as a result of writing a message, this message is also counted.

You have to answer $ t $ independent test cases.

## 输入格式

The first line of the input contains one integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases. The next $ t $ lines describe test cases.

The only line of the test case contains integers $ k $ and $ x $ ( $ 1 \le k \le 10^9; 1 \le x \le 10^{18} $ ).

## 输出格式

For each test case, print the number of messages you will write before getting banned for the corresponding values $ k $ and $ x $ .

## 样例 #1

### 样例输入 #1

```
7
4 6
4 7
1 2
3 7
2 5
100 1
1000000000 923456789987654321
```

### 样例输出 #1

```
3
4
1
4
3
1
1608737403
```

## 提示

Let's analyze the test cases of the example.

1. In the first test case, you write three messages containing $ 1 $ , $ 2 $ and $ 3 $ emotes respectively, and since $ 1 + 2 + 3 \ge 6 $ , you get banned after that.
2. In the second test case, you write four messages containing $ 1 $ , $ 2 $ , $ 3 $ and $ 4 $ emotes respectively, and since $ 1 + 2 + 3 + 4 \ge 7 $ , you get banned after that.
3. In the third test case, you write one message containing exactly $ 1 $ emote. It doesn't get you banned, since $ 1 < 2 $ , but you have already finished posting your emote triangle. So you wrote one message successfully.
4. In the fourth test case, you write four messages containing $ 1 $ , $ 2 $ , $ 3 $ and $ 2 $ emotes respectively, and since $ 1 + 2 + 3 + 2 \ge 7 $ , you get banned after that.
5. In the fifth test case, you write three messages containing $ 1 $ , $ 2 $ and $ 1 $ emote respectively. It doesn't get you banned, since $ 1 + 2 + 1 < 5 $ , but you have already finished posting your emote triangle. So you wrote three messages successfully.
6. In the sixth test case, since $ x = 1 $ , you get banned as soon as you send your first message.
7. The seventh test case is too large to analyze, so we'll skip it.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值