Alice and Bob has just learned how to find average of some numbers. They got really excited and
decided to come up with a game about finding average.
The game works like this, at the start of game a sequence of numbers is written.
Then there will be several rounds in the game. In first round Alice will say a number x and Bob
has to select two index i and j. Let’s say the average of the unique numbers between i-th number and
j-th number is y. Then Alice gets abs(x− y) points in that round. In next round Alice and Bob switch
the role. The game continues this way.
While Alice and Bob enjoy playing this game, they hate calculating average of unique numbers. So,
they are asking you, their only programmer friend, to write a program that calculates the average for
them.
Input
First line of input is a number T (T ≤ 100). T test cases follow.
First line of each test case is an integer n (0 < n < 1e4) representing the length of number sequence.
Next line consists n space separated integer representing the sequence. Each of these integers has
absolute value less than 1e9. Next line has an integer q (q < 1e5). q lines follow, each representing a
query. Each query has two space separated integer i, j (1 ≤ i ≤ j ≤ n).
Output
For each case output q lines representing average of unique elements of corresponding range rounded 6 digits after decimal points. No case will have output whose rounding changes if the 10e−9 is added to
or subtracted from answer.
Notes:
Explanation for second query in first sample
The range is (1,10) which includes ten numbers (1, 2, 3, 4, 4, 3, 2, 1, -1, 0). Unique numbers in this
ranges are (1, 2, 3, 4, -1, 0) whose average is 1.5
Explanation for third query in first sample
The range is (3,5) which includes three numbers (3, 4, 4). Unique numbers in this ranges are 3 and
4 whose average is 3.5
Sample Input
2
10
1 2 3 4 4 3 2 1 -1 0
4
1 4
1 10
3 5
8 10
3
1 1 0
1
1 3
Sample Output
Case 1:
2.500000
1.500000
3.500000
0.000000
Case 2:
0.500000
题意:
对已知数组,求给定区间内不同的数的平均值
思路:
分块用莫队,跟Group一样,就是多了个求不同数字种数
#include <bits/stdc++.h>
using namespace std;
map<int, int>coun; //队友说过,map啥都能干,,,这里数太大1e9,个数也就1e4
//身为c语言钉子户,写的东西多了,咱还真就得认,c++就是方便点
struct node
{
int left, right, num;
}str[100005];
int a[10005], belong[10005], number;
bool cmp(struct node x, struct node y)
{
if(belong[x.left]==belong[y.left]) return x.right < y.right;
else return x.left < y.left;
}
double add(int n, double sum)
{
if(coun[n]==0)
{
sum += n;
number++;
}
coun[n]++;
return sum;
}
double subtract(int n, double sum)
{
coun[n]--;
if(coun[n]==0)
{
number--;
sum -= n;
}
return sum;
}
int main()
{
int n, m, t, i, temp, l, r, j;
double re[100005], sum; //一开始sum定义成int,WA了,改成double就对了,不知道什么情况
scanf("%d", &t);
for(j=1;j<=t;j++)
{
coun.clear(); //coun清零
scanf("%d", &n);
temp = sqrt(n);
for(i=1;i<=n;i++)
{
scanf("%d", &a[i]);
belong[i] = (i-1) / temp + 1;
}
scanf("%d", &m);
for(i=1;i<=m;i++)
{
str[i].num = i;
scanf("%d %d", &str[i].left, &str[i].right);
}
sort(str+1, str+m+1, cmp);
l = 1;
r = 0;
sum = 0;
number = 0;
for(i=1;i<=m;i++)
{
while(r<str[i].right)
{
r++;
sum = add(a[r], sum);
}
while(r>str[i].right)
{
sum = subtract(a[r], sum);
r--;
}
while(l<str[i].left)
{
sum = subtract(a[l], sum);
l++;
}
while(l>str[i].left)
{
l--;
sum = add(a[l], sum);
}
re[str[i].num] = 1.0 * sum / number;
}
printf("Case %d:\n", j);
for(i=1;i<=m;i++)
{
printf("%.6f\n", re[i]);
}
}
return 0;
}