Hdu4995revenge of KNN (violent)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.