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
Note
The first test case was described ih the statement. It can be seen that:
k1=0 (initial array): a3=1;
k2=1 (after the 1-st step): a1=2;
k3=2 (after the 2-nd step): a2=3;
k4=1 (after the 1-st step): a6=3.
For the second test case,
Initial array: 1 1
After the 1-st step: 2 2
After the 2-nd step: 2 2
… …
It can be seen that:
k1=0 (initial array): a1=1;
k2=1000000000: a2=2;
这道题目呢,就是如果当前的序列的某个数的个数!=数字本身,那么下一次这些数字就变成这次数字的个数,我们发现,它最后一定会成为n个n,m个m,t个t…组合而成的数列,基于此,用结构体存储数据,每次遍历判断然后变更,直到满足上述条件,跳出。
我最近感觉分治算法特别适用在生活,学习的各个方面,首先拿到这道题目感觉很棘手,不过慢慢来,一点点朝着目标做,就能做出来。
另外,1.结构体要敲的熟。
2.发挥计算机的优势,计算思维
3.利用注释,减轻人脑记忆负担。注意力有限
#include<bits/stdc++.h>
using namespace std;
map<char,int>ma;
struct ndoe
{
int a[2050];//每个下标是啥?
int b[2005];//每个数有多少
}c[1000];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{ memset(c,0,sizeof c);
int chang ,ci;
scanf("%d",&chang);
for(int i =1;i<=chang;i++)
{
scanf("%d",&c[0].a[i]);
c[0].b[c[0].a[i]]++;
}//第几步的第几个下标是啥!
int hao=0,zhidao=0;
while(zhidao==0)
{
for(int i=1;i<=2000;i++)
{
if(c[hao].b[i]==i||c[hao].b[i]==0)
continue ;
else
{
zhidao=1;
hao++;
break;
}
}
// cout<<"hao:"<<hao<<"zhidao"<<zhidao<<endl;
if(zhidao==0)break;
if(zhidao==1)
{
for(int i = 1 ;i<=chang;i++)
{
c[hao].a[i]=c[hao-1].b[c[hao-1].a[i]];
}
for(int i =1;i<=chang;i++)
{
c[hao].b[c[hao].a[i]]++;
}
zhidao= 0;
}
}
scanf("%d",&ci);
for(int i =1;i<=ci;i++)
{
int k,o ;
scanf("%d%d",&k,&o);
if(o>=hao)cout<<c[hao].a[k]<<endl;
else cout<<c[o].a[k]<<endl;
}
}
return 0 ;
}
→Judgement Protocol
Test: #1, time: 0 ms., memory: 16908 KB, exit code: 0, checker exit code: 0, verdict: OK
Input
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
Output
1
2
3
3
1
2