[CF2152D]D. Division Versus Addition题解
D. Division Versus Addition
time limit per test
2 seconds
memory limit per test
1024 megabytes
For an array b=[b1,b2,…,bm] of length m (bi≥2), consider the following two-player game played by Poby and Rekkles.
- The players take turns, with Poby moving first.
- On Poby's turn, he must choose an element x≥2 and replace it with ⌊x2⌋. In other words, he picks i (1≤i≤m) such that bi≥2, then does bi:=⌊bi2⌋.
- On Rekkles' turn, he must choose an element x≥2 from the array b and replace it with x+1. In other words, he picks i (1≤i≤m) such that bi≥2, then does bi:=bi+1.
The game ends once all elements in the array b are equal to 1.
Define the score of the game as the number of moves that Poby makes. Poby's goal is to minimize the score, while Rekkles's goal is to maximize the score.
Then, the value of the array b is the score of the game when both players play optimally.
You are given an integer array a of length n (ai≥2).
Answer q independent queries. In each query, you are given a range 1≤l≤r≤n and must find the value of the array [al,al+1,…,ar].
讯飞听见 翻译
对于长度为 m ( bi≥2 )的数组 b=[b1,b2,…,bm] ,考虑以下由Poby和Rekkles进行的两人游戏。-球员轮流上场,波比先走。
-轮到Poby时,他必须选择一个元素 x≥2 并将其替换为 ⌊x2⌋ 。
换句话说,他选择 i ( 1≤i≤m ),使得 bi≥2 ,然后选择 bi:=⌊bi2⌋ 。-轮到Rekkles时,他必须从数组 b 中选择一个元素 x≥2 ,并将其替换为 x+1 。
换句话说,他选择 i ( 1≤i≤m ),使得 bi≥2 ,然后选择 bi:=bi+1 。当数组 b 中的所有元素都等于 1 时,游戏结束。
将游戏的得分定义为Poby移动的次数。
Poby的目标是最小化得分,而Rekkles的目标是最大化得分。然后,数组 b 的值是当两个玩家都玩得最好时游戏的分数。
您将得到一个长度为 n ( ai≥2 )的整数数组 a 。
回答 q 个独立问题。
在每个查询中,您都会得到一个范围 1≤l≤r≤n ,并且必须找到数组 [al,al+1,…,ar] 的值。
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains two integers n and q (1≤n,q≤250000) — the length of the array and the number of queries.
The next line contains n integers a1,a2,…,an (2≤ai≤109) — the elements of the array a.
Then q lines follow. The j-th of them contains two integers lj and rj (1≤lj≤rj≤n) — the range of the subarray for the i-th query.
It is guaranteed that the sum of n over all test cases does not exceed 250000.
It is guaranteed that the sum of q over all test cases does not exceed 250000.
讯飞听见 翻译
输入
每个测试包含多个测试用例。第一行包含测试用例的数量 t ( 1≤t≤104 )。
测试用例的描述如下。
每个测试用例的第一行包含两个整数 n 和 q ( 1≤n,q≤250000 )
-数组的长度和查询的数量。
下一行包含 n 个整数 a1,a2,…,an ( 2≤ai≤109 )
—数组 a 的元素。
然后是 q 行。
第 j 个包含两个整数 lj 和 rj ( 1≤lj≤rj≤n )—第 i 个查询的子数组的范围。保证所有测试用例的 n 之和不超过 250000 。
保证所有测试用例的 q 之和不超过 250000 。
Output
For each test case, output q lines. The i-th line should contain a single integer representing the answer to the i-th query.
讯飞听见 翻译
输出
对于每个测试用例,输出 q 行。第 i 行应包含表示第 i 个查询的答案的单个整数。
Example
Input
Copy
2
5 5
4 3 2 5 6
1 1
1 2
2 4
3 5
1 5
10 1
314 159 265 358 979 323 846 264 338 327
1 10
Output
Copy
2
3
5
6
10
91
Note
Explanation of the first test case, first query (1 1):
The subarray is [4].
- Poby: 4→⌊42⌋=2. The array is [2].
- Rekkles: 2→3. The array is [3].
- Poby: 3→⌊32⌋=1. The array is [1], so the game ends.
It can be shown that this strategy is optimal for both players. Therefore, the value of the array [4] is 2.
Explanation of the first test case, second query (1 2):
The subarray is [4,3].
- Poby: 3→⌊32⌋=1. The array is [4,1].
- Rekkles: 4→5. The array is [5,1].
- Poby: 5→⌊52⌋=2. The array is [2,1].
- Rekkles: 2→3. The array is [3,1].
- Poby: 3→⌊32⌋=1. The array is [1,1], so the game ends.
It can be shown that this strategy is optimal for both players. Therefore, the value of the array [4,3] is 3.
讯飞听见 翻译
注意
第一个测试用例的说明,第一个查询(11):
子数组为 [4] 。
-
波比: 4→⌊42⌋=2 。数组为 [2] 。
Rekkles: 2→3 。数组为 [3] 。
- 波比: 3→⌊32⌋=1 。数组为 [1] ,游戏结束。可以证明,该策略对两个参与者都是最优的。因此,数组 [4] 的值为 2 。
第一个测试用例的说明,第二个查询(12):
子数组为 [4,3] 。
-
波比: 3→⌊32⌋=1 。数组为 [4,1] 。
雷克尔斯: 4→5 。数组为 [5,1] 。
-
波比: 5→⌊52⌋=2 。数组为 [2,1] 。
-
Rekkles: 2→3 。数组为 [3,1] 。
5.波比: 3→⌊32⌋=1 。数组为 [1,1] ,游戏结束。
可以证明该策略对两个参与者都是最优的。
因此,数组 [4,3] 的值为 3 。
思路
贪心即可。
代码见下
#include<bits/stdc++.h>
using namespace std;
long long t,n,q,a[250005],l,r,g[41],h[250005],o[250005],h2[250005],sd[250005],sd2[250005],lk=0;
int main(){
cin>>t;
while(t--){
g[0]=1;
cin>>n>>q;
for(int i=1;i<=40;i++){
g[i]=g[i-1]*2;
}
for(int i=1;i<=n;i++){
cin>>a[i];
for(int j=0;j<=40;j++){
if(g[j]>a[i]){
h[i]=j-1;
o[i]=g[j]-a[i];
break;
}
}
sd[i]=sd[i-1];
sd2[i]=sd2[i-1];
if(o[i]!=a[i]&&a[i]!=o[i]+2){
sd[i]++;
}
else if(a[i]==o[i]+2){
sd2[i]++;
}
}
for(int i=1;i<=n;i++){
h2[i]=h2[i-1]+h[i];
}
for(int i=1;i<=q;i++){
cin>>l>>r;
lk=h2[r]-h2[l-1];
cout<<lk+(sd2[r]-sd2[l-1])/2+(sd[r]-sd[l-1])<<endl;
}
}
return 0;
}

浙公网安备 33010602011771号