HDU 4995 Revenge of kNN

来源:互联网 发布:北京软件退税政策 编辑:程序博客网 时间:2024/05/22 00:09

Revenge of kNN

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 364    Accepted Submission(s): 81


Problem Description
In pattern recognition, the k-Nearest Neighbors algorithm (or k-NN for short) is a non-parametric method used for classification and regression. In both cases, the input consists of the k closest training examples in the feature space.
In k-NN regression, the output is the property value for the object. This value is the average of the values of its k nearest neighbors.
---Wikipedia

Today, kNN takes revenge on you. You have to handle a kNN case in one-dimensional coordinate system. There are N points with a position Xi and value Vi. Then there are M kNN queries for point with index i, recalculate its value by averaging the values its k-Nearest Neighbors. Note you have to replace the value of i-th point with the new calculated value. And if there is a tie while choosing k-Nearest Neighbor, choose the one with the minimal index first.
 

Input
The first line contains a single integer T, indicating the number of test cases. 

Each test case begins with three integers N, M and K, in which K indicating the number of k-Nearest Neighbors. Then N lines follows, each line contains two integers Xi and Vi. Then M lines with the queried index Qi follows.

[Technical Specification]
1. 1 <= T <= 5
2. 2<=N<= 100 000, 1<=M<=100 000
3. 1 <= K <= min(N – 1, 10)
4. 1 <= Vi <= 1 000
5. 1 <= Xi <= 1 000 000 000, and no two Xi are identical.
6. 1 <= Qi <= N
 

Output
For each test case, output sum of all queries rounded to six fractional digits.
 

Sample Input
15 3 21 22 33 64 85 8234
 

Sample Output
17.000000
Hint
For the first query, the 2-NN for point 2 is point 1 and 3, so the new value is (2 + 6) / 2 = 4.For the second query, the 2-NN for point 3 is point 2 and 4, and the value of point 2 is changed to 4 by the last query, so the new value is (4 + 8) / 2 = 6.Huge input, faster I/O method is recommended.
 


一开始理解错题意了。。还以为数据是有顺序的。。。


#include <cstring>#include <cstdio>#include <iostream>#include <algorithm>using namespace std;int scan(){   char c;   while(c=getchar(),(c<'0'||c>'9')&&(c!='-'));   bool flag=(c=='-');   if(flag) c=getchar();   int x=0;   while(c>='0'&&c<='9')   {      x=x*10+c-48;      c=getchar();   }   return flag?-x:x;}const int maxn=1e5+100;int n,m,k;struct node{    int x;    int id;}a[maxn];double val[maxn];int p[maxn][22];bool check(int i,int l,int r){    if(r>n)       return true;    if(l<=0)       return false;    if(a[i].x-a[l].x!=a[r].x-a[i].x)       return a[i].x-a[l].x<a[r].x-a[i].x;    return a[l].id<a[r].id;}void work(int x){    int l,r;    l=x-1,r=x+1;    for(int i=0;i<k;i++)    {        if(check(x,l,r))            p[a[x].id][i]=a[l--].id;        else            p[a[x].id][i]=a[r++].id;    }}int cmp(node a,node b){    return a.x<b.x;}int main(){    int T;    T=scan();    while(T--)    {        n=scan(),m=scan(),k=scan();        double ans=0;        for(int i=1;i<=n;i++)        {            a[i].x=scan();            scanf("%lf",&val[i]);            a[i].id=i;        }        sort(a+1,a+1+n,cmp);        for(int i=1;i<=n;i++)            work(i);        int x;        for(int i=0;i<m;i++)        {            x=scan();            double sum=0;            for(int j=0;j<k;j++)               sum+=val[p[x][j]];            val[x]=sum/k;            ans+=val[x];        }        printf("%.6lf\n",ans);    }    return 0;}


0 0