Alice 和 Bob 正在玩一个游戏,游戏分为 n 个回合,Alice 和 Bob 要轮流对一个数 x 进行操作,已知这个数初始值是 0。
具体每个回合的行动规则如下:
Alice 选择一个在区间 [0,k] 之间的实数 t。
Bob 可以选择让 x 变成 x+t 或者 x−t,但是 Bob 在 n 个回合之内至少选择 m 次让 x 变成 x+t。
Alice想让最终的 x 最大,Bob 想让最终的 x 最小。
已知双方均采用最优策略,求最终的 x 值(对 109+7 取模)。
数据范围保证:1≤m≤n≤2000,k<109+7。
输入格式
The first line of the input contains a single integer t ( 1≤t≤1000 ) — the number of test cases. The description of test cases follows.
Each test case consists of a single line containing the three integers, n , m , and k ( 1≤m≤n≤2000,0≤k<109+7 ) — the number of turns, how many of those turns Bob has to add, and the biggest number Alice can choose, respectively.
It is guaranteed that the sum of n over all test cases does not exceed 2000 .
输出格式
For each test case output a single integer number — the score of the optimal game modulo 109+7 .
Formally, let M=109+7 . It can be shown that the answer can be expressed as an irreducible fraction qp , where p and q are integers and q≡0(modM) . Output the integer equal to p⋅q−1modM . In other words, output such an integer x that 0≤x<M and x⋅q≡p(modM) .
样例 #1
样例输入 #1
代码
73 3 22 1 106 3 106 4 10100 1 14 4 069 4 20
样例输出 #1
代码
65375000012500000026958557139049735962
提示
In the first test case, the entire game has 3 turns, and since m=3 , Bob has to add in each of them. Therefore Alice should pick the biggest number she can, which is k=2 , every turn.
In the third test case, Alice has a strategy to guarantee a score of 875≡375000012(mod109+7) .
In the fourth test case, Alice has a strategy to guarantee a score of 245≡500000026(mod109+7) .
Bob想让结果尽量小,所以他肯定会恰好卡边选择 m 次加法操作,因为多选加法会使结果变大产生劣势。
先玩个小样例:
假设当前 n=2 , m=1。
两轮游戏中 Alice 分别选择了 ti。
当 t1<t2 时,Bob 在第一轮一定会选择加法,那么此时第二轮就只能减法了,Alice 绝顶聪明,所以第二轮 Alice 会选择让 t2=k,这样最终 x 的结果就是 +k−t1。
当 t1>t2 时,Bob 在第一轮一定会选择减法,第二轮加法,Alice 会让 t2=0 ,所以最终 x 答案是 +t1−0=t1。
Apart from plush toys, Imp is a huge fan of little yellow birds!
一条直线上有 n 棵树,第 i 棵树上有 ci 只鸟。
在第 i 棵树底下召唤一只鸟的魔法代价是 costi。每召唤一只鸟,魔法上限会增加 B。从一棵树走到另一棵树,会增加魔法 X。一开始的魔法和魔法上限都是 W。
只能从编号为 1 的树开始,每次移动到编号为当前编号 +1 的树。
问最多能够召唤的鸟的个数。
输入格式
The first line contains four integers n , W , B , X ( 1<=n<=103,0<=W,B,X<=109 ) — the number of trees, the initial points of mana, the number of points the mana capacity increases after a bird is summoned, and the number of points restored when Imp moves from a tree to the next one.
The second line contains n integers c1,c2,...,cn ( 0<=ci<=104 ) — where ci is the number of birds living in the i -th nest. It is guaranteed that ∑i=1nci≤104 .
The third line contains n integers cost1,cost2,...,costn ( 0<=costi<=109 ), where costi is the mana cost to summon a bird from the i -th nest.
输出格式
Print a single integer — the maximum number of birds Imp can summon.
样例 #1
样例输入 #1
代码
2 12 0 43 44 2
样例输出 #1
代码
6
样例 #2
样例输入 #2
代码
4 1000 10 351 2 4 51000 500 250 200
样例输出 #2
代码
5
样例 #3
样例输入 #3
代码
2 10 7 112 106 1
样例输出 #3
代码
11
提示
In the first sample base amount of Imp’s mana is equal to 12 (with maximum capacity also equal to 12 ). After he summons two birds from the first nest, he loses 8 mana points, although his maximum capacity will not increase (since B=0 ). After this step his mana will be 4 of 12 ; during the move you will replenish 4 mana points, and hence own 8 mana out of 12 possible. Now it’s optimal to take 4 birds from the second nest and spend 8 mana. The final answer will be — 6 .
In the second sample the base amount of mana is equal to 1000 . The right choice will be to simply pick all birds from the last nest. Note that Imp’s mana doesn’t restore while moving because it’s initially full.
钱数是 1e9 不能做dp下标。
设 fi,j 为走到了第 i 棵数下,已经召唤了 j 只鸟 这时剩下的魔力数。(怎么想到的?不知道)
假设在这棵树下召唤了 k 只 bird, fi,j=max{fi−1,j−k−k×costi}+x
In the first example, one valid way to select the elements is [1,2,3,4,5] . All subsegments satisfy at least one of the criteria. For example, for the subsegment l=1 , r=2 we have that the element 2 is not selected, satisfying the first criterion. For the subsegment l=3 , r=5 we have 3+4+5=12≥2⋅3 , satisfying the second criterion.
We can’t select all elements, because in this case for l=1 , r=2 all elements are selected and we have a1+a2=3<2⋅2 . Thus, the maximum number of selected elements is 4 .
In the second example, one valid solution is [2,4,2,4,2,4,2,4,2,4] .
In the third example, one valid solution is [−10,−5,−10] .
In the fourth example, one valid solution is [9,9,−3] .
t 组数据,每组给定一个长度为 n 的 01 串 S 。求 S 的子串个数,满足经过若干次操作,可将该子串长度变为 1 。
1≤t≤1000,1≤n≤2×105,∑n≤2×105
输入格式
The first line contains an integer t ( 1≤t≤1000 ) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n ( 1≤n≤2⋅105 ) — the size of S .
The second line of each test case contains a binary string S of n characters S1S2…Sn . ( Si= 0 or Si= 1 for each 1≤i≤n )
It is guaranteed that the sum of n over all test cases doesn’t exceed 2⋅105 .
输出格式
For each test case, output the number of pairs of integers (l,r)1≤l≤r≤n such that S[l…r] (the substring of S from l to r ) is a paranoid string.
样例 #1
样例输入 #1
代码
511201310041001511111
样例输出 #1
代码
13485
提示
In the first sample, S already has length 1 and doesn’t need any operations.
In the second sample, all substrings of S are paranoid. For the entire string, it’s enough to perform the first operation.
In the third sample, all substrings of S are paranoid except [S2S3] , because we can’t perform any operations on it, and [S1S2S3] (the entire string).
int build(int l, int r)
{
for (int i = l ; i <= r ; i ++)
{
int k = top;
while (k > 0 and a[stk[k]] > a[i])
{
k --;
}
if (k)
{
rs[stk[k]] = i;
}
if (k < top)
{
ls[i] = stk[k + 1];
}
stk[++ k] = i;
top = k;
}
return stk[1]; // 最小的就是根,根就是最小的。
}
int query(int root, int l, int r) // 随机数据下 树高期望为 logn 所以随机数据下复杂度是可以的,但是这仍然会被卡成 O(n) 应该配合其他稳定复杂度求 LCA 的算法食用。
{
while (root < l or root > r)
{
if (root < l)
{
root = rs[root];
}
else
{
root = ls[root];
}
}
return root;
}
这是一个返回最小值的代码,如果需要查找最大值可以给数据全部变成负的(
性质
笛卡尔树有一个美妙的性质,对于 [l,r] 的最值就是下标为 l 和 r 的节点的 LCA 对应的值。