CodeForces 1208 B. Physics Practical 区间统计,树状数组
One day Vasya was on a physics practical, performing the task on measuring the capacitance. He followed the teacher's advice and did as much as n measurements, and recorded the results in the notebook. After that he was about to show the results to the teacher, but he remembered that at the last lesson, the teacher had made his friend Petya redo the experiment because the largest and the smallest results differed by more than two times. Vasya is lazy, and he does not want to redo the experiment. He wants to do the task and go home play computer games. So he decided to cheat: before Vasya shows the measurements to the teacher, he will erase some of them, so as to make the largest and the smallest results of the remaining measurements differ in no more than two times. In other words, if the remaining measurements have the smallest result x, and the largest result y, then the inequality y ≤ 2·x must fulfill. Of course, to avoid the teacher's suspicion, Vasya wants to remove as few measurement results as possible from his notes.
Help Vasya, find what minimum number of measurement results he will have to erase from his notes so that the largest and the smallest of the remaining results of the measurements differed in no more than two times.
The first line contains integer n (2 ≤ n ≤ 105) — the number of measurements Vasya made. The second line contains n integersc1, c2, ..., cn (1 ≤ ci ≤ 5000) — the results of the measurements. The numbers on the second line are separated by single spaces.
Print a single integer — the minimum number of results Vasya will have to remove.
6
4 5 3 8 3 7
2
4
4 3 2 4
0
In the first sample you can remove the fourth and the sixth measurement results (values 8 and 7). Then the maximum of the remaining values will be 5, and the minimum one will be 3. Or else, you can remove the third and fifth results (both equal 3). After that the largest remaining result will be 8, and the smallest one will be 4.
#include<stdio.h> #include<stdlib.h> #include<math.h> #include<iostream> #include<string.h> using namespace std; const int N = 100007; const int M = 10000; const int inf = 0x3fffffff; #define MIN(a,b) (a)<(b)?(a):(b) #define MAX(a,b) (a)>(b)?(a):(b) int n, m, C[N], val[N]; int Max, Min; void add( int x ) { for( ; x <= M; x += x&(-x)) C[x] += 1; } int read( int x ) { int res = 0; for( ; x >= 1; x -= x&(-x) ) res += C[x]; return res; } int main(){ freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); while( scanf("%d", &m) != EOF) { int x; Max = 0; Min = 5000; memset( C, 0, sizeof(C)); memset( val, 0, sizeof(val)); for(int i = 0; i < m; i++) { scanf("%d", &x); val[x] = 1; add( x ); Max = MAX( Max, x ); Min = MIN( Min, x ); } int ans = inf; for(int x = 1; x <= Max; x++) { if( val[x] == 0 ) continue; int y = 2*x; int tmp = read(M)-read(y)+read(x-1); ans = MIN( ans, tmp ); } printf("%d\n", ans); } return 0; }
解题思路:N - Max{ A[i],2*A[i] } 即可, 使用树状数组优化统计
具体代码: