Rday20

Rday20

二分 + CTF入门

Computer Game

1
2
3
4
5
6
7
8
9
10
11
12
13
Vova is playing a computer game. There are in total n turns in the game and Vova really wants to play all of them. The initial charge of his laptop battery (i.e. the charge before the start of the game) is k.

During each turn Vova can choose what to do:

If the current charge of his laptop battery is strictly greater than a, Vova can just play, and then the charge of his laptop battery will decrease by a;
if the current charge of his laptop battery is strictly greater than b (b<a), Vova can play and charge his laptop, and then the charge of his laptop battery will decrease by b;if the current charge of his laptop battery is less than or equal to a and b at the same time then Vova cannot do anything and loses the game.
Regardless of Vova's turns the charge of the laptop battery is always decreases.

Vova wants to complete the game (Vova can complete the game if after each of n turns the charge of the laptop battery is strictly greater than 0). Vova has to play exactly n turns. Among all possible ways to complete the game, Vova wants to choose the one where the number of turns when he just plays (first type turn) is the maximum possible. It is possible that Vova cannot complete the game at all.

Your task is to find out the maximum possible number of turns Vova can just play (make the first type turn) or report that Vova cannot complete the game.

You have to answer q independent queries.

Input

1
2
3
The first line of the input contains one integer q (1≤q≤105) — the number of queries. Each query is presented by a single line.

The only line of the query contains four integers k,n,a and b (1≤k,n≤109,1≤b<a109) — the initial charge of Vova's laptop battery, the number of turns in the game and values a and b, correspondingly.

Output

1
For each query print one integer: -1 if Vova cannot complete the game or the maximum number of turns Vova can just play (make the first type turn) otherwise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Example
Input
6
15 5 3 2
15 5 4 3
15 5 2 1
15 5 5 1
16 7 5 2
20 5 7 3
Output
4
-1
5
2
0
1
Note
In the first example query Vova can just play 4 turns and spend 12 units of charge and then one turn play and charge and spend 2 more units. So the remaining charge of the battery will be 1.

In the second example query Vova cannot complete the game because even if he will play and charge the battery during each turn then the charge of the laptop battery will be 0 after the last turn.

思路:
给定四个数字 k, n, a, b, 其中 a > b,要求最大数 i 使得 i a + (n - i) b < n 成立

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//二分解法

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll k, n, a, b;
ll ans;
bool slove(ll mid)
{
return mid*a + (n - mid) * b < k;
}

int main()
{
ios::sync_with_stdio(false);
ll t;
cin >> t;
while(t --)
{
cin >> k >> n >> a >> b;
ll l = 0, r = n;
ans = -1;
while(l <= r)
{
ll mid = (l + r) / 2;
if(slove(mid))
{
l = mid + 1;
ans = mid;
}
else
{
r = mid - 1;
}
}
cout << ans << '\n';
}

return 0;
}





//大佬的解法
#include<bits/stdc++.h>
using namespace std;
int main()
{
ll t;
ios::sync_with_stdio(false);
cin >> t;
while(t --)
{
cin >> k >> n >> a >> b;
ll ans = 0;
k --;
if(k / b < n) ans = -1;
else
{
k -= n * b;
a -= b;
ans = min(n, k / a);
}
cout << ans << '\n';
}

return 0;
}

Training: Regex (Training, Regex)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Level 1
匹配一个空字符串,学习匹配匹配字符串开头结尾的两个符号:/^$/

^ 匹配字符串的开始

$ 匹配字符串的结束
Level 2
匹配”wechall”,/^wechall$/

Level 3
匹配以wechall或wechall4为文件名,并以.jpg/.gif/.tiff/.bmp/.png为后缀的图像,

/^wechall4?\.(?:jpg|gif|tiff|bmp|png)$/
? 重复零次或一次
所以4?表示重复0次或14,也就是wechall和wechall4都可以匹配。

\. 转义
使用\来取消.字符的特殊意义,来显示.字符本身

(?:jpg|gif|tiff|bmp|png)\
(?:exp)表示非捕获分组,匹配exp,不捕获匹配的文本,也不给此分组分配组号。

为什么要用(?:exp),而不用(exp)呢?因为直接提交/^wechall4?\.(jpg|gif|tiff|bmp|png)$/会报错:

Your pattern would capture a string, but this is not wanted. Please use a non capturing group.
您的模式将捕获一个字符串,但这是不需要的。请使用非捕获组。

所以需要使用(?:exp)非捕获分组。

至于(?:jpg|gif|tiff|bmp|png)\中的|表示分枝条件

Level 4
捕获文件名,需要对文件名添加捕获分组:

/^(wechall4?)\.(?:jpg|gif|tiff|bmp|png)$/
(wechall4?)用小括号来指定子表达式(也叫做分组),然后你就可以指定这个子表达式的重复次数了,你也可以对子表达式进行其它一些操作。
---------------- The End ----------------
0%