Title: hdu4995revenge of KNN (violent)
Give you n points in one dimension, each of which has X coordinates and v values, and then give you m modifications, each row of the next M line gives you an Qi (the numbers of the First n vertices are 1--n ). It is required that K neighbors closest to X (QI) be retrieved each time, and then the value of X (QI) be changed to (the average value of the values of these K neighbors ), finally, the sum of the M modified values is output. If the distance is the same, the neighbor whose original subscript is small will be taken.
Solution: first sort the N points by X value + brute force. Qi is the subscript rather than X. This is not clear yet, and it is very difficult.
Code:
# Include <cstdio> # include <cstring> # include <algorithm> # include <map> using namespace STD; const int n = 1e5 + 5; typedef long ll; map <int, int> vis; int t, n, m, K; struct node {ll X; Double V; int ID;} node [N]; int CMP (const node & A, const node & B) {return. x <B. x;} double solve () {double ans = 0; vis. clear (); sort (node, node + N, CMP); For (INT I = 0; I <n; I ++) vis [node [I]. id] = I; int POs, TMP; int num; For (INT I = 0; I <m; I ++) {scanf ("% d", & num ); pos = vis [num-1]; // printf ("% d \ n", POS); int p1 = pos-1; int P2 = POS + 1; double sum = 0; TMP = 0; while (TMP <k) {If (p1 =-1) // note the boundary sum + = node [P2 ++]. v; else if (P2 = N) // sum + = node [P1 --]. v; else {If (node [POS]. x-node [P1]. x <node [P2]. x-node [POS]. x) sum + = node [P1 --]. v; else if (node [POS]. x-node [P1]. x> node [P2]. x-node [POS]. x) sum + = node [P2 ++]. v; else if (node [P1]. ID <node [P2]. ID) sum + = node [P1 --]. v; elsesum + = node [P2 ++]. v;} TMP ++;} // printf ("% lf \ n", sum/K); ans + = sum/K; node [POS]. V = sum/K;} return ans;} int main () {scanf ("% d", & T); While (t --) {scanf ("% d", & N, & M, & K); For (INT I = 0; I <n; I ++) {scanf ("% i64d % lf", & node [I]. x, & node [I]. v); node [I]. id = I;} printf ("%. 6lf \ n ", solve ();} return 0 ;}
Hdu4995revenge of KNN (violent)