[CF2160B]B. Distinct Elements题解
time limit per test
1.5 seconds
memory limit per test
256 megabytes
Given an array c, let f(c) be the number of distinct elements in c. For example, f([1,2,2])=2 because there are two distinct elements in [1,2,2]: 1 and 2. Also, define c[i,j] as the subarray∗ of c bounded by positions i and j (that is, the array [ci,ci+1,…,cj]).
There is an array a of size n. An array b of n elements is constructed such that bi=f(a[1,i])+f(a[2,i])+…+f(a[i,i]). You are given the array b. Find any possible a with elements 1≤ai≤n. It is guaranteed that at least one possible a exists.
∗An array x is a subarray of an array y if x can be obtained from y by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
有道 翻译
给定一个数组 c ,设 f(c) 为 c 中不同元素的个数。例如, f([1,2,2])=2 ,因为 [1,2,2] 中有两个不同的元素: 1 和 2 。同样,将 c[i,j] 定义为 c 的子数组 ∗ ,以位置 i 和 j 为界(即数组 [ci,ci+1,…,cj] )。
存在一个大小为 n 的数组 a 。一个包含 n 元素的数组 b 被构造成 bi=f(a[1,i])+f(a[2,i])+…+f(a[i,i]) 。给定数组 b 。查找元素 1≤ai≤n 中任何可能的 a 。保证至少存在一个可能的 a 。
∗ 如果 x 可以从 y 中通过删除开始的几个(可能为零或全部)元素和结束的几个(可能为零或全部)元素来获得,则数组 x 是数组 y 的子数组。
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 an integer n (1≤n≤105) – the number of elements in a and b.
The second line of each test case contains n integers b1,b2,…,bn (1≤bi≤1018).
It is guaranteed that the sum of n over all test cases does not exceed 105.
有道 翻译
输入** **
每个测试包含多个测试用例。第一行包含测试用例的数量 t ( 1≤t≤104 )。下面是测试用例的描述。
每个测试用例的第一行包含一个整数 n ( 1≤n≤105 )— a 和 b 中的元素数量。
每个测试用例的第二行包含 n 个整数 b1,b2,…,bn ( 1≤bi≤1018 )。
保证所有测试用例 n 的和不超过 105 。
Output
For each test case, print any possible a on a new line. The array a should satisfy 1≤ai≤n.
For every test case, it is guaranteed at least one a that satisfies the conditions exists.
Example
Input
Copy
4
3
1 3 6
3
1 3 5
3
1 3 4
4
1 2 3 7
Output
Copy
1 3 2 2 3 2 3 2 2 4 4 4 1
Note
Let's verify our output for the second test case is correct:
- b1=f([2])=1
- b2=f([2,3])+f([3])=2+1=3
- b3=f([2,3,2])+f([3,2])+f([2])=2+2+1=5
有道 翻译
注意
让我们验证第二个测试用例的输出是否正确:
—— b1=f([2])=1
—— b2=f([2,3])+f([3])=2+1=3
—— b3=f([2,3,2])+f([3,2])+f([2])=2+2+1=5
思路
直接差分,判断相同,加数构造即可。
代码见下
#include<bits/stdc++.h>
using namespace std;
long long t,n,a[100005],b[100005],c[100005],lk=0;
int main(){
cin>>t;
while(t--){
cin>>n;
for(int i=1;i<=n;i++){
cin>>b[i];
c[i]=b[i]-b[i-1];
}
lk=0;
for(int i=1;i<=n;i++){
if(c[i]==i){
a[i]=++lk;
}
else{
a[i]=a[i-c[i]];
}
}
for(int i=1;i<=n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
return 0;
}

浙公网安备 33010602011771号