其实这题没啥好说的,纪念一下第一次玩codeforces吧
题目如下:
B. Divine Array
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Black is gifted with a Divine array a consisting of n (1≤n≤2000) integers. Each position in a has an initial value. After shouting a curse over the array, it becomes angry and starts an unstoppable transformation.
The transformation consists of infinite steps. Array a changes at the i-th step in the following way: for every position j, aj becomes equal to the number of occurrences of aj in a before starting this step.
Here is an example to help you understand the process better:
Initial array: 2 1 1 4 3 1 2
After the 1-st step: 2 3 3 1 1 3 2
After the 2-nd step: 2 3 3 2 2 3 2
After the 3-rd step: 4 3 3 4 4 3 4
… …
In the initial array, we had two 2-s, three 1-s, only one 4 and only one 3, so after the first step, each element became equal to the number of its occurrences in the initial array: all twos changed to 2, all ones changed to 3, four changed to 1 and three changed to 1.
The transformation steps continue forever.
You have to process q queries: in each query, Black is curious to know the value of ax after the k-th step of transformation.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000). Description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤2000) — the size of the array a.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n) — the initial values of array a.
The third line of each test case contains a single integer q (1≤q≤100000) — the number of queries.
Next q lines contain the information about queries — one query per line. The i-th line contains two integers xi and ki (1≤xi≤n; 0≤ki≤109), meaning that Black is asking for the value of axi after the ki-th step of transformation. ki=0 means that Black is interested in values of the initial array.
It is guaranteed that the sum of n over all test cases doesn’t exceed 2000 and the sum of q over all test cases doesn’t exceed 100000.
Output
For each test case, print q answers. The i-th of them should be the value of axi after the ki-th step of transformation. It can be shown that the answer to each query is unique.
Example
inputCopy
2
7
2 1 1 4 3 1 2
4
3 0
1 1
2 2
6 1
2
1 1
2
1 0
2 1000000000
outputCopy
1
2
3
3
1
2
思路:自己写几个例子就可以发现,数列a[n]在一定次的操作后各元素的值就不再改变了。 题解上说是log n 次,我没想明白怎么证。
C++代码如下:
#include<bits/stdc++.h>
using namespace std;
int t,n,a[2005][2005],K,num[2005];//num用来计数上次操作后a[e][last]出现了几次
int x,k;
int main(){
cin>>t;
while(t--){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i][0];
}
for(int i=0;i<n;i++){
memset(num,0,sizeof(num));
for(int e=1;e<=n;e++)
num[a[e][i]]++;
for(int p=1;p<=n;p++)
a[p][i+1]=num[a[p][i]];
}
cin>>K;
for(int i=1;i<=K;i++){
cin>>x>>k;
k=min(k,n);
cout<<a[x][k]<<endl;
}
}
return 0;
}